From 13581c4d12df98b84e154eaed146c913d26b2ecc Mon Sep 17 00:00:00 2001 From: Abhishiv Saxena Date: Wed, 16 Oct 2024 11:01:02 +0530 Subject: [PATCH] rf --- package.json | 2 +- src/core/state/signal.ts | 4 ++-- src/core/state/store.ts | 26 +++++++++++++------------- src/core/state/storeAPI.ts | 2 +- src/core/state/types.ts | 18 +++++++++--------- src/core/state/wire.ts | 38 +++++++++++++++++++------------------- src/dom/index.ts | 4 ++-- src/stdlib/Each/index.tsx | 4 ++-- 8 files changed, 49 insertions(+), 49 deletions(-) diff --git a/package.json b/package.json index 9b5b6a6..ebd72d3 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "alfama", - "version": "1.3.7", + "version": "1.3.8", "author": "Abhishiv Saxena", "license": "MIT", "description": "Fine-grained reactive library with no compiler, no magic, and no virtual DOM", diff --git a/src/core/state/signal.ts b/src/core/state/signal.ts index cdb64af..661891f 100644 --- a/src/core/state/signal.ts +++ b/src/core/state/signal.ts @@ -14,7 +14,7 @@ export const createSignal = (val: T): Signal => { function get(token?: SubToken) { if (token) { // Two-way link. Signal writes will now call/update wire W - token.wire.sigRS.add(sig); + token.wire.sigs.add(sig); sig.wires.add(token.wire); return sig.value as T; } else { @@ -45,7 +45,7 @@ export const createComputedSignal = (wire: Wire) => { const value = wire.run(); const signal = createSignal(value); const handler = () => { - if (signal.get() !== wire.value) signal.set(wire.value as T); + if (signal.get() !== wire.v) signal.set(wire.v as T); }; wire.tasks.add(handler); return signal; diff --git a/src/core/state/store.ts b/src/core/state/store.ts index 5db64d8..a41876d 100644 --- a/src/core/state/store.ts +++ b/src/core/state/store.ts @@ -20,11 +20,11 @@ export const createStoreManager = ( ): StoreManager => { const manager: StoreManager = { id: "store|" + id, - value: observedObject, - wires: new Set(), + v: observedObject, + w: new Set(), type: Constants.STORE, tasks: new Set(), - unsubscribe: () => { + unsub: () => { onChange.unsubscribe(observedObject); }, get: (cursor: StoreCursor, token: SubToken) => @@ -41,19 +41,19 @@ const createStoreSubscription = ( ): any => { const cursorPath = getCursor(cursor); const encodedCursor = encodeCursor(cursorPath); - manager.wires.add(wire); - if (wire.storesRS.has(manager)) { - wire.storesRS.get(manager)?.add(encodedCursor); + manager.w.add(wire); + if (wire.stores.has(manager)) { + wire.stores.get(manager)?.add(encodedCursor); } else { const set = new Set(); set.add(encodedCursor); - wire.storesRS.set(manager, set); + wire.stores.set(manager, set); } try { - const v = getValueUsingPath(manager.value as any, cursorPath); + const v = getValueUsingPath(manager.v as any, cursorPath); return v; } catch (e) { - console.log(wire, wire.storesRS, encodedCursor, manager.value); + console.log(wire, wire.stores, encodedCursor, manager.v); throw e; } }; @@ -90,8 +90,8 @@ const findMatchingWires = ( ): Set => { const matchingWires = new Set(); - manager.wires.forEach((wire) => { - const cursors = wire.storesRS.get(manager); + manager.w.forEach((wire) => { + const cursors = wire.stores.get(manager); if (!cursors) return; for (const cursorStr of cursors) { @@ -169,8 +169,8 @@ function adjustCursorForArrayChange( // console.log("adjustCursorForArrayChange", { start, deleteCount }); - manager.wires.forEach((wire) => { - wire.storesRS.forEach((cursorSet) => { + manager.w.forEach((wire) => { + wire.stores.forEach((cursorSet) => { const { rm: toRemove, add: toAdd } = adjustCursorsInSet( cursorSet, changePath, diff --git a/src/core/state/storeAPI.ts b/src/core/state/storeAPI.ts index 258cda5..0cc57d6 100644 --- a/src/core/state/storeAPI.ts +++ b/src/core/state/storeAPI.ts @@ -43,7 +43,7 @@ export const reify = (cursor: T): T => { ); if (manager) { const cursorPath = getCursor(s); - const v = getValueUsingPath(manager.value as any, cursorPath); + const v = getValueUsingPath(manager.v as any, cursorPath); return v as T; } else { return cursor; diff --git a/src/core/state/types.ts b/src/core/state/types.ts index deb4d49..6ce03ce 100644 --- a/src/core/state/types.ts +++ b/src/core/state/types.ts @@ -19,11 +19,11 @@ export type SignalAPI = [SignalGetter, SignalSetter]; export type Signal = SignalAPI & { id: string; /** Wires subscribed to this signal */ - wires: Set>; + w: Set>; /** To check "if x is a signal" */ type: typeof Constants.SIGNAL; - value: T; + v: T; get: SignalGetter; set: SignalSetter; }; @@ -53,17 +53,17 @@ type extractGeneric = Type extends ObjPathProxy export type StoreManager = { id: string; - value: T; + v: T; rootCursor?: StoreCursor; /** Wires subscribed to this signal */ - wires: Set>; + w: Set>; type: typeof Constants.STORE; tasks: Set<{ path: string[]; observor: (change: StoreChange) => void; }>; get: (cursor: StoreCursor, token: SubToken) => any; - unsubscribe: Function; + unsub: Function; }; export type StoreChangeData = ApplyData; @@ -84,15 +84,15 @@ export type Wire = { fn: WireFunction | StoreCursor; // FSM state 3-bit bitmask: [RUNNING][SKIP_RUN_QUEUE][NEEDS_RUN] state: WireState; - runCount: number; - value?: T; + r: number; + v?: T; // Run the wire run: () => T; // Signals/Stores read-subscribed last run - sigRS: Set; - storesRS: Map>; + sigs: Set; + stores: Map>; // Post-run tasks tasks: Set<(nextValue: T) => void>; diff --git a/src/core/state/wire.ts b/src/core/state/wire.ts index a26c275..abed9c3 100644 --- a/src/core/state/wire.ts +++ b/src/core/state/wire.ts @@ -29,13 +29,13 @@ export const createWire: WireFactory = (arg: WireFunction): Wire => { id: "wire|" + WIRE_COUNTER, type: Constants.WIRE, fn: arg, - sigRS: new Set(), - storesRS: new Map(), + sigs: new Set(), + stores: new Map(), tasks: new Set(), state: S_NEEDS_RUN, upper: undefined, lower: new Set(), - runCount: 0, + r: 0, run: () => { const val = runWire(arg, wire.token, wire.subWire); // Clean up unused nested wires @@ -57,18 +57,18 @@ const getSubtoken = (wire: Wire): SubToken => { const token: SubToken = (arg: Signal | StoreCursor | SignalGetter) => { if (isSignal(arg)) { const v = arg.get(token); - wire.value = v; + wire.v = v; return v; } else if (isSignalGetter(arg)) { const sig = arg.sig; const v = sig.get(token); - wire.value = v; + wire.v = v; return v; } else if (isStoreCursor(arg)) { const cursor = arg; const manager = getCursorProxyMeta(cursor); const v = manager.get(cursor, token); - wire.value = v; + wire.v = v; return v; } }; @@ -85,37 +85,37 @@ export const runWire = ( if (isStoreCursor(arg)) { const cursor = arg; const v = token(cursor); - token.wire.value = v; + token.wire.v = v; return v; } else if (isSignal(arg)) { const sig = arg; const v = token(sig); - token.wire.value = v; + token.wire.v = v; return v; } else if (isSignalGetter(arg)) { const sig = arg.sig; const v = token(sig); - token.wire.value = v; + token.wire.v = v; return v; } else { const fn = arg as WireFunction; const v = fn(token, { createWire: subWireFactory, wire: subWireFactory, - previousValue: token.wire.value, + previousValue: token.wire.v, }); - token.wire.value = v; + token.wire.v = v; return v; } }; export const wireReset = (wire: Wire): void => { wire.lower.forEach(wireReset); - wire.sigRS.forEach((signal) => signal.wires.delete(wire)); - wire.storesRS.forEach((store) => { + wire.sigs.forEach((signal) => signal.w.delete(wire)); + wire.stores.forEach((store) => { const manager = getCursorProxyMeta(store); if (manager) { - manager.wires.delete(wire); + manager.w.delete(wire); } }); _initWire(wire); @@ -125,8 +125,8 @@ const _initWire = (wire: Wire): void => { wire.state = S_NEEDS_RUN; wire.lower = new Set(); // Drop all signals now that they have been unlinked - wire.sigRS = new Set(); - wire.storesRS = new Map(); + wire.sigs = new Set(); + wire.stores = new Map(); }; // Pauses a wire so signal writes won't cause runs. Affects nested wires @@ -160,13 +160,13 @@ export const runWires = (wires: Set>): void => { while ((curr = curr.upper)) if (toRun.has(curr)) return toRun.delete(wire); }); toRun.forEach((wire) => { - const previousValue = wire.value; + const previousValue = wire.v; const val = runWire(wire.fn, wire.token, wire.subWire); - wire.runCount = wire.runCount + 1; + wire.r = wire.r + 1; if (val === previousValue) return; - wire.value = val; + wire.v = val; for (const task of wire.tasks) { task(val); } diff --git a/src/dom/index.ts b/src/dom/index.ts index 7121a2a..81a7191 100644 --- a/src/dom/index.ts +++ b/src/dom/index.ts @@ -93,13 +93,13 @@ export function unrender(arg: RenderContext | TreeStep[]) { Object.values(step.state.stores).forEach((s) => { const manager = getCursorProxyMeta(s as any); manager.tasks.clear(); - manager.wires.clear(); + manager.w.clear(); // manager.unsubscribe(); }); step.onUnmount.forEach((el) => el(step)); // step.state.stores = {}; Object.values(step.state.sigs).forEach((sig) => { - sig.wires.clear(); + sig.w.clear(); }); step.state.ctx.clear(); } else if (step.type == DOMConstants.WireTreeStep) { diff --git a/src/stdlib/Each/index.tsx b/src/stdlib/Each/index.tsx index ff232e8..df41b06 100644 --- a/src/stdlib/Each/index.tsx +++ b/src/stdlib/Each/index.tsx @@ -52,7 +52,7 @@ export const Each: ( // console.log("Each", listCursorPath); const listValue: typeof listCursor = getValueUsingPath( - store.value as any, + store.v as any, listCursorPath ) as typeof listCursor; //console.log("value", value); @@ -62,7 +62,7 @@ export const Each: ( const getItemCursor = (item: ExtractElement) => { const store: StoreManager = (listCursor as any)[META_FLAG]; const listValue: typeof listCursor = getValueUsingPath( - store.value as any, + store.v as any, listCursorPath ) as typeof listCursor; // console.log("listValue", listValue, item);