UNPKG

@zag-js/store

Version:

The reactive store package for zag machines

24 lines (23 loc) 767 B
// src/proxy-computed.ts import { proxy, snapshot } from "./proxy.mjs"; function proxyWithComputed(initialObject, computedFns) { const keys = Object.keys(computedFns); keys.forEach((key) => { if (Object.getOwnPropertyDescriptor(initialObject, key)) { throw new Error("object property already defined"); } const computedFn = computedFns[key]; const { get, set } = typeof computedFn === "function" ? { get: computedFn } : computedFn; const desc = {}; desc.get = () => get(snapshot(proxyObject)); if (set) { desc.set = (newValue) => set(proxyObject, newValue); } Object.defineProperty(initialObject, key, desc); }); const proxyObject = proxy(initialObject); return proxyObject; } export { proxyWithComputed };