deno.land / x / nano_jsx@v0.1.0 / store.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import { isSSR } from './core.ts'
export class Store<S = any> { private _state: S private _prevState: S private _listeners: Map<string, Function> = new Map() private _storage: 'memory' | 'local' | 'session' private _id: string
/** * Create your own Store. * @param defaultState Pass the initial State. * @param name The name of the Store (only required if you persist the state in localStorage or sessionStorage). * @param storage Pass 'memory', 'local' or 'session'. */ constructor(defaultState: Object, name: string = '', storage: 'memory' | 'local' | 'session' = 'memory') { if (isSSR()) storage = 'memory'
this._id = name this._storage = storage
this._state = this._prevState = defaultState as any
if (storage === 'memory' || !storage) return
const Storage = storage === 'local' ? localStorage : sessionStorage
// get/set initial state of Storage const item = Storage.getItem(this._id) if (item) { this._state = this._prevState = JSON.parse(item) } else Storage.setItem(this._id, JSON.stringify(defaultState)) }
private persist(newState: S) { if (this._storage === 'memory') return const Storage = this._storage === 'local' ? localStorage : sessionStorage Storage.setItem(this._id, JSON.stringify(newState)) }
/** Clears the state of the whole store. */ public clear() { // @ts-ignore this._state = this._prevState = undefined
if (this._storage === 'local') localStorage.removeItem(this._id) else if (this._storage === 'session') sessionStorage.removeItem(this._id) }
public setState(newState: S) { this.state = newState }
public set state(newState: S) { this._prevState = this._state this._state = newState
this.persist(newState)
this._listeners.forEach(fnc => { fnc(this._state, this._prevState) }) }
public get state(): S { return this._state }
public use() { const id = Math.random().toString(36).substring(2, 9) const _this = this return { get state(): S { return _this.state }, setState: (newState: S) => { this.state = newState }, subscribe: (fnc: (newState: S, prevState: S) => void) => { this._listeners.set(id, fnc) }, cancel: () => { if (this._listeners.has(id)) this._listeners.delete(id) } } }}
nano_jsx

Version Info

Tagged at
8 months ago