27 lines
499 B
JavaScript
27 lines
499 B
JavaScript
export class AppState {
|
|
constructor(initialState = {}) {
|
|
this._state = { ...initialState };
|
|
this._listeners = new Set();
|
|
}
|
|
|
|
get(key) {
|
|
return this._state[key];
|
|
}
|
|
|
|
getAll() {
|
|
return { ...this._state };
|
|
}
|
|
|
|
set(patch) {
|
|
this._state = { ...this._state, ...patch };
|
|
for (const listener of this._listeners) {
|
|
listener(this.getAll());
|
|
}
|
|
}
|
|
|
|
subscribe(listener) {
|
|
this._listeners.add(listener);
|
|
return () => this._listeners.delete(listener);
|
|
}
|
|
}
|