UNPKG

@wener/console

Version:

Base console UI toolkit

48 lines (47 loc) 1.18 kB
import { get, set } from "@wener/utils"; import { createStore } from "zustand"; import { immer } from "zustand/middleware/immer"; export class DynamicStore { store; constructor(store = createStore(immer(()=>{ return {}; }))){ this.store = store; } get value() { return this.store.getState(); } add(type, payload) { this.store.setState((s)=>{ let last = get(s, type, []); if (!Array.isArray(last)) { last = [ last ]; } Array.isArray(payload) ? last.push(...payload) : last.push(payload); set(s, type, last, false); }); } collect(type) { let found = get(this.value, type) ?? []; if (!Array.isArray(found)) { found = [ found ]; } return found; } set(key, value, { merge } = {}) { this.store.setState((s)=>{ set(s, key, value, merge); }); } get(key) { return get(this.value, key); } as() { return this; } } //# sourceMappingURL=DynamicStore.js.map