{"version":3,"file":"deepsignal-react.mjs","sources":["../../core/src/index.ts","../src/index.ts"],"sourcesContent":["import { computed, signal, Signal } from \"@preact/signals-core\";\n\nconst proxyToSignals = new WeakMap();\nconst objToProxy = new WeakMap();\nconst arrayToArrayOfSignals = new WeakMap();\nconst proxies = new WeakSet();\nconst objToIterable = new WeakMap();\nconst rg = /^\\$/;\nlet peeking = false;\n\nexport const deepSignal = <T extends object>(obj: T): DeepSignal<T> => {\n\tif (!shouldProxy(obj)) throw new Error(\"This object can't be observed.\");\n\tif (!objToProxy.has(obj))\n\t\tobjToProxy.set(obj, createProxy(obj, objectHandlers) as DeepSignal<T>);\n\treturn objToProxy.get(obj);\n};\n\nexport const peek = <\n\tT extends DeepSignalObject<object>,\n\tK extends keyof RevertDeepSignalObject<T>\n>(\n\tobj: T,\n\tkey: K\n): RevertDeepSignal<RevertDeepSignalObject<T>[K]> => {\n\tpeeking = true;\n\tconst value = obj[key];\n\ttry {\n\t\tpeeking = false;\n\t} catch (e) {}\n\treturn value as RevertDeepSignal<RevertDeepSignalObject<T>[K]>;\n};\n\nconst createProxy = (target: object, handlers: ProxyHandler<object>) => {\n\tconst proxy = new Proxy(target, handlers);\n\tproxies.add(proxy);\n\treturn proxy;\n};\n\nconst throwOnMutation = () => {\n\tthrow new Error(\"Don't mutate the signals directly.\");\n};\n\nconst get =\n\t(isArrayOfSignals: boolean) =>\n\t(target: object, fullKey: string, receiver: object): unknown => {\n\t\tif (peeking) return Reflect.get(target, fullKey, receiver);\n\t\tlet returnSignal = isArrayOfSignals || fullKey[0] === \"$\";\n\t\tif (!isArrayOfSignals && returnSignal && Array.isArray(target)) {\n\t\t\tif (fullKey === \"$\") {\n\t\t\t\tif (!arrayToArrayOfSignals.has(target))\n\t\t\t\t\tarrayToArrayOfSignals.set(target, createProxy(target, arrayHandlers));\n\t\t\t\treturn arrayToArrayOfSignals.get(target);\n\t\t\t}\n\t\t\treturnSignal = fullKey === \"$length\";\n\t\t}\n\t\tif (!proxyToSignals.has(receiver)) proxyToSignals.set(receiver, new Map());\n\t\tconst signals = proxyToSignals.get(receiver);\n\t\tconst key = returnSignal ? fullKey.replace(rg, \"\") : fullKey;\n\t\tif (\n\t\t\t!signals.has(key) &&\n\t\t\ttypeof Object.getOwnPropertyDescriptor(target, key)?.get === \"function\"\n\t\t) {\n\t\t\tsignals.set(\n\t\t\t\tkey,\n\t\t\t\tcomputed(() => Reflect.get(target, key, receiver))\n\t\t\t);\n\t\t} else {\n\t\t\tlet value = Reflect.get(target, key, receiver);\n\t\t\tif (returnSignal && typeof value === \"function\") return;\n\t\t\tif (typeof key === \"symbol\" && wellKnownSymbols.has(key)) return value;\n\t\t\tif (!signals.has(key)) {\n\t\t\t\tif (shouldProxy(value)) {\n\t\t\t\t\tif (!objToProxy.has(value))\n\t\t\t\t\t\tobjToProxy.set(value, createProxy(value, objectHandlers));\n\t\t\t\t\tvalue = objToProxy.get(value);\n\t\t\t\t}\n\t\t\t\tsignals.set(key, signal(value));\n\t\t\t}\n\t\t}\n\t\treturn returnSignal ? signals.get(key) : signals.get(key).value;\n\t};\n\nconst objectHandlers = {\n\tget: get(false),\n\tset(target: object, fullKey: string, val: any, receiver: object): boolean {\n\t\tif (!proxyToSignals.has(receiver)) proxyToSignals.set(receiver, new Map());\n\t\tconst signals = proxyToSignals.get(receiver);\n\t\tif (fullKey[0] === \"$\") {\n\t\t\tif (!(val instanceof Signal)) throwOnMutation();\n\t\t\tconst key = fullKey.replace(rg, \"\");\n\t\t\tsignals.set(key, val);\n\t\t\treturn Reflect.set(target, key, val.peek(), receiver);\n\t\t} else {\n\t\t\tlet internal = val;\n\t\t\tif (shouldProxy(val)) {\n\t\t\t\tif (!objToProxy.has(val))\n\t\t\t\t\tobjToProxy.set(val, createProxy(val, objectHandlers));\n\t\t\t\tinternal = objToProxy.get(val);\n\t\t\t}\n\t\t\tconst isNew = !(fullKey in target);\n\t\t\tconst result = Reflect.set(target, fullKey, val, receiver);\n\t\t\tif (!signals.has(fullKey)) signals.set(fullKey, signal(internal));\n\t\t\telse signals.get(fullKey).value = internal;\n\t\t\tif (isNew && objToIterable.has(target)) objToIterable.get(target).value++;\n\t\t\tif (Array.isArray(target) && signals.has(\"length\"))\n\t\t\t\tsignals.get(\"length\").value = target.length;\n\t\t\treturn result;\n\t\t}\n\t},\n\tdeleteProperty(target: object, key: string): boolean {\n\t\tif (key[0] === \"$\") throwOnMutation();\n\t\tconst signals = proxyToSignals.get(objToProxy.get(target));\n\t\tconst result = Reflect.deleteProperty(target, key);\n\t\tif (signals && signals.has(key)) signals.get(key).value = undefined;\n\t\tobjToIterable.has(target) && objToIterable.get(target).value++;\n\t\treturn result;\n\t},\n\townKeys(target: object): (string | symbol)[] {\n\t\tif (!objToIterable.has(target)) objToIterable.set(target, signal(0));\n\t\t(objToIterable as any)._ = objToIterable.get(target).value;\n\t\treturn Reflect.ownKeys(target);\n\t},\n};\n\nconst arrayHandlers = {\n\tget: get(true),\n\tset: throwOnMutation,\n\tdeleteProperty: throwOnMutation,\n};\n\nconst wellKnownSymbols = new Set(\n\tObject.getOwnPropertyNames(Symbol)\n\t\t.map(key => Symbol[key as WellKnownSymbols])\n\t\t.filter(value => typeof value === \"symbol\")\n);\nconst supported = new Set([Object, Array]);\nconst shouldProxy = (val: any): boolean => {\n\tif (typeof val !== \"object\" || val === null) return false;\n\tconst isBuiltIn =\n\t\ttypeof val.constructor === \"function\" &&\n\t\tval.constructor.name in globalThis &&\n\t\t(globalThis as any)[val.constructor.name] === val.constructor;\n\treturn (!isBuiltIn || supported.has(val.constructor)) && !proxies.has(val);\n};\n\n/** TYPES **/\n\nexport type DeepSignal<T> = T extends Function\n\t? T\n\t: T extends Array<unknown>\n\t? DeepSignalArray<T>\n\t: T extends object\n\t? DeepSignalObject<T>\n\t: T;\n\ntype DeepSignalObject<T extends object> = {\n\t[P in keyof T & string as `$${P}`]?: T[P] extends Function\n\t\t? never\n\t\t: Signal<T[P]>;\n} & {\n\t[P in keyof T]: DeepSignal<T[P]>;\n};\n\n/** @ts-expect-error **/\ninterface DeepArray<T> extends Array<T> {\n\tmap: <U>(\n\t\tcallbackfn: (\n\t\t\tvalue: DeepSignal<T>,\n\t\t\tindex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => U,\n\t\tthisArg?: any\n\t) => U[];\n\tforEach: (\n\t\tcallbackfn: (\n\t\t\tvalue: DeepSignal<T>,\n\t\t\tindex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => void,\n\t\tthisArg?: any\n\t) => void;\n\tconcat(...items: ConcatArray<T>[]): DeepSignalArray<T[]>;\n\tconcat(...items: (T | ConcatArray<T>)[]): DeepSignalArray<T[]>;\n\treverse(): DeepSignalArray<T[]>;\n\tshift(): DeepSignal<T> | undefined;\n\tslice(start?: number, end?: number): DeepSignalArray<T[]>;\n\tsplice(start: number, deleteCount?: number): DeepSignalArray<T[]>;\n\tsplice(\n\t\tstart: number,\n\t\tdeleteCount: number,\n\t\t...items: T[]\n\t): DeepSignalArray<T[]>;\n\tfilter<S extends T>(\n\t\tpredicate: (\n\t\t\tvalue: DeepSignal<T>,\n\t\t\tindex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => value is DeepSignal<S>,\n\t\tthisArg?: any\n\t): DeepSignalArray<S[]>;\n\tfilter(\n\t\tpredicate: (\n\t\t\tvalue: DeepSignal<T>,\n\t\t\tindex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => unknown,\n\t\tthisArg?: any\n\t): DeepSignalArray<T[]>;\n\treduce(\n\t\tcallbackfn: (\n\t\t\tpreviousValue: DeepSignal<T>,\n\t\t\tcurrentValue: DeepSignal<T>,\n\t\t\tcurrentIndex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => T\n\t): DeepSignal<T>;\n\treduce(\n\t\tcallbackfn: (\n\t\t\tpreviousValue: DeepSignal<T>,\n\t\t\tcurrentValue: DeepSignal<T>,\n\t\t\tcurrentIndex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => DeepSignal<T>,\n\t\tinitialValue: T\n\t): DeepSignal<T>;\n\treduce<U>(\n\t\tcallbackfn: (\n\t\t\tpreviousValue: U,\n\t\t\tcurrentValue: DeepSignal<T>,\n\t\t\tcurrentIndex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => U,\n\t\tinitialValue: U\n\t): U;\n\treduceRight(\n\t\tcallbackfn: (\n\t\t\tpreviousValue: DeepSignal<T>,\n\t\t\tcurrentValue: DeepSignal<T>,\n\t\t\tcurrentIndex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => T\n\t): DeepSignal<T>;\n\treduceRight(\n\t\tcallbackfn: (\n\t\t\tpreviousValue: DeepSignal<T>,\n\t\t\tcurrentValue: DeepSignal<T>,\n\t\t\tcurrentIndex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => DeepSignal<T>,\n\t\tinitialValue: T\n\t): DeepSignal<T>;\n\treduceRight<U>(\n\t\tcallbackfn: (\n\t\t\tpreviousValue: U,\n\t\t\tcurrentValue: DeepSignal<T>,\n\t\t\tcurrentIndex: number,\n\t\t\tarray: DeepSignalArray<T[]>\n\t\t) => U,\n\t\tinitialValue: U\n\t): U;\n}\ntype ArrayType<T> = T extends Array<infer I> ? I : T;\ntype DeepSignalArray<T> = DeepArray<ArrayType<T>> & {\n\t[key: number]: DeepSignal<ArrayType<T>>;\n\t$?: { [key: number]: Signal<ArrayType<T>> };\n\t$length?: Signal<number>;\n};\n\nexport declare const useDeepSignal: <T extends object>(obj: T) => DeepSignal<T>;\n\ntype FilterSignals<K> = K extends `$${infer P}` ? never : K;\ntype RevertDeepSignalObject<T> = Pick<T, FilterSignals<keyof T>>;\ntype RevertDeepSignalArray<T> = Omit<T, \"$\" | \"$length\">;\n\nexport type RevertDeepSignal<T> = T extends Array<unknown>\n\t? RevertDeepSignalArray<T>\n\t: T extends object\n\t? RevertDeepSignalObject<T>\n\t: T;\n\ntype WellKnownSymbols =\n\t| \"asyncIterator\"\n\t| \"hasInstance\"\n\t| \"isConcatSpreadable\"\n\t| \"iterator\"\n\t| \"match\"\n\t| \"matchAll\"\n\t| \"replace\"\n\t| \"search\"\n\t| \"species\"\n\t| \"split\"\n\t| \"toPrimitive\"\n\t| \"toStringTag\"\n\t| \"unscopables\";\n","import \"@preact/signals-react\";\nimport { useMemo } from \"react\";\nimport { deepSignal, type DeepSignal } from \"../../core/src\";\n\nexport const useDeepSignal = <T extends object>(obj: T): DeepSignal<T> => {\n\treturn useMemo(() => deepSignal(obj), []);\n};\n\nexport * from \"../../core/src\";\n"],"names":["useMemo","Signal","signal","computed","proxyToSignals","WeakMap","objToProxy","arrayToArrayOfSignals","proxies","WeakSet","objToIterable","rg","peeking","deepSignal","obj","shouldProxy","Error","has","set","createProxy","objectHandlers","get","peek","key","value","e","target","handlers","proxy","Proxy","add","throwOnMutation","isArrayOfSignals","fullKey","receiver","_Object$getOwnPropert","Reflect","returnSignal","Array","isArray","arrayHandlers","Map","signals","replace","Object","getOwnPropertyDescriptor","wellKnownSymbols","val","internal","isNew","result","length","deleteProperty","undefined","ownKeys","_","Set","getOwnPropertyNames","Symbol","map","filter","supported","constructor","name","globalThis","useDeepSignal"],"mappings":"MAEA,0CAAAA,MAAA,yBAAAC,YAAAC,cAAAC,MAAA,uBAAA,MAAoBC,EAAG,IAAIC,QACXC,EAAG,IAAID,QACIE,EAAG,IAAIF,QACrBG,EAAG,IAAIC,QACDC,EAAG,IAAIL,QAClBM,EAAG,MACX,IAAIC,GAAU,EAEDC,MAAAA,EAAgCC,IAC5C,IAAKC,EAAYD,GAAM,MAAM,IAAAE,MAAU,kCAGvC,OAFKV,EAAWW,IAAIH,IACnBR,EAAWY,IAAIJ,EAAKK,EAAYL,EAAKM,IACrBd,EAACe,IAAIP,EAAf,EAGSQ,EAAG,CAInBR,EACAS,KAEAX,GAAU,EACV,MAAMY,EAAQV,EAAIS,GAClB,IACCX,GAAU,CAEX,CADE,MAAOa,GACT,CAA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