-
-
Notifications
You must be signed in to change notification settings - Fork 59
/
session.ts
52 lines (39 loc) · 1.21 KB
/
session.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_session< 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.sessionStorage
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_key
static value< Value >( key : string , next? : Value ) : Value {
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_session.value( this.prefix() + '.' + key , next )
}
}
}