Skip to content

Commit 66f41a4

Browse files
committed
perf(signals): reduce seedless store validation size
1 parent aca1706 commit 66f41a4

6 files changed

Lines changed: 25 additions & 23 deletions

File tree

‎packages/signals/src/store/next/optimistic.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
} from "../store.js";
6565
import {
6666
runProjectionComputedNext,
67+
validateSeedlessOptions,
6768
validateStoreValue,
6869
createReplayStoreValidator,
6970
type ProjectionResultValidator
@@ -210,8 +211,7 @@ export function createOptimisticStoreNext<T extends object = {}>(
210211
const derived = typeof first === "function";
211212
const seeded = !derived || second != null;
212213
const options = (derived ? third : second) as SeededProjectionOptions | undefined;
213-
if (!seeded && options?.seedLoadingValue)
214-
throw new Error("seedLoadingValue requires an explicit store seed");
214+
if (!seeded) validateSeedlessOptions(options);
215215
const derive =
216216
derived && !seeded ? () => (first as () => T | Promise<T> | AsyncIterable<T>)() : first;
217217
return createOptimisticStoreNextInternal<T>(
@@ -388,6 +388,7 @@ export function createOptimisticStoreHydrationReplayNext<T extends object = {}>(
388388
replaying: () => boolean,
389389
options?: ProjectionOptions
390390
): [get: Refreshable<Store<T>>, set: StoreSetter<T>] {
391+
validateSeedlessOptions(options);
391392
return createOptimisticStoreNextInternal<T>(
392393
fn,
393394
{} as T,

‎packages/signals/src/store/next/projection.ts‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,15 @@ import { storeSetterNext, wrapNext } from "./store.js";
4343
import type { StoreNextFamily } from "./target.js";
4444

4545
export function validateStoreValue(value: void | object): void {
46-
if (value === undefined) throw new Error("A seedless store projection must produce a value");
47-
if (value === null || typeof value !== "object")
48-
throw new Error("A seedless store projection must produce an object value");
49-
if (Array.isArray(value)) throw new Error("Array store projections require an explicit seed");
50-
const prototype = Object.getPrototypeOf(value);
46+
const prototype =
47+
value != null && typeof value === "object" ? Object.getPrototypeOf(value) : false;
5148
if (prototype !== Object.prototype && prototype !== null)
52-
throw new Error("A seedless store projection must produce a plain object value");
49+
throw new Error(__DEV__ ? "A seedless store projection must produce a plain object value" : "");
50+
}
51+
52+
export function validateSeedlessOptions(options?: SeededProjectionOptions): void {
53+
if (options?.seedLoadingValue)
54+
throw new Error(__DEV__ ? "seedLoadingValue requires an explicit store seed" : "");
5355
}
5456

5557
export type ProjectionResultValidator<T extends object> = (
@@ -261,8 +263,7 @@ export function createProjectionNext<T extends object = {}>(
261263
options?: SeededProjectionOptions
262264
): Refreshable<Store<T>> {
263265
const seeded = seed != null;
264-
if (!seeded && options?.seedLoadingValue)
265-
throw new Error("seedLoadingValue requires an explicit store seed");
266+
if (!seeded) validateSeedlessOptions(options);
266267
const derive = seeded ? fn : () => (fn as () => T | Promise<T> | AsyncIterable<T>)();
267268
return createProjectionNextInternal(
268269
derive,
@@ -278,6 +279,7 @@ export function createProjectionHydrationReplayNext<T extends object = {}>(
278279
replaying: () => boolean,
279280
options?: ProjectionOptions
280281
): Refreshable<Store<T>> {
282+
validateSeedlessOptions(options);
281283
return createProjectionNextInternal(fn, {} as T, options, createReplayStoreValidator(replaying))
282284
.store;
283285
}
@@ -293,8 +295,7 @@ export function createStoreDerivedNext<T extends object = {}>(
293295
options?: SeededProjectionOptions
294296
): [Refreshable<Store<T>>, (f: (draft: T) => T | void) => void] {
295297
const seeded = seed != null;
296-
if (!seeded && options?.seedLoadingValue)
297-
throw new Error("seedLoadingValue requires an explicit store seed");
298+
if (!seeded) validateSeedlessOptions(options);
298299
const derive = seeded ? fn : () => (fn as () => T | Promise<T> | AsyncIterable<T>)();
299300
return createStoreDerivedNextInternal(
300301
derive,
@@ -327,6 +328,7 @@ export function createStoreHydrationReplayNext<T extends object = {}>(
327328
replaying: () => boolean,
328329
options?: ProjectionOptions
329330
): [Refreshable<Store<T>>, (f: (draft: T) => T | void) => void] {
331+
validateSeedlessOptions(options);
330332
return createStoreDerivedNextInternal(
331333
fn,
332334
{} as T,

‎packages/signals/tests/store/createProjection.async.test.ts‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ describe("Projection async behavior", () => {
6363
};
6464

6565
expect(() => createInvalid(() => undefined)).toThrow(
66-
"A seedless store projection must produce a value"
66+
"A seedless store projection must produce a plain object value"
6767
);
6868
expect(() => createInvalid(() => [])).toThrow(
69-
"Array store projections require an explicit seed"
69+
"A seedless store projection must produce a plain object value"
7070
);
7171
expect(() => createInvalid(() => new State())).toThrow(
7272
"A seedless store projection must produce a plain object value"
@@ -76,7 +76,9 @@ describe("Projection async behavior", () => {
7676
flush();
7777
await Promise.resolve();
7878
await Promise.resolve();
79-
expect(() => asyncProjection.value).toThrow("A seedless store projection must produce a value");
79+
expect(() => asyncProjection.value).toThrow(
80+
"A seedless store projection must produce a plain object value"
81+
);
8082
});
8183

8284
it("resolves async draft and transforms into new value", async () => {

‎packages/solid/src/client/hydration.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,8 +1209,6 @@ function hydrateStoreLike(
12091209
options?: any
12101210
) {
12111211
markTopLevelSnapshotScope();
1212-
if (initialValue == null && options?.seedLoadingValue)
1213-
throw new Error("seedLoadingValue requires an explicit store seed");
12141212
return hydrateStoreLikeFn(coreFn, replayFn, fn, initialValue, options, options?.ssrSource);
12151213
}
12161214

‎packages/solid/src/server/signals.ts‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1990,11 +1990,8 @@ function replaceState<T extends object>(target: T, next: T): void {
19901990
}
19911991

19921992
function validateStoreValue(value: void | object): void {
1993-
if (value === undefined) throw new Error("A seedless store projection must produce a value");
1994-
if (value === null || typeof value !== "object")
1995-
throw new Error("A seedless store projection must produce an object value");
1996-
if (Array.isArray(value)) throw new Error("Array store projections require an explicit seed");
1997-
const prototype = Object.getPrototypeOf(value);
1993+
const prototype =
1994+
value != null && typeof value === "object" ? Object.getPrototypeOf(value) : false;
19981995
if (prototype !== Object.prototype && prototype !== null)
19991996
throw new Error("A seedless store projection must produce a plain object value");
20001997
}

‎packages/solid/test/client-hydration.spec.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1728,7 +1728,9 @@ describe("Async Iterable Hydration — createProjection", () => {
17281728
await Promise.resolve();
17291729
await Promise.resolve();
17301730
flush();
1731-
expect(() => store.name).toThrow("A seedless store projection must produce a value");
1731+
expect(() => store.name).toThrow(
1732+
"A seedless store projection must produce a plain object value"
1733+
);
17321734
});
17331735

17341736
test("server+AI: deep nested patch application", async () => {

0 commit comments

Comments
 (0)