From c9947e328019f3d891441edb7b511f295220fe84 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Fri, 3 Jan 2025 21:41:50 +0100 Subject: [PATCH] dev: Add afterRegisterGraph hook replacing afterCreateSignal Deprecate afterCreateSignal for now --- packages/solid/src/reactive/signal.ts | 15 ++++++++++----- packages/solid/test/dev.spec.ts | 20 ++++++++++++++------ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/packages/solid/src/reactive/signal.ts b/packages/solid/src/reactive/signal.ts index fd1ab94d..c5751f4f 100644 --- a/packages/solid/src/reactive/signal.ts +++ b/packages/solid/src/reactive/signal.ts @@ -61,11 +61,14 @@ let ExecCount = 0; export const DevHooks: { afterUpdate: (() => void) | null; afterCreateOwner: ((owner: Owner) => void) | null; + /** @deprecated use `afterRegisterGraph` */ afterCreateSignal: ((signal: SignalState) => void) | null; + afterRegisterGraph: ((sourceMapValue: SourceMapValue) => void) | null; } = { afterUpdate: null, afterCreateOwner: null, - afterCreateSignal: null + afterCreateSignal: null, + afterRegisterGraph: null, }; export type ComputationState = 0 | 1 | 2; @@ -1131,10 +1134,12 @@ export function devComponent(Comp: (props: P) => V, props: P): V { } export function registerGraph(value: SourceMapValue): void { - if (!Owner) return; - if (Owner.sourceMap) Owner.sourceMap.push(value); - else Owner.sourceMap = [value]; - value.graph = Owner; + if (Owner) { + if (Owner.sourceMap) Owner.sourceMap.push(value); + else Owner.sourceMap = [value]; + value.graph = Owner; + } + if (DevHooks.afterRegisterGraph) DevHooks.afterRegisterGraph(value) } export type ContextProviderComponent = FlowComponent<{ value: T }>; diff --git a/packages/solid/test/dev.spec.ts b/packages/solid/test/dev.spec.ts index a6c72fc9..66e752c3 100644 --- a/packages/solid/test/dev.spec.ts +++ b/packages/solid/test/dev.spec.ts @@ -130,23 +130,31 @@ describe("Dev features", () => { }); }); - test("afterCreateSignal Hook", () => { + test("afterRegisterGraph Hook", () => { createRoot(() => { const owner = getOwner()!; const cb = vi.fn(); - DEV!.hooks.afterCreateSignal = cb; + DEV!.hooks.afterRegisterGraph = cb; - createSignal(3, { name: "test" }); + createSignal(1); expect(cb).toHaveBeenCalledTimes(1); expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![0]); + expect(owner.sourceMap).toHaveLength(1); - createSignal(5); + createSignal(2, { internal: true }); + expect(cb).toHaveBeenCalledTimes(1); + expect(owner.sourceMap).toHaveLength(1); + + createStore({}); expect(cb).toHaveBeenCalledTimes(2); expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![1]); + expect(owner.sourceMap).toHaveLength(2); - createSignal(6, { name: "explicit" }); + const customValue = {value: 3}; + DEV!.registerGraph(customValue); expect(cb).toHaveBeenCalledTimes(3); - expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![2]); + expect(cb).toHaveBeenLastCalledWith(customValue); + expect(owner.sourceMap).toHaveLength(3); }); });