Skip to content

Commit

Permalink
feat(DEV): Add afterCreateSignal hook to DevHooks (#1960)
Browse files Browse the repository at this point in the history
* Add afterCreateSignal hook to DevHooks

* add changeset

---------

Co-authored-by: Ryan Carniato <ryansolid@gmail.com>
  • Loading branch information
thetarnav and ryansolid committed Nov 22, 2023
1 parent 54e1aec commit b092368
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/polite-coins-prove.md
@@ -0,0 +1,5 @@
---
"solid-js": patch
---

feat(DEV): Add afterCreateSignal hook to DevHooks
9 changes: 6 additions & 3 deletions packages/solid/src/reactive/signal.ts
Expand Up @@ -57,9 +57,11 @@ let ExecCount = 0;
export const DevHooks: {
afterUpdate: (() => void) | null;
afterCreateOwner: ((owner: Owner) => void) | null;
afterCreateSignal: ((signal: SignalState<any>) => void) | null;
} = {
afterUpdate: null,
afterCreateOwner: null
afterCreateOwner: null,
afterCreateSignal: null
};

// keep immediately evaluated module code, below its indirect declared let dependencies like Listener
Expand Down Expand Up @@ -228,9 +230,10 @@ export function createSignal<T>(
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<T | undefined> = (value?: unknown) => {
Expand Down
20 changes: 20 additions & 0 deletions packages/solid/test/dev.spec.ts
Expand Up @@ -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;
Expand Down

0 comments on commit b092368

Please sign in to comment.