-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
62 lines (49 loc) · 1.36 KB
/
index.js
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
import Emitter from '@tmbr/emitter';
import { isFunction, toArray } from '@tmbr/utils';
class Store {
#emitter;
#initial;
#state;
constructor(initial = {}) {
this.#emitter = new Emitter();
this.#initial = {...initial};
this.#state = {...initial};
}
get state() {
return this.get();
}
get(key) {
return key ? this.#state[key] : {...this.#state};
}
set(...args) {
switch (typeof args[0]) {
case 'function':
this.set(args[0](this.get()));
break;
case 'object':
const updates = Object.entries(args[0]);
updates.forEach(([key, value]) => this.set(key, value));
break;
default:
if (this.#state[args[0]] === args[1]) return;
this.#state[args[0]] = args[1];
this.#emitter.emit(args[0], this.get());
this.#emitter.emit('*', this.get());
break;
}
}
subscribe(key, callback, immediate = true) {
if (isFunction(key)) return this.subscribe('*', key, callback);
toArray(key).forEach(k => this.#emitter.on(k, callback));
immediate && callback(this.get());
return () => this.unsubscribe(key, callback);
}
unsubscribe(key, callback) {
if (isFunction(key)) return this.unsubscribe('*', key);
toArray(key).forEach(k => this.#emitter.off(k, callback));
}
reset() {
this.set(this.#initial);
}
}
export default Store;