1 | import { EventEmitter } from "events";
|
2 |
|
3 | export class Store<T extends Object> {
|
4 | protected state: T;
|
5 | protected event = new EventEmitter();
|
6 |
|
7 | |
8 |
|
9 |
|
10 |
|
11 |
|
12 | constructor(public name: string, private initialState: T) {
|
13 | this.state = this.initialState;
|
14 | }
|
15 |
|
16 |
|
17 | public get on() {
|
18 | return this.event.on;
|
19 | }
|
20 |
|
21 |
|
22 | public get once() {
|
23 | return this.event.once;
|
24 | }
|
25 |
|
26 |
|
27 | public update(state: Partial<T>) {
|
28 | this.state = { ...this.state, ...state };
|
29 | }
|
30 |
|
31 |
|
32 | public get<Key extends keyof T>(key: Key): T[Key] {
|
33 | return this.state[key];
|
34 | }
|
35 |
|
36 |
|
37 | public reset() {
|
38 | this.state = this.initialState;
|
39 | }
|
40 |
|
41 |
|
42 | public dispatch() {
|
43 | this.event.emit("newState", this.state);
|
44 | }
|
45 | }
|
46 |
|
47 | interface EntityState<T> {
|
48 | ids: string[];
|
49 | actives: string[];
|
50 | entities: {
|
51 | [id: string]: T;
|
52 | };
|
53 | [key: string]: any;
|
54 | }
|
55 |
|
56 | export class EntityStore<T> extends Store<EntityState<T>> {
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 | constructor(
|
64 | name: string,
|
65 | initialState: EntityState<T>,
|
66 | private keyId: keyof T
|
67 | ) {
|
68 | super(name, initialState);
|
69 | }
|
70 |
|
71 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 | get entities() {
|
77 | return this.state.entities;
|
78 | }
|
79 |
|
80 |
|
81 | get ids() {
|
82 | return this.state.ids;
|
83 | }
|
84 |
|
85 |
|
86 | get actives() {
|
87 | return this.state.actives;
|
88 | }
|
89 |
|
90 |
|
91 | get length() {
|
92 | return this.state.ids.length;
|
93 | }
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 | public add(entity: T) {
|
101 | const id = entity[this.keyId];
|
102 | if (typeof id !== "string") {
|
103 | throw new Error(
|
104 | `${id} should be of type 'string', but is of type ${typeof id}`
|
105 | );
|
106 | }
|
107 | this.state.entities[id as string] = entity;
|
108 | this.state.ids.push(id);
|
109 | }
|
110 |
|
111 |
|
112 | public remove(id: string) {
|
113 | delete this.state.entities[id];
|
114 | this.state.ids.slice(this.state.ids.indexOf(id));
|
115 | this.state.actives.slice(this.state.ids.indexOf(id));
|
116 | }
|
117 |
|
118 |
|
119 | public updateOne(id: string, update: Partial<T>) {
|
120 | this.state.entities[id] = {
|
121 | ...this.state.entities[id],
|
122 | ...update
|
123 | };
|
124 | }
|
125 |
|
126 |
|
127 | public activate(ids: string[] | string) {
|
128 | Array.isArray(ids)
|
129 | ? this.state.actives.push()
|
130 | : this.state.actives.concat(ids);
|
131 | this.event.emit("activate", ids);
|
132 | }
|
133 |
|
134 |
|
135 | public deactivate(ids: string[] | string) {
|
136 | Array.isArray(ids)
|
137 | ? ids.forEach(id => this.state.actives.slice(this.state.ids.indexOf(id)))
|
138 | : this.state.actives.slice(this.state.ids.indexOf(ids));
|
139 | }
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 |
|
146 | getOne(id: string) {
|
147 | return this.state.entities[id];
|
148 | }
|
149 |
|
150 |
|
151 | getMany(ids: string[]) {
|
152 | return ids.map(id => this.state.entities[id]);
|
153 | }
|
154 |
|
155 |
|
156 | getAll() {
|
157 | return this.state.ids.map(id => this.state.entities[id]);
|
158 | }
|
159 |
|
160 |
|
161 | getActives() {
|
162 | return this.state.actives.map(id => this.state.entities[id]);
|
163 | }
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 |
|
170 | public isActive(id: string) {
|
171 | return this.state.actives.includes(id);
|
172 | }
|
173 |
|
174 |
|
175 | public hasEntity(id: string) {
|
176 | return this.state.ids.includes(id);
|
177 | }
|
178 |
|
179 |
|
180 | public isEmpty() {
|
181 | return this.state.ids.length === 0;
|
182 | }
|
183 | }
|
184 |
|
185 |
|
186 | function localState(stores: Store<any>[]) {
|
187 | stores.forEach(store => {
|
188 | const name = store.name;
|
189 | store.on("newState", (state: any) => {
|
190 | localStorage.setItem(name, JSON.stringify(state));
|
191 | });
|
192 | });
|
193 | }
|