Skip to content

Commit c9947e3

Browse files
committed
dev: Add afterRegisterGraph hook replacing afterCreateSignal
Deprecate afterCreateSignal for now
1 parent 4d824b0 commit c9947e3

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

packages/solid/src/reactive/signal.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,14 @@ let ExecCount = 0;
6161
export const DevHooks: {
6262
afterUpdate: (() => void) | null;
6363
afterCreateOwner: ((owner: Owner) => void) | null;
64+
/** @deprecated use `afterRegisterGraph` */
6465
afterCreateSignal: ((signal: SignalState<any>) => void) | null;
66+
afterRegisterGraph: ((sourceMapValue: SourceMapValue) => void) | null;
6567
} = {
6668
afterUpdate: null,
6769
afterCreateOwner: null,
68-
afterCreateSignal: null
70+
afterCreateSignal: null,
71+
afterRegisterGraph: null,
6972
};
7073

7174
export type ComputationState = 0 | 1 | 2;
@@ -1131,10 +1134,12 @@ export function devComponent<P, V>(Comp: (props: P) => V, props: P): V {
11311134
}
11321135

11331136
export function registerGraph(value: SourceMapValue): void {
1134-
if (!Owner) return;
1135-
if (Owner.sourceMap) Owner.sourceMap.push(value);
1136-
else Owner.sourceMap = [value];
1137-
value.graph = Owner;
1137+
if (Owner) {
1138+
if (Owner.sourceMap) Owner.sourceMap.push(value);
1139+
else Owner.sourceMap = [value];
1140+
value.graph = Owner;
1141+
}
1142+
if (DevHooks.afterRegisterGraph) DevHooks.afterRegisterGraph(value)
11381143
}
11391144

11401145
export type ContextProviderComponent<T> = FlowComponent<{ value: T }>;

packages/solid/test/dev.spec.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,23 +130,31 @@ describe("Dev features", () => {
130130
});
131131
});
132132

133-
test("afterCreateSignal Hook", () => {
133+
test("afterRegisterGraph Hook", () => {
134134
createRoot(() => {
135135
const owner = getOwner()!;
136136
const cb = vi.fn();
137-
DEV!.hooks.afterCreateSignal = cb;
137+
DEV!.hooks.afterRegisterGraph = cb;
138138

139-
createSignal(3, { name: "test" });
139+
createSignal(1);
140140
expect(cb).toHaveBeenCalledTimes(1);
141141
expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![0]);
142+
expect(owner.sourceMap).toHaveLength(1);
142143

143-
createSignal(5);
144+
createSignal(2, { internal: true });
145+
expect(cb).toHaveBeenCalledTimes(1);
146+
expect(owner.sourceMap).toHaveLength(1);
147+
148+
createStore({});
144149
expect(cb).toHaveBeenCalledTimes(2);
145150
expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![1]);
151+
expect(owner.sourceMap).toHaveLength(2);
146152

147-
createSignal(6, { name: "explicit" });
153+
const customValue = {value: 3};
154+
DEV!.registerGraph(customValue);
148155
expect(cb).toHaveBeenCalledTimes(3);
149-
expect(cb).toHaveBeenLastCalledWith(owner.sourceMap![2]);
156+
expect(cb).toHaveBeenLastCalledWith(customValue);
157+
expect(owner.sourceMap).toHaveLength(3);
150158
});
151159
});
152160

0 commit comments

Comments
 (0)