From 7d7e4c736c32a0caa7fe4212a4492948f8c99567 Mon Sep 17 00:00:00 2001 From: Luis Herranz Date: Sun, 14 Jan 2024 21:03:46 +0100 Subject: [PATCH] Replaces strings with variables --- packages/deepsignal/core/src/index.ts | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/packages/deepsignal/core/src/index.ts b/packages/deepsignal/core/src/index.ts index 38bd66d..c62f046 100644 --- a/packages/deepsignal/core/src/index.ts +++ b/packages/deepsignal/core/src/index.ts @@ -7,6 +7,9 @@ const ignore = new WeakSet(); const objToIterable = new WeakMap(); const rg = /^\$/; const descriptor = Object.getOwnPropertyDescriptor; +const strFunction: "function" = "function"; +const str$: "$" = "$"; +const strLength: "length" = "length"; let peeking = false; export const deepSignal = (obj: T): DeepSignal => { @@ -58,21 +61,21 @@ const get = (isArrayOfSignals: boolean) => (target: object, fullKey: string, receiver: object): unknown => { if (peeking) return Reflect.get(target, fullKey, receiver); - let returnSignal = isArrayOfSignals || fullKey[0] === "$"; + let returnSignal = isArrayOfSignals || fullKey[0] === str$; if (!isArrayOfSignals && returnSignal && Array.isArray(target)) { - if (fullKey === "$") { + if (fullKey === str$) { if (!arrayToArrayOfSignals.has(target)) arrayToArrayOfSignals.set(target, createProxy(target, arrayHandlers)); return arrayToArrayOfSignals.get(target); } - returnSignal = fullKey === "$length"; + returnSignal = fullKey === str$ + strLength; } if (!proxyToSignals.has(receiver)) proxyToSignals.set(receiver, new Map()); const signals = proxyToSignals.get(receiver); const key = returnSignal ? fullKey.replace(rg, "") : fullKey; if ( !signals.has(key) && - typeof descriptor(target, key)?.get === "function" + typeof descriptor(target, key)?.get === strFunction ) { signals.set( key, @@ -80,7 +83,7 @@ const get = ); } else { let value = Reflect.get(target, key, receiver); - if (returnSignal && typeof value === "function") return; + if (returnSignal && typeof value === strFunction) return; if (typeof key === "symbol" && wellKnownSymbols.has(key)) return value; if (!signals.has(key)) { if (shouldProxy(value)) { @@ -97,11 +100,11 @@ const get = const objectHandlers = { get: get(false), set(target: object, fullKey: string, val: any, receiver: object): boolean { - if (typeof descriptor(target, fullKey)?.set === "function") + if (typeof descriptor(target, fullKey)?.set === strFunction) return Reflect.set(target, fullKey, val, receiver); if (!proxyToSignals.has(receiver)) proxyToSignals.set(receiver, new Map()); const signals = proxyToSignals.get(receiver); - if (fullKey[0] === "$") { + if (fullKey[0] === str$) { if (!(val instanceof Signal)) throwOnMutation(); const key = fullKey.replace(rg, ""); signals.set(key, val); @@ -124,7 +127,7 @@ const objectHandlers = { } }, deleteProperty(target: object, key: string): boolean { - if (key[0] === "$") throwOnMutation(); + if (key[0] === str$) throwOnMutation(); const signals = proxyToSignals.get(objToProxy.get(target)); const result = Reflect.deleteProperty(target, key); if (signals && signals.has(key)) signals.get(key).value = undefined;