Skip to content

Commit

Permalink
Replaces strings with variables
Browse files Browse the repository at this point in the history
  • Loading branch information
luisherranz committed Jan 14, 2024
1 parent 7441d20 commit 7d7e4c7
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions packages/deepsignal/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = <T extends object>(obj: T): DeepSignal<T> => {
Expand Down Expand Up @@ -58,29 +61,29 @@ 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,
computed(() => Reflect.get(target, key, receiver))
);
} 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)) {
Expand All @@ -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);
Expand All @@ -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;
Expand Down

0 comments on commit 7d7e4c7

Please sign in to comment.