UNPKG

use-reactive-object

Version:
1 lines 8.23 kB
{"version":3,"file":"index.esm.mjs","sources":["../src/utils/isObject.ts","../src/buildProxy.ts","../src/utils/isNativeObject.ts","../src/useReactiveObject.ts","../src/reactive-object.ts"],"sourcesContent":["const isDateObject = (value: unknown): value is Date => value instanceof Date;\n\nconst isNullOrUndefined = (value: unknown): value is null | undefined => value == null;\n\nconst isObjectType = (value: unknown): value is object =>\n typeof value === 'object';\n\nconst isObject = <T extends object>(value: unknown): value is T =>\n !isNullOrUndefined(value) &&\n isObjectType(value) &&\n !isDateObject(value);\n\nexport default isObject","import { BuildProxyConfig, BuildProxyParams } from \"./types\";\nimport isNativeObject from \"./utils/isNativeObject\";\nimport isObject from \"./utils/isObject\";\n\nexport default function buildProxy<T extends object>(\n instance: T,\n callback: (payload: BuildProxyParams) => void,\n config?: BuildProxyConfig,\n tree = [] as (string | symbol)[]\n) {\n const getPath = (prop: string | symbol) => tree.concat(prop).join(\".\");\n const proxiesMemo = new WeakMap()\n\n let ocurrencyLevel = 0\n const proxy = new Proxy(instance, {\n set(target, property, value) {\n\n const path = getPath(property)\n callback({\n path,\n target,\n newValue: value,\n previousValue: Reflect.get(target, property, value),\n });\n\n return Reflect.set(target, property, value);\n },\n\n get(target, property, receiver) {\n const value = Reflect.get(target, property, receiver);\n \n if (typeof value === 'function' && isNativeObject(target)) {\n return value.bind(target);\n }\n\n if(!isObject(value) || Object.isFrozen(value)) return value\n \n const currentProxy = proxiesMemo.get(value)\n if (currentProxy) return currentProxy\n \n\n const path = getPath(property)\n\n if(config){\n if (config.level) {\n ocurrencyLevel = ocurrencyLevel || (path.match(/\\./g) || []).length + 1\n if (ocurrencyLevel === config.level) return value\n }\n\n const filter = config.filter?.({\n path,\n target,\n value\n }) ?? true\n \n if(!filter) return value;\n }\n \n const proxy = buildProxy(value, callback, config, tree.concat(property));\n proxiesMemo.set(value, proxy)\n return proxy\n },\n });\n\n if(proxy['onProxy'] && typeof proxy['onProxy'] === 'function') proxy['onProxy'](proxy);\n\n return proxy\n}","export default function isNativeObject(target: any): boolean {\n return target instanceof Map ||\n target instanceof Set ||\n target instanceof WeakMap ||\n target instanceof WeakSet ||\n target instanceof Date ||\n target instanceof RegExp ||\n target instanceof ArrayBuffer ||\n target instanceof DataView ||\n ArrayBuffer.isView(target) || // Detecta arrays tipados\n target instanceof Promise ||\n target instanceof Error;\n}\n","import { useReducer, useRef } from \"react\";\nimport buildProxy from \"./buildProxy\";\nimport { UseReactiveObjectConfig } from \"./types\";\n\nfunction useReactiveObject<T extends object>(instance: () => T, config?: UseReactiveObjectConfig): T;\nfunction useReactiveObject<T extends object>(instance: T, config?: UseReactiveObjectConfig): T;\nfunction useReactiveObject<T extends object>(instance: T | (() => T), config?: UseReactiveObjectConfig): T {\n const [, reRender] = useReducer(() => Math.random(), 0);\n\n const referencesObject = useRef<Map<string, any>>()\n\n const proxy = useRef<T>()\n if (!proxy.current) {\n\n referencesObject.current = new Map()\n const instanceExec = typeof instance === 'function' ? instance() : instance\n\n proxy.current = buildProxy(instanceExec, (payload) => {\n\n if(referencesObject.current!.get(payload.path) === payload.newValue) return;\n\n if (config?.intercept) {\n config.intercept(() => reRender(), payload)\n } else {\n reRender()\n }\n \n referencesObject.current!.set(payload.path, payload.newValue)\n\n }, config)\n }\n\n return proxy.current\n}\n\nexport default useReactiveObject","export default abstract class ReactiveObject<T> {\n protected onProxy(instance: T) {\n let proto = Object.getPrototypeOf(this);\n while (proto && proto !== Object.prototype) {\n for (const property of Object.getOwnPropertyNames(proto)) {\n\n const descriptor = Object.getOwnPropertyDescriptor(proto, property);\n\n if (\n descriptor &&\n typeof descriptor.value === 'function' &&\n property !== 'constructor'\n ) {\n this[property] = this[property].bind(instance);\n }\n }\n\n proto = Object.getPrototypeOf(proto); // Move para o próximo protótipo na cadeia\n }\n }\n}"],"names":["isObject","value","isNullOrUndefined","isObjectType","Date","isDateObject","buildProxy","instance","callback","config","tree","getPath","prop","concat","join","proxiesMemo","WeakMap","ocurrencyLevel","proxy","Proxy","set","target","property","path","newValue","previousValue","Reflect","get","receiver","Map","Set","WeakSet","RegExp","ArrayBuffer","DataView","isView","Promise","Error","isNativeObject","bind","Object","isFrozen","currentProxy","level","match","length","_b","filter","_a","call","useReactiveObject","reRender","useReducer","Math","random","referencesObject","useRef","current","instanceExec","payload","intercept","ReactiveObject","onProxy","proto","getPrototypeOf","this","prototype","getOwnPropertyNames","descriptor","getOwnPropertyDescriptor"],"mappings":"+CAAA,MAOMA,EAA8BC,IALV,CAACA,GAAuD,MAATA,EAMtEC,CAAkBD,IAJA,CAACA,GACH,iBAAVA,EAIPE,CAAaF,KATM,CAACA,GAAkCA,aAAiBG,KAUtEC,CAAaJ,GCNQ,SAAAK,EACpBC,EACAC,EACAC,EACAC,EAAO,IAEP,MAAMC,EAAWC,GAA0BF,EAAKG,OAAOD,GAAME,KAAK,KAC5DC,EAAc,IAAIC,QAExB,IAAIC,EAAiB,EACrB,MAAMC,EAAQ,IAAIC,MAAMZ,EAAU,CAC9B,GAAAa,CAAIC,EAAQC,EAAUrB,GAElB,MAAMsB,EAAOZ,EAAQW,GAQrB,OAPAd,EAAS,CACLe,OACAF,SACAG,SAAUvB,EACVwB,cAAeC,QAAQC,IAAIN,EAAQC,EAAUrB,KAG1CyB,QAAQN,IAAIC,EAAQC,EAAUrB,EACxC,EAED,GAAA0B,CAAIN,EAAQC,EAAUM,WAClB,MAAM3B,EAAQyB,QAAQC,IAAIN,EAAQC,EAAUM,GAE5C,GAAqB,mBAAV3B,GC/BC,SAAeoB,GACnC,OAAOA,aAAkBQ,KAClBR,aAAkBS,KAClBT,aAAkBL,SAClBK,aAAkBU,SAClBV,aAAkBjB,MAClBiB,aAAkBW,QAClBX,aAAkBY,aAClBZ,aAAkBa,UAClBD,YAAYE,OAAOd,IACnBA,aAAkBe,SAClBf,aAAkBgB,KAC7B,CDmB+CC,CAAejB,GAC9C,OAAOpB,EAAMsC,KAAKlB,GAGtB,IAAIrB,EAASC,IAAUuC,OAAOC,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