import { status } from "./helper" ; export default class Storage { is_new_version : boolean ; storage : any = { } ; constructor ( public config ) { this . config = config ; this . is_new_version = false ; this . init ( ) ; } init ( ) { if ( typeof Storage !== "undefined" ) { if ( localStorage . getItem ( "spritemate_config" ) == null ) { console . log ( "creating local storage file..." ) ; localStorage . setItem ( "spritemate_config" , JSON . stringify ( this . config ) ) ; this . is_new_version = true ; } this . storage = JSON . parse ( localStorage . getItem ( "spritemate_config" ) || "{}" ) ; if ( this . config . version > this . storage . version ) { this . storage = JSON . parse ( JSON . stringify ( this . config ) ) ; this . write ( this . storage ) ; this . is_new_version = true ; console . log ( "updating storage" ) ; } this . config = JSON . parse ( JSON . stringify ( this . storage ) ) ; } else { } } write ( data ) { if ( typeof Storage !== "undefined" ) { localStorage . setItem ( "spritemate_config" , JSON . stringify ( data ) ) ; } else { status ( "I can't write to local web storage." ) ; } } read ( ) { if ( typeof Storage !== "undefined" ) { return JSON . parse ( localStorage . getItem ( "spritemate_config" ) || "{}" ) ; } else { status ( "I can't read from web storage." ) ; this . storage = this . config ; } } is_updated_version ( ) { return this . is_new_version ; } get_config ( ) { return this . config ; } }