Skip to content

Commit

Permalink
rf
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishiv committed Oct 16, 2024
1 parent 8f87cfe commit 13581c4
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "alfama",
"version": "1.3.7",
"version": "1.3.8",
"author": "Abhishiv Saxena<abhishiv@gmail.com>",
"license": "MIT",
"description": "Fine-grained reactive library with no compiler, no magic, and no virtual DOM",
Expand Down
4 changes: 2 additions & 2 deletions src/core/state/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const createSignal = <T = any>(val: T): Signal<T> => {
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 {
Expand Down Expand Up @@ -45,7 +45,7 @@ export const createComputedSignal = <T = any>(wire: Wire<T>) => {
const value = wire.run();
const signal = createSignal<T>(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;
Expand Down
26 changes: 13 additions & 13 deletions src/core/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ export const createStoreManager = <T>(
): StoreManager<T> => {
const manager: StoreManager<T> = {
id: "store|" + id,
value: observedObject,
wires: new Set<Wire>(),
v: observedObject,
w: new Set<Wire>(),
type: Constants.STORE,
tasks: new Set(),
unsubscribe: () => {
unsub: () => {
onChange.unsubscribe(observedObject);
},
get: (cursor: StoreCursor, token: SubToken) =>
Expand All @@ -41,19 +41,19 @@ const createStoreSubscription = <T>(
): 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<string>();
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;
}
};
Expand Down Expand Up @@ -90,8 +90,8 @@ const findMatchingWires = (
): Set<Wire> => {
const matchingWires = new Set<Wire>();

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) {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/core/state/storeAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export const reify = <T = unknown>(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;
Expand Down
18 changes: 9 additions & 9 deletions src/core/state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ export type SignalAPI<T = any> = [SignalGetter<T>, SignalSetter<T>];
export type Signal<T = unknown> = SignalAPI & {
id: string;
/** Wires subscribed to this signal */
wires: Set<Wire<any>>;
w: Set<Wire<any>>;
/** To check "if x is a signal" */
type: typeof Constants.SIGNAL;

value: T;
v: T;
get: SignalGetter<T>;
set: SignalSetter<T>;
};
Expand Down Expand Up @@ -53,17 +53,17 @@ type extractGeneric<Type> = Type extends ObjPathProxy<unknown, infer X>

export type StoreManager<T = unknown> = {
id: string;
value: T;
v: T;
rootCursor?: StoreCursor;
/** Wires subscribed to this signal */
wires: Set<Wire<any>>;
w: Set<Wire<any>>;
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;
Expand All @@ -84,15 +84,15 @@ export type Wire<T = unknown> = {
fn: WireFunction<T> | 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<Signal>;
storesRS: Map<StoreManager, Set<string>>;
sigs: Set<Signal>;
stores: Map<StoreManager, Set<string>>;

// Post-run tasks
tasks: Set<(nextValue: T) => void>;
Expand Down
38 changes: 19 additions & 19 deletions src/core/state/wire.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<StoreManager>(cursor);
const v = manager.get(cursor, token);
wire.value = v;
wire.v = v;
return v;
}
};
Expand All @@ -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<any>): 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<StoreManager>(store);
if (manager) {
manager.wires.delete(wire);
manager.w.delete(wire);
}
});
_initWire(wire);
Expand All @@ -125,8 +125,8 @@ const _initWire = (wire: Wire<any>): 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
Expand Down Expand Up @@ -160,13 +160,13 @@ export const runWires = (wires: Set<Wire<any>>): 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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/dom/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ export function unrender(arg: RenderContext | TreeStep[]) {
Object.values(step.state.stores).forEach((s) => {
const manager = getCursorProxyMeta<StoreManager>(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) {
Expand Down
4 changes: 2 additions & 2 deletions src/stdlib/Each/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const Each: <T extends ArrayOrObject>(
// 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);
Expand All @@ -62,7 +62,7 @@ export const Each: <T extends ArrayOrObject>(
const getItemCursor = (item: ExtractElement<typeof listCursor>) => {
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);
Expand Down

0 comments on commit 13581c4

Please sign in to comment.