Skip to content

Commit f9ef621

Browse files
thetarnavryansolid
andauthored
dev: Add afterRegisterGraph hook replacing afterCreateSignal (#2396)
* dev: Add afterRegisterGraph hook replacing afterCreateSignal Deprecate afterCreateSignal for now * Create wise-crabs-destroy.md --------- Co-authored-by: Ryan Carniato <[email protected]>
1 parent 89e016d commit f9ef621

File tree

3 files changed

+29
-11
lines changed

3 files changed

+29
-11
lines changed

.changeset/wise-crabs-destroy.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"solid-js": patch
3+
---
4+
5+
dev: Add afterRegisterGraph hook replacing afterCreateSignal

packages/solid/src/reactive/signal.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,14 @@ let ExecCount = 0;
6464
export const DevHooks: {
6565
afterUpdate: (() => void) | null;
6666
afterCreateOwner: ((owner: Owner) => void) | null;
67+
/** @deprecated use `afterRegisterGraph` */
6768
afterCreateSignal: ((signal: SignalState<any>) => void) | null;
69+
afterRegisterGraph: ((sourceMapValue: SourceMapValue) => void) | null;
6870
} = {
6971
afterUpdate: null,
7072
afterCreateOwner: null,
71-
afterCreateSignal: null
73+
afterCreateSignal: null,
74+
afterRegisterGraph: null,
7275
};
7376

7477
export type ComputationState = 0 | 1 | 2;
@@ -1139,10 +1142,12 @@ export function devComponent<P, V>(Comp: (props: P) => V, props: P): V {
11391142
}
11401143

11411144
export function registerGraph(value: SourceMapValue): void {
1142-
if (!Owner) return;
1143-
if (Owner.sourceMap) Owner.sourceMap.push(value);
1144-
else Owner.sourceMap = [value];
1145-
value.graph = Owner;
1145+
if (Owner) {
1146+
if (Owner.sourceMap) Owner.sourceMap.push(value);
1147+
else Owner.sourceMap = [value];
1148+
value.graph = Owner;
1149+
}
1150+
if (DevHooks.afterRegisterGraph) DevHooks.afterRegisterGraph(value)
11461151
}
11471152

11481153
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)