Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev: Add afterRegisterGraph hook replacing afterCreateSignal #2396

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions packages/solid/src/reactive/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,14 @@ let ExecCount = 0;
export const DevHooks: {
afterUpdate: (() => void) | null;
afterCreateOwner: ((owner: Owner) => void) | null;
/** @deprecated use `afterRegisterGraph` */
afterCreateSignal: ((signal: SignalState<any>) => void) | null;
afterRegisterGraph: ((sourceMapValue: SourceMapValue) => void) | null;
} = {
afterUpdate: null,
afterCreateOwner: null,
afterCreateSignal: null
afterCreateSignal: null,
afterRegisterGraph: null,
};

export type ComputationState = 0 | 1 | 2;
Expand Down Expand Up @@ -1131,10 +1134,12 @@ export function devComponent<P, V>(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<T> = FlowComponent<{ value: T }>;
Expand Down
20 changes: 14 additions & 6 deletions packages/solid/test/dev.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

Expand Down
Loading