forked from hyoo-ru/mam_mol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
local.ts
52 lines (39 loc) · 1.21 KB
/
local.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
namespace $ {
export class $mol_state_local< Value > extends $mol_object {
static 'native()' : Pick< Storage , 'getItem'|'setItem'|'removeItem' >
static native() {
if( this['native()'] ) return this['native()']
check : try {
const native = $mol_dom_context.localStorage
if( !native ) break check
native.setItem( '' , '' )
native.removeItem( '' )
return this['native()'] = native
} catch( error ) {
console.warn( error )
}
return this['native()'] = {
getItem( key : string ) {
return this[ ':' + key ]
} ,
setItem( key : string , value : string ) {
this[ ':' + key ] = value
} ,
removeItem( key : string ) {
this[ ':' + key ] = void 0
}
}
}
@ $mol_mem_key
static value< Value >( key : string , next? : Value , force? : $mol_mem_force ) : Value | null {
if( next === void 0 ) return JSON.parse( this.native().getItem( key ) || 'null' )
if( next === null ) this.native().removeItem( key )
else this.native().setItem( key , JSON.stringify( next ) )
return next
}
prefix() { return '' }
value( key : string , next? : Value ) {
return $mol_state_local.value( this.prefix() + '.' + key , next )
}
}
}