feat: multi-account system

This commit is contained in:
2023-04-14 12:33:19 +01:00
parent 723589fec7
commit fe788853c9
58 changed files with 966 additions and 1080 deletions

View File

@ -0,0 +1,41 @@
type HookFn = () => void;
interface HookFilter {
fn: HookFn;
}
/**
* Simple React hookable store with manual change notifications
*/
export default abstract class ExternalStore<TSnapshot> {
#hooks: Array<HookFilter> = [];
#snapshot: Readonly<TSnapshot> = {} as Readonly<TSnapshot>;
#changed = true;
hook(fn: HookFn) {
this.#hooks.push({
fn,
});
return () => {
const idx = this.#hooks.findIndex(a => a.fn === fn);
if (idx >= 0) {
this.#hooks.splice(idx, 1);
}
};
}
snapshot() {
if (this.#changed) {
this.#snapshot = this.takeSnapshot();
this.#changed = false;
}
return this.#snapshot;
}
protected notifyChange() {
this.#changed = true;
this.#hooks.forEach(h => h.fn());
}
abstract takeSnapshot(): TSnapshot;
}