diff --git a/src/signal.ts b/src/signal.ts index 780243ff..95c1af2a 100644 --- a/src/signal.ts +++ b/src/signal.ts @@ -7,6 +7,12 @@ function baseEquality(oldValue: T, newValue: T) { } export class Signal { +/** + @private This is available for internal "friend" APIs to use, but it is *not* + legal to use by consumers. + */ +export const Peek = Symbol('Peek'); + #value: T; protected isEqual: Equality; protected tag: Tag; @@ -27,7 +33,12 @@ export class Signal { return this.#value; } - peek() { + // Expressly *not* part of the public API: peeking a value in contexts other than internal parts + // of the reactivity system itself tends very strongly to produce bugs, because it decouples + // consumers from the root state. (It is very, very tempting to wire your own caching on with a + // "peek", rather than using caching tools composed out of the core primitives, or to "be smarter" + // than the signal system.) + [Peek](): T { return this.#value; } diff --git a/tests/signal.spec.ts b/tests/signal.spec.ts index c131c5e1..04d6dd1c 100644 --- a/tests/signal.spec.ts +++ b/tests/signal.spec.ts @@ -1,5 +1,5 @@ import { expect, describe, test, vi } from 'vitest'; -import { createSignal } from '../src/signal'; +import { createSignal, Peek } from '../src/signal'; import { createEffect } from '../src/effect'; import isEqual from 'lodash/isEqual'; @@ -50,7 +50,7 @@ describe('Signal', () => { let foo = createSignal('foo'); let spy = vi.fn(() => { - foo.peek(); + foo[Peek](); }); createEffect(spy);