Skip to content

Commit f887792

Browse files
committed
refactor(core): Add warning when signal equality is false for object.is (#52532)
This commit adds a warning when a signal equality function returns `false` but `Object.is` returns `true`. PR Close #52532
1 parent d01f371 commit f887792

File tree

1 file changed

+11
-3
lines changed
  • packages/core/primitives/signals/src

1 file changed

+11
-3
lines changed

packages/core/primitives/signals/src/signal.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {defaultEquals, ValueEqualityFn} from './equality';
1010
import {throwInvalidWriteToSignalError} from './errors';
1111
import {producerAccessed, producerIncrementEpoch, producerNotifyConsumers, producerUpdatesAllowed, REACTIVE_NODE, ReactiveNode, SIGNAL} from './graph';
1212

13+
// Required as the signals library is in a separate package, so we need to explicitly ensure the
14+
// global `ngDevMode` type is defined.
15+
declare const ngDevMode: boolean|undefined;
16+
1317
/**
1418
* If set, called after `WritableSignal`s are updated.
1519
*
@@ -63,9 +67,13 @@ export function signalSetFn<T>(node: SignalNode<T>, newValue: T) {
6367
}
6468

6569
const value = node.value;
66-
// assuming that signal value equality implementations should always return true for values that
67-
// are the same according to Object.is
68-
if (!Object.is(value, newValue) && !node.equal(value, newValue)) {
70+
if (Object.is(value, newValue)) {
71+
if (typeof ngDevMode !== 'undefined' && ngDevMode && !node.equal(value, newValue)) {
72+
console.warn(
73+
'Signal value equality implementations should always return `true` for' +
74+
' values that are the same according to `Object.is` but returned `false` instead.');
75+
}
76+
} else if (!node.equal(value, newValue)) {
6977
node.value = newValue;
7078
signalValueChanged(node);
7179
}

0 commit comments

Comments
 (0)