complete performance and reliability follow-ups

This commit is contained in:
2026-08-09 23:04:36 +05:30
parent 8f19a5eb2b
commit 232d8e6734
31 changed files with 525 additions and 726 deletions
+5 -2
View File
@@ -1,6 +1,9 @@
import { createStoreContainer } from "./index.ts";
let globalContainer: ReturnType<typeof createStoreContainer> | undefined;
const storeGlobal = globalThis as typeof globalThis & {
__wrnexusStoreContainer?: ReturnType<typeof createStoreContainer>;
};
export function browserStoreContainer(hydration: Record<string, unknown> = {}) {
if (!globalContainer) {
@@ -17,7 +20,7 @@ export function browserStoreContainer(hydration: Record<string, unknown> = {}) {
}
},
});
(globalThis as any).__wrnexusStoreContainer = globalContainer;
storeGlobal.__wrnexusStoreContainer = globalContainer;
}
return globalContainer;
}
@@ -25,5 +28,5 @@ export function browserStoreContainer(hydration: Record<string, unknown> = {}) {
export async function resetBrowserStores() {
await globalContainer?.dispose();
globalContainer = undefined;
delete (globalThis as any).__wrnexusStoreContainer;
delete storeGlobal.__wrnexusStoreContainer;
}
+20 -13
View File
@@ -27,7 +27,7 @@ function readonlySnapshot<S extends object>(state: S): Readonly<S> {
return Object.freeze(clone(state));
}
function storageFor(kind: StorePersistenceConfig<any>["storage"]): Storage | null {
function storageFor(kind: StorePersistenceConfig<object>["storage"]): Storage | null {
if (typeof window === "undefined") return null;
if (kind === "local") return window.localStorage;
if (kind === "session") return window.sessionStorage;
@@ -91,7 +91,10 @@ export class StoreContainer {
readonly runtime: "server" | "client";
readonly request?: unknown;
readonly routeId?: string;
private readonly instances = new Map<string, StoreInstance<any, any, any>>();
private readonly instances = new Map<
string,
StoreInstance<Record<string, unknown>, Record<string, unknown>, Record<string, StoreFunction>>
>();
private readonly hydration: Record<string, unknown>;
private readonly onMutation?: (mutation: StoreMutation) => void;
private readonly lastMutations = new Map<string, StoreMutation>();
@@ -159,7 +162,7 @@ export class StoreContainer {
Object.fromEntries(
Reflect.ownKeys(instance.computed).map((key) => [
String(key),
(instance.computed as any)[key],
Reflect.get(instance.computed, key),
]),
),
),
@@ -314,7 +317,7 @@ export function createStoreInstance<
const publicState = new Proxy({} as State, {
get(_target, property) {
return (mutableState as any)[property];
return Reflect.get(mutableState, property);
},
set(_target, property) {
throw new TypeError(
@@ -334,7 +337,7 @@ export function createStoreInstance<
return {
enumerable: true,
configurable: true,
value: (mutableState as any)[property],
value: Reflect.get(mutableState, property),
writable: false,
};
},
@@ -360,7 +363,7 @@ export function createStoreInstance<
mutate("$reset", () => {
const next = createInitialState();
for (const key of Object.keys(mutableState)) {
if (!(key in next)) delete (mutableState as any)[key];
if (!(key in next)) Reflect.deleteProperty(mutableState, key);
}
Object.assign(mutableState, next);
});
@@ -382,16 +385,20 @@ export function createStoreInstance<
definitions.find((entry) => entry.runtime === "shared") ??
definitions.find((entry) => entry.runtime === "legacy");
if (!selected) continue;
(actions as any)[name] = async (...args: unknown[]) => {
Reflect.set(actions, name, async (...args: unknown[]) => {
currentAction = name;
internalMutation = true;
try {
return await (selected.handler as any)(context, ...args);
const handler = selected.handler as unknown as (
context: StoreActionContext<State>,
...args: unknown[]
) => unknown;
return await handler(context, ...args);
} finally {
currentAction = "direct";
internalMutation = false;
}
};
});
}
const computed = new Proxy({} as C, {
@@ -461,7 +468,7 @@ export function createStoreInstance<
const result: Partial<State> = {};
const serverKeys = new Set(Object.keys(definition.createServerState?.() ?? {}));
for (const [key, value] of Object.entries(mutableState)) {
if (!serverKeys.has(key)) (result as any)[key] = clone(value);
if (!serverKeys.has(key)) Reflect.set(result, key, clone(value));
}
return result;
},
@@ -479,9 +486,9 @@ export function createStoreInstance<
return new Proxy(core as StoreInstance<State, C, A>, {
get(target, property, receiver) {
if (Reflect.has(target, property)) return Reflect.get(target, property, receiver);
if (property in actions) return (actions as any)[property];
if (property in (definition.computed ?? {})) return (computed as any)[property];
if (property in mutableState) return (publicState as any)[property];
if (property in actions) return Reflect.get(actions, property);
if (property in (definition.computed ?? {})) return Reflect.get(computed, property);
if (property in mutableState) return Reflect.get(publicState, property);
return undefined;
},
set(_target, property) {
+2 -1
View File
@@ -1,7 +1,8 @@
export type StoreKind = "global" | "page";
export type StoreRuntime = "shared" | "client" | "server" | "legacy";
export type PersistenceStorage = "memory" | "session" | "local";
export type StoreFunction = (...args: any[]) => any;
/** Broad callable constraint that preserves each action's concrete parameters and return type. */
export type StoreFunction = (...args: any[]) => unknown;
export type StoreCombinedState<
S extends object,