Skip to content

Commit

Permalink
feat: update alien-signals to 1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
johnsoncodehk committed Jan 15, 2025
1 parent ac88d81 commit 4f38bd6
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"@preact/signals": "^2.0.0",
"@reactively/core": "^0.0.8",
"@vue/reactivity": "^3.5.13",
"alien-signals": "1.0.0-alpha.3",
"alien-signals": "1.0.0",
"compostate": "0.6.0-alpha.1",
"kairo": "0.6.0-rc.0",
"mobx": "^6.13.5",
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.

4 changes: 1 addition & 3 deletions src/frameworks/alienSignals.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import { getDefaultSystem } from "alien-signals/esm";
import { signal, computed, effect, startBatch, endBatch } from "alien-signals/esm";
import { ReactiveFramework } from "../util/reactiveFramework";

const { signal, computed, effect, startBatch, endBatch } = getDefaultSystem();

export const alienFramework: ReactiveFramework = {
name: "alien-signals",
signal: (initial) => {
Expand Down
76 changes: 35 additions & 41 deletions src/frameworks/tc39-proposal-signals-alien-polyfill.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,25 @@ import { ReactiveFramework } from "../util/reactiveFramework";
export namespace Signal {
const WATCHER_PLACEHOLDER = Symbol('watcher') as any;

const {endTrack, link, propagate, startTrack, processQueuedEffects, processComputedUpdate} =
alien.createSystem({
computed: {
is(sub): sub is Computed {
return sub instanceof Computed;
},
update(computed) {
return computed.update();
},
},
effect: {
is(sub): sub is subtle.Watcher {
return sub instanceof subtle.Watcher;
},
notify(watcher: subtle.Watcher) {
if (watcher.flags & alien.SubscriberFlags.Dirty) {
watcher.run();
return true;
}
return false;
},
},
});
const {
endTracking,
link,
propagate,
startTracking,
processComputedUpdate,
processEffectNotifications,
} = alien.createReactiveSystem({
updateComputed(computed: Computed) {
return computed.update();
},
notifyEffect(watcher: subtle.Watcher) {
if (watcher.flags & alien.SubscriberFlags.Dirty) {
watcher.run();
return true;
}
return false;
},
});

let activeSub: alien.Subscriber | undefined;

Expand Down Expand Up @@ -76,7 +72,7 @@ export namespace Signal {
}
if (activeSub !== undefined) {
if (link(this, activeSub)) {
const newSub = this.subsTail!;
const newSub = this.subsTail!.sub;
if (newSub instanceof Computed && newSub.watchCount) {
this.onWatched();
}
Expand All @@ -94,7 +90,7 @@ export namespace Signal {
const subs = this.subs;
if (subs !== undefined) {
propagate(subs);
processQueuedEffects();
processEffectNotifications();
}
}
}
Expand All @@ -105,7 +101,7 @@ export namespace Signal {
subsTail: alien.Link | undefined = undefined;
deps: alien.Link | undefined = undefined;
depsTail: alien.Link | undefined = undefined;
flags = alien.SubscriberFlags.Dirty;
flags = alien.SubscriberFlags.Computed | alien.SubscriberFlags.Dirty;
isError = true;
watchCount = 0;
currentValue: T | undefined = undefined;
Expand Down Expand Up @@ -144,22 +140,20 @@ export namespace Signal {
}

get() {
if (this.flags & alien.SubscriberFlags.Tracking) {
throw new Error('Cycles detected');
}
if (activeSub === WATCHER_PLACEHOLDER) {
throw new Error('Cannot read from computed inside watcher');
}
const flags = this.flags;
if (flags) {
if (flags & alien.SubscriberFlags.Tracking) {
throw new Error('Cycles detected');
}
if (flags & (alien.SubscriberFlags.Dirty | alien.SubscriberFlags.PendingComputed)) {
processComputedUpdate(this, flags);
}
if (activeSub !== undefined) {
if (link(this, activeSub)) {
const newSub = this.subsTail!;
if (newSub instanceof Computed && newSub.watchCount) {
this.onWatched();
}
const newSub = link(this, activeSub)?.sub;
if (newSub instanceof Computed && newSub.watchCount) {
this.onWatched();
}
}
if (this.isError) {
Expand All @@ -171,7 +165,7 @@ export namespace Signal {
update(): boolean {
const prevSub = activeSub;
activeSub = this;
startTrack(this);
startTracking(this);
const oldValue = this.currentValue;
try {
const newValue = this.getter();
Expand Down Expand Up @@ -200,7 +194,7 @@ export namespace Signal {
}
}
activeSub = prevSub;
endTrack(this);
endTracking(this);
}
}
}
Expand All @@ -211,7 +205,7 @@ export namespace Signal {
export class Watcher implements alien.Subscriber {
deps: alien.Link | undefined = undefined;
depsTail: alien.Link | undefined = undefined;
flags = alien.SubscriberFlags.None;
flags = alien.SubscriberFlags.Effect;
watchList = new Set<AnySignal>();

constructor(private fn: () => void) {}
Expand Down Expand Up @@ -245,21 +239,21 @@ export namespace Signal {
this.watchList.delete(signal);
signal.onUnwatched();
}
startTrack(this);
startTracking(this);
for (let _link = this.deps; _link !== undefined; _link = _link.nextDep) {
const dep = _link.dep as AnySignal;
if (this.watchList.has(dep)) {
link(dep, this);
}
}
endTrack(this);
endTracking(this);
}

getPending() {
return introspectSources(this).filter(
(source) =>
source instanceof Computed &&
source.flags & (alien.SubscriberFlags.CheckRequired | alien.SubscriberFlags.Dirty),
source.flags & (alien.SubscriberFlags.PendingComputed | alien.SubscriberFlags.Dirty),
);
}
}
Expand Down

0 comments on commit 4f38bd6

Please sign in to comment.