From cce159d19bfdac8ef0b61ab3364aff12fbcfc6c0 Mon Sep 17 00:00:00 2001 From: Damian Tarnawski Date: Sat, 18 Nov 2023 21:34:19 +0100 Subject: [PATCH] Add afterCreateSignal hook to DevHooks --- packages/solid/src/reactive/signal.ts | 9 ++++++--- packages/solid/test/dev.spec.ts | 20 ++++++++++++++++++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/packages/solid/src/reactive/signal.ts b/packages/solid/src/reactive/signal.ts index 67c70f265..807cd9d8e 100644 --- a/packages/solid/src/reactive/signal.ts +++ b/packages/solid/src/reactive/signal.ts @@ -57,9 +57,11 @@ let ExecCount = 0; export const DevHooks: { afterUpdate: (() => void) | null; afterCreateOwner: ((owner: Owner) => void) | null; + afterCreateSignal: ((signal: SignalState) => void) | null; } = { afterUpdate: null, - afterCreateOwner: null + afterCreateOwner: null, + afterCreateSignal: null }; // keep immediately evaluated module code, below its indirect declared let dependencies like Listener @@ -228,9 +230,10 @@ export function createSignal( comparator: options.equals || undefined }; - if ("_SOLID_DEV_" && !options.internal) { + if ("_SOLID_DEV_") { if (options.name) s.name = options.name; - registerGraph(s); + if (DevHooks.afterCreateSignal) DevHooks.afterCreateSignal(s); + if (!options.internal) registerGraph(s); } const setter: Setter = (value?: unknown) => { diff --git a/packages/solid/test/dev.spec.ts b/packages/solid/test/dev.spec.ts index 8c2b9afdc..cc9548ae1 100644 --- a/packages/solid/test/dev.spec.ts +++ b/packages/solid/test/dev.spec.ts @@ -129,6 +129,26 @@ describe("Dev features", () => { }); }); + test("afterCreateSignal Hook", () => { + createRoot(() => { + const owner = getOwner()!; + const cb = vi.fn(); + DEV!.hooks.afterCreateSignal = cb; + + createSignal(3, { name: "test" }); + expect(cb).toHaveBeenCalledTimes(1); + expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![0]); + + createSignal(5); + expect(cb).toHaveBeenCalledTimes(2); + expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![1]); + + createSignal(6, { name: "explicit" }); + expect(cb).toHaveBeenCalledTimes(3); + expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![2]); + }); + }); + test("OnStoreNodeUpdate Hook", () => { const cb = vi.fn(); STORE_DEV!.hooks.onStoreNodeUpdate = cb;