-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
local.ts
65 lines (49 loc) · 1.39 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
53
54
55
56
57
58
59
60
61
62
63
64
65
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: any ) {
console.warn( error )
}
return this['native()'] = {
getItem( key : string ) {
return (this as any)[ ':' + key ]
} ,
setItem( key : string , value : string ) {
(this as any)[ ':' + key ] = value
} ,
removeItem( key : string ) {
(this as any)[ ':' + key ] = void 0
}
}
}
@ $mol_mem
static changes( next?: StorageEvent ) { return next }
@ $mol_mem_key
static value< Value >(
key : string ,
next? : Value | null ,
) : Value | null {
this.changes()
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 ) )
this.$.$mol_storage.persisted( true )
}
return next
}
prefix() { return '' }
value( key : string , next? : Value ) {
return $mol_state_local.value( this.prefix() + '.' + key , next )
}
}
}