Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
"cicheck": "pnpm -s test && pnpm -s typecheck && pnpm -s build && pnpm -s smoke && pnpm -s format"
},
"dependencies": {
"alien-signals": "^3.1.1"
"alien-signals": "^3.2.1"
},
"devDependencies": {
"@biomejs/biome": "1.9.4",
Expand Down
70 changes: 70 additions & 0 deletions packages/core/__tests__/reactivity.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { describe, expect, it } from "vitest";

import { computed } from "../computed";
import { effect } from "../reactivity";
import { signal } from "../signal";
import { watchEffect } from "../watchEffect";

describe("reactivity runtime", () => {
it("continues propagation after inner writes through a computed chain", () => {
const source = signal(0);
const first = computed(() => source.value);
const second = computed(() => first.value);
let runs = 0;

const stop = watchEffect(
() => {
runs += 1;
if (second.value > 0) {
source.value = 0;
}
},
{ flush: "sync" },
);

expect(runs).toBe(1);

source.value = 1;
expect(source.value).toBe(0);
expect(runs).toBe(2);

source.value = 2;
expect(source.value).toBe(0);
expect(runs).toBe(3);

source.value = 3;
expect(source.value).toBe(0);
expect(runs).toBe(4);

stop();
});

it("does not throw when a computed update disposes its subscriber", () => {
const shouldDispose = signal(false);
const subscriber = {
current: undefined as ReturnType<typeof effect> | undefined,
};

const indirectlyDisposes = computed(() => {
if (shouldDispose.value) {
subscriber.current?.stop();
}
return 0;
});

const selfDisposing = computed(() => {
indirectlyDisposes.value;
return 0;
});

subscriber.current = effect(() => {
selfDisposing.value;
});

expect(() => {
shouldDispose.value = true;
}).not.toThrow();

subscriber.current.stop();
});
});
76 changes: 59 additions & 17 deletions packages/core/reactivity.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import {
type Link,
ReactiveFlags,
type ReactiveNode,
createReactiveSystem,
} from "alien-signals/system";
import { ReactiveFlags, createReactiveSystem } from "alien-signals/system";

import type { Computed } from "./computed";
import type { Signal } from "./signal";
import type { SignalNode } from "./signal";

export { ReactiveFlags };
export type { Link, ReactiveNode };

export interface ReactiveNode {
deps?: Link;
depsTail?: Link;
subs?: Link;
subsTail?: Link;
flags: ReactiveFlags;
}

export interface Link {
version: number;
dep: ReactiveNode;
sub: ReactiveNode;
prevSub: Link | undefined;
nextSub: Link | undefined;
prevDep: Link | undefined;
nextDep: Link | undefined;
}

export enum SignalFlags {
IS_SIGNAL = "__v_isSignal",
Expand Down Expand Up @@ -232,21 +244,37 @@ export function trigger(
triggerEffects(depsToTrigger);
}

const { link, unlink, propagate, checkDirty, shallowPropagate } =
createReactiveSystem({
update(node: ReactiveNode & { update(): boolean }) {
return node.update();
},
notify(effect: Effect) {
queue.push(effect);
},
unwatched() {},
});
const system = createReactiveSystem({
update(node) {
return (node as ReactiveNode & { update(): boolean }).update();
},
notify(effect) {
queue.push(effect as Effect);
},
unwatched() {},
});

const link = system.link as (
dep: ReactiveNode,
sub: ReactiveNode,
version: number,
) => void;
const unlink = system.unlink as (
link: Link,
sub?: ReactiveNode,
) => Link | undefined;
const propagate = system.propagate as (link: Link, innerWrite: boolean) => void;
const checkDirty = system.checkDirty as (
link: Link,
sub: ReactiveNode,
) => boolean;
const shallowPropagate = system.shallowPropagate as (link: Link) => void;

export { link, unlink, propagate, shallowPropagate };

let cycle = 0;
let batchDepth = 0;
let reactiveExecutionDepth = 0;
let activeSub: ReactiveNode | undefined;

const queue: Effect[] = [];
Expand Down Expand Up @@ -287,6 +315,18 @@ export function setActiveSubscriber(node: ReactiveNode | undefined): void {
activeSub = node;
}

export function enterReactiveExecution(): void {
reactiveExecutionDepth += 1;
}

export function exitReactiveExecution(): void {
reactiveExecutionDepth -= 1;
}

export function isRunningReactiveExecution(): boolean {
return reactiveExecutionDepth > 0;
}

export function startBatch(): void {
batchDepth += 1;
}
Expand Down Expand Up @@ -354,9 +394,11 @@ export class Effect<T = unknown> implements ReactiveNode {
this.flags = ReactiveFlags.Watching | ReactiveFlags.RecursedCheck;
const previous = getActiveSubscriber();
setActiveSubscriber(this);
enterReactiveExecution();
try {
return this.fn();
} finally {
exitReactiveExecution();
setActiveSubscriber(previous);
this.flags &= ~ReactiveFlags.RecursedCheck;
let toRemove =
Expand Down
3 changes: 2 additions & 1 deletion packages/core/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getCurrentCycle,
hasChanged,
isBatching,
isRunningReactiveExecution,
link,
propagate,
setDepFactory,
Expand Down Expand Up @@ -50,7 +51,7 @@ export class SignalNode<T = unknown> implements ReactiveNode {
this.flags = ReactiveFlags.Mutable | ReactiveFlags.Dirty;
const subs = this.subs;
if (subs !== undefined) {
propagate(subs);
propagate(subs, isRunningReactiveExecution());
if (!isBatching()) {
flushSchedulerQueue();
}
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.