UNPKG

@virtualstate/examples

Version:
58 lines 1.98 kB
import { createFragment, EnableThen, isTokenVNode, isVNode } from "@virtualstate/fringe"; import { read } from "./read.js"; export const Delivery = Symbol("📦"); export class Store { static [EnableThen] = true; #options; #domainSources; #domainTokens; #domain = undefined; #state; constructor(options, state) { this.#options = options; this.#state = state || createFragment({}); this.#domainTokens = Object.freeze([...options.domain]); this.#domainSources = this.#domainTokens.map(token => token.source); } #isDomainSource = (value) => { return this.#domainSources.includes(value); }; #isDomainToken = (value) => { if (!isVNode(value)) return false; if (!this.#isDomainSource(value.source)) return false; const found = this.#domainTokens.find(token => token.source === value.source); if (!isTokenVNode(found)) { throw new Error("Please report this bug, store should have the dimai"); } return found.is(value); }; get(key) { if (this.#isDomainToken(key)) { return this.get(key.source); } else { return this.#domain?.get(key) ?? new Set(); } } async *[Symbol.asyncIterator]() { for await (const children of this.#state.children) { const nextDomain = new Map(); for await (const child of read(this.#options, this.#state)) { const currentArray = nextDomain.get(child.source); const array = currentArray ?? new Set(); array.add(child); if (!currentArray) { nextDomain.set(child.source, array); } } this.#domain = nextDomain; yield { reference: Delivery, source: nextDomain }; } } } //# sourceMappingURL=store.js.map