UNPKG

@virtualstate/examples

Version:
48 lines 1.72 kB
import { performCached } from "./perform-cached.js"; import { h, isVNode, TokenConstructor } from "@virtualstate/fringe"; import { isPromise } from "iterable"; const moduleCache = new WeakMap(); export async function* Cache({ cache = moduleCache, fn }, input) { if (!input?.children) return; yield* mapChildren(input); async function ProxyChildren({ child }) { if (!isVNode(child)) return; return new Proxy(child, { get(target, p) { if (p === 'children') { if (!child.children) return undefined; return { async *[Symbol.asyncIterator]() { yield* mapChildren(child); } }; } return target[p]; } }); } async function* mapChildren(child) { yield* performCached([cache], true, getKey(child), proxyChildren()); function getKey(node) { const source = node.source; if (typeof source === "function" || typeof source === "object") { return source; } if (node[TokenConstructor]) { return node[TokenConstructor]; } return node; } async function* proxyChildren() { for await (const children of child.children) { yield await Promise.all(children .map((child) => fn(child)) .map(async (child) => h(ProxyChildren, { child: isPromise(child) ? await child : child }))); } } } } //# sourceMappingURL=cache.js.map