Skip to content

Commit 95d063c

Browse files
committed
refactor(store): improve store types
This PR focuses more on the performance and inference issues the store types have. It is not meant to be a complete typing overhaul. Added `bigint` to `NotWrappable`. `Store` was quite complicated and often confusing as it contained properties specific to the Store implementation. However, most internal functions use `StoreNode`. As such `Store<T>` has been repurposed to simply be `DeepReadonly<T>`. `DeepReadonly` has been simplified. The Setter type is mostly unchanged externally, except: - Only `number` is allowed to index Array types (before, for example Array methods and properties were allowed). - The fallback overload is now much better and correctly constrains types but requires type annotations (they can't be inferred). - All functions passed via the setter have parameters typed readonly as they should(?) be. I tried to optimize the Setter as much as possible, and I think it's better at the same number of overloads but don't have any definitive proof of that. `produce` is now able to infer types correctly from the `setStore` call. It currently uses a `DeepMutable` type to circumvent `DeepReadonly` being part of the inferred input; the DeepReadonly part shouldn't be inferred. fixes solidjs#732
1 parent 805aa70 commit 95d063c

6 files changed

Lines changed: 201 additions & 199 deletions

File tree

‎packages/solid/store/src/index.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,10 @@ export type {
88
StorePathRange,
99
ArrayFilterFn,
1010
Part,
11-
Next,
12-
Readonly,
13-
DeepReadonly
11+
DeepReadonly,
12+
StoreKeys,
13+
StoreBranch,
14+
SetStoreFallback
1415
} from "./store";
1516
export * from "./mutable";
16-
export * from "./modifiers";
17+
export * from "./modifiers";

‎packages/solid/store/src/modifiers.ts‎

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
1-
import {
2-
setProperty,
3-
unwrap,
4-
isWrappable,
5-
Store,
6-
NotWrappable,
7-
StoreNode,
8-
DeepReadonly,
9-
$RAW,
10-
} from "./store";
1+
import { setProperty, unwrap, isWrappable, Store, StoreNode, $RAW, NotWrappable } from "./store";
112

123
export type ReconcileOptions = {
134
key?: string | null;
@@ -113,16 +104,13 @@ function applyState(
113104

114105
// Diff method for setState
115106
export function reconcile<T>(
116-
value: T | Store<T>,
107+
value: T,
117108
options: ReconcileOptions = {}
118-
): (
119-
state: T extends NotWrappable ? T : Store<DeepReadonly<T>>
120-
) => T extends NotWrappable ? T : Store<T> {
109+
): (state: Store<T>) => Store<T> {
121110
const { merge, key = "id" } = options,
122111
v = unwrap(value);
123-
return s => {
124-
const state = s as T extends NotWrappable ? T : Store<T>;
125-
if (!isWrappable(state) || !isWrappable(v)) return v as T extends NotWrappable ? T : Store<T>;
112+
return state => {
113+
if (!isWrappable(state) || !isWrappable(v)) return v as Store<T>;
126114
applyState(v, { state }, "state", merge, key);
127115
return state;
128116
};
@@ -146,15 +134,19 @@ const setterTraps: ProxyHandler<StoreNode> = {
146134
}
147135
};
148136

137+
type DeepMutable<T> = T extends NotWrappable
138+
? T
139+
: {
140+
-readonly [K in keyof T]: DeepMutable<T[K]>;
141+
};
142+
149143
// Immer style mutation style
150144
export function produce<T>(
151-
fn: (state: T) => void
152-
): (
153-
state: T extends NotWrappable ? T : Store<DeepReadonly<T>>
154-
) => T extends NotWrappable ? T : Store<T> {
155-
return s => {
156-
const state = s as T extends NotWrappable ? T : Store<T>;
157-
if (isWrappable(state)) fn(new Proxy(state as object, setterTraps) as unknown as T);
145+
fn: (state: DeepMutable<T>) => void
146+
): (state: Store<T>) => Partial<Store<T>> {
147+
return state => {
148+
if (isWrappable(state))
149+
fn(new Proxy(state as object, setterTraps) as unknown as DeepMutable<T>);
158150
return state;
159151
};
160152
}

‎packages/solid/store/src/mutable.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
$NODE,
99
$NAME,
1010
StoreNode,
11-
Store,
1211
setProperty,
1312
proxyDescriptor,
1413
ownKeys
@@ -52,7 +51,7 @@ const proxyTraps: ProxyHandler<StoreNode> = {
5251
getOwnPropertyDescriptor: proxyDescriptor
5352
};
5453

55-
function wrap<T extends StoreNode>(value: T, name?: string): Store<T> {
54+
function wrap<T extends StoreNode>(value: T, name?: string): T {
5655
let p = value[$PROXY];
5756
if (!p) {
5857
Object.defineProperty(value, $PROXY, { value: (p = new Proxy(value, proxyTraps)) });
@@ -79,10 +78,7 @@ function wrap<T extends StoreNode>(value: T, name?: string): Store<T> {
7978
return p;
8079
}
8180

82-
export function createMutable<T extends StoreNode>(
83-
state: T | Store<T>,
84-
options?: { name?: string }
85-
): Store<T> {
81+
export function createMutable<T extends StoreNode>(state: T, options?: { name?: string }): T {
8682
const unwrappedStore = unwrap<T>(state || {});
8783
const wrappedStore = wrap(
8884
unwrappedStore,
@@ -92,5 +88,5 @@ export function createMutable<T extends StoreNode>(
9288
const name = (options && options.name) || DEV.hashValue(unwrappedStore);
9389
DEV.registerGraph(name, { value: unwrappedStore });
9490
}
95-
return wrappedStore as Store<T>;
91+
return wrappedStore;
9692
}

0 commit comments

Comments
 (0)