Skip to content

Commit 7875166

Browse files
committed
Remove NODE_SUBSCRIBED flag from Nodes
The flag can be replaced in ._subscribe with an alternative check so that the method stays idempotent, and ._unsubscribe should be idempotent without any additional checks.
1 parent a42a2bd commit 7875166

File tree

1 file changed

+13
-21
lines changed

1 file changed

+13
-21
lines changed

packages/core/src/index.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,12 @@ const TRACKING = 1 << 5;
1212

1313
// Flags for Nodes.
1414
const NODE_FREE = 1 << 0;
15-
const NODE_SUBSCRIBED = 1 << 1;
1615

1716
// A linked list node used to track dependencies (sources) and dependents (targets).
1817
// Also used to remember the source's last version number that the target saw.
1918
type Node = {
2019
// A node may have the following flags:
2120
// NODE_FREE when it's unclear whether the source is still a dependency of the target
22-
// NODE_SUBSCRIBED when the target has subscribed to listen change notifications from the source
2321
_flags: number;
2422

2523
// A source whose value the target depends on.
@@ -219,10 +217,8 @@ Signal.prototype._refresh = function () {
219217
};
220218

221219
Signal.prototype._subscribe = function (node) {
222-
if (!(node._flags & NODE_SUBSCRIBED)) {
223-
node._flags |= NODE_SUBSCRIBED;
220+
if (this._targets !== node && node._prevTarget === undefined) {
224221
node._nextTarget = this._targets;
225-
226222
if (this._targets !== undefined) {
227223
this._targets._prevTarget = node;
228224
}
@@ -231,22 +227,18 @@ Signal.prototype._subscribe = function (node) {
231227
};
232228

233229
Signal.prototype._unsubscribe = function (node) {
234-
if (node._flags & NODE_SUBSCRIBED) {
235-
node._flags &= ~NODE_SUBSCRIBED;
236-
237-
const prev = node._prevTarget;
238-
const next = node._nextTarget;
239-
if (prev !== undefined) {
240-
prev._nextTarget = next;
241-
node._prevTarget = undefined;
242-
}
243-
if (next !== undefined) {
244-
next._prevTarget = prev;
245-
node._nextTarget = undefined;
246-
}
247-
if (node === this._targets) {
248-
this._targets = next;
249-
}
230+
const prev = node._prevTarget;
231+
const next = node._nextTarget;
232+
if (prev !== undefined) {
233+
prev._nextTarget = next;
234+
node._prevTarget = undefined;
235+
}
236+
if (next !== undefined) {
237+
next._prevTarget = prev;
238+
node._nextTarget = undefined;
239+
}
240+
if (node === this._targets) {
241+
this._targets = next;
250242
}
251243
};
252244

0 commit comments

Comments
 (0)