Skip to content

Commit

Permalink
v1.3.12
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishiv committed Oct 16, 2024
1 parent 3a574ee commit 7f21a45
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 15 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.11",
"version": "1.3.12",
"author": "Abhishiv Saxena<[email protected]>",
"license": "MIT",
"description": "Fine-grained reactive library with no compiler, no magic, and no virtual DOM",
Expand Down
29 changes: 16 additions & 13 deletions src/core/state/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,39 @@ export {
getCursor as getProxyPath,
} from "../../utils/index";
export type { ObjPathProxy } from "../../utils/index";
import { Signal, SubToken, Wire } from "./types";
import { Signal, SignalGetter, SignalSetter, SubToken, Wire } from "./types";
import { runWires } from "./wire";

let SIGNAL_COUNTER = 0;

export const createSignal = <T = any>(val: T): Signal<T> => {
SIGNAL_COUNTER++;
function get(token?: SubToken) {
const get: SignalGetter<T> = (token?: SubToken) => {
if (token) {
// Two-way link. Signal writes will now call/update wire W
token.wire.sigs.add(sig);
sig.wires.add(token.wire);
return sig.value as T;
sig.w.add(token.wire);
return sig.v as T;
} else {
return sig.value as T;
return sig.v as T;
}
}
};
get.type = Constants.SIGNAL_GETTER;

const set = (value: T) => {
if (sig.value === value) return value;
sig.value = value;
runWires(sig.wires);
const set: SignalSetter<T> = (value: T) => {
if (sig.v === value) return value;
sig.v = value;
runWires(sig.w);
return val;
};

const sig: any = [get, set];
const sig = [get, set] as unknown as Signal<T>;
sig.id = "signal|" + SIGNAL_COUNTER;
sig.type = Constants.SIGNAL;
sig.value = val;
sig.wires = new Set<Wire>();
sig.v = val;
sig.w = new Set<Wire>();

get.sig = sig;

sig.get = get;
sig.set = set;
Expand Down
2 changes: 1 addition & 1 deletion src/core/state/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export type Wire<T = unknown> = {
run: () => T;

// Signals/Stores read-subscribed last run
sigs: Set<Signal>;
sigs: Set<Signal<any>>;
stores: Map<StoreManager, Set<string>>;

// Post-run tasks
Expand Down

0 comments on commit 7f21a45

Please sign in to comment.