Skip to content

Commit

Permalink
Merge pull request #523 from jviide/sub
Browse files Browse the repository at this point in the history
refactor: use untracked to implement Signal.prototype.subscribe
  • Loading branch information
marvinhagemeister committed Mar 13, 2024
2 parents d7f2afa + 3e58027 commit e7b9ccc
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions packages/core/src/index.ts
Expand Up @@ -288,16 +288,9 @@ Signal.prototype._unsubscribe = function (node) {
};

Signal.prototype.subscribe = function (fn) {
const signal = this;
return effect(function (this: Effect) {
const value = signal.value;
const flag = this._flags & TRACKING;
this._flags &= ~TRACKING;
try {
fn(value);
} finally {
this._flags |= flag;
}
return effect(() => {
const value = this.value;
untracked(() => fn(value));
});
};

Expand Down Expand Up @@ -550,7 +543,7 @@ Computed.prototype._subscribe = function (node) {
if (this._targets === undefined) {
this._flags |= OUTDATED | TRACKING;

// A computed signal subscribes lazily to its dependencies when the it
// A computed signal subscribes lazily to its dependencies when it
// gets its first subscriber.
for (
let node = this._sources;
Expand Down Expand Up @@ -768,12 +761,4 @@ function effect(compute: () => unknown | EffectCleanup): () => void {
return effect._dispose.bind(effect);
}

export {
signal,
computed,
effect,
batch,
Signal,
ReadonlySignal,
untracked,
};
export { signal, computed, effect, batch, Signal, ReadonlySignal, untracked };

0 comments on commit e7b9ccc

Please sign in to comment.