UNPKG

@tldraw/store

Version:

tldraw infinite canvas SDK (store).

150 lines (149 loc) 4.05 kB
import { atom, transact, UNINITIALIZED } from "@tldraw/state"; import { assert } from "@tldraw/utils"; import { emptyMap } from "./ImmutableMap.mjs"; class AtomMap { constructor(name, entries) { this.name = name; let atoms = emptyMap(); if (entries) { atoms = atoms.withMutations((atoms2) => { for (const [k, v] of entries) { atoms2.set(k, atom(`${name}:${String(k)}`, v)); } }); } this.atoms = atom(`${name}:atoms`, atoms); } atoms; /** @internal */ getAtom(key) { const valueAtom = this.atoms.__unsafe__getWithoutCapture().get(key); if (!valueAtom) { this.atoms.get(); return void 0; } return valueAtom; } get(key) { const value = this.getAtom(key)?.get(); assert(value !== UNINITIALIZED); return value; } __unsafe__getWithoutCapture(key) { const valueAtom = this.atoms.__unsafe__getWithoutCapture().get(key); if (!valueAtom) return void 0; const value = valueAtom.__unsafe__getWithoutCapture(); assert(value !== UNINITIALIZED); return value; } has(key) { const valueAtom = this.getAtom(key); if (!valueAtom) { return false; } return valueAtom.get() !== UNINITIALIZED; } __unsafe__hasWithoutCapture(key) { const valueAtom = this.atoms.__unsafe__getWithoutCapture().get(key); if (!valueAtom) return false; assert(valueAtom.__unsafe__getWithoutCapture() !== UNINITIALIZED); return true; } set(key, value) { const existingAtom = this.atoms.__unsafe__getWithoutCapture().get(key); if (existingAtom) { existingAtom.set(value); } else { this.atoms.update((atoms) => { return atoms.set(key, atom(`${this.name}:${String(key)}`, value)); }); } return this; } update(key, updater) { const valueAtom = this.atoms.__unsafe__getWithoutCapture().get(key); if (!valueAtom) { throw new Error(`AtomMap: key ${key} not found`); } const value = valueAtom.__unsafe__getWithoutCapture(); assert(value !== UNINITIALIZED); valueAtom.set(updater(value)); } delete(key) { const valueAtom = this.atoms.__unsafe__getWithoutCapture().get(key); if (!valueAtom) { return false; } transact(() => { valueAtom.set(UNINITIALIZED); this.atoms.update((atoms) => { return atoms.delete(key); }); }); return true; } deleteMany(keys) { return transact(() => { const deleted = []; const newAtoms = this.atoms.get().withMutations((atoms) => { for (const key of keys) { const valueAtom = atoms.get(key); if (!valueAtom) continue; const oldValue = valueAtom.get(); assert(oldValue !== UNINITIALIZED); deleted.push([key, oldValue]); atoms.delete(key); valueAtom.set(UNINITIALIZED); } }); if (deleted.length) { this.atoms.set(newAtoms); } return deleted; }); } clear() { return transact(() => { for (const valueAtom of this.atoms.__unsafe__getWithoutCapture().values()) { valueAtom.set(UNINITIALIZED); } this.atoms.set(emptyMap()); }); } *entries() { for (const [key, valueAtom] of this.atoms.get()) { const value = valueAtom.get(); assert(value !== UNINITIALIZED); yield [key, value]; } } *keys() { for (const key of this.atoms.get().keys()) { yield key; } } *values() { for (const valueAtom of this.atoms.get().values()) { const value = valueAtom.get(); assert(value !== UNINITIALIZED); yield value; } } // eslint-disable-next-line no-restricted-syntax get size() { return this.atoms.get().size; } forEach(callbackfn, thisArg) { for (const [key, value] of this.entries()) { callbackfn.call(thisArg, value, key, this); } } [Symbol.iterator]() { return this.entries(); } [Symbol.toStringTag] = "AtomMap"; } export { AtomMap }; //# sourceMappingURL=AtomMap.mjs.map