srasm 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +36 -25
- package/dist/index.js +36 -25
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -57,10 +57,17 @@ function deepEqual(obj1, obj2) {
|
|
|
57
57
|
// src/utils/tryRegisterSRASMDevtools.ts
|
|
58
58
|
function tryRegisterSRASMDevtools(storeName, api) {
|
|
59
59
|
if (typeof window === "undefined") return;
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
60
|
+
const w = window;
|
|
61
|
+
let tries = 0;
|
|
62
|
+
const t = setInterval(() => {
|
|
63
|
+
const dt = w.__SRASM_DEVTOOLS__;
|
|
64
|
+
if (dt && typeof dt.registerStore === "function") {
|
|
65
|
+
dt.registerStore(storeName, api);
|
|
66
|
+
clearInterval(t);
|
|
67
|
+
}
|
|
68
|
+
tries++;
|
|
69
|
+
if (tries > 50) clearInterval(t);
|
|
70
|
+
}, 100);
|
|
64
71
|
}
|
|
65
72
|
|
|
66
73
|
// src/context/createStateStore.ts
|
|
@@ -93,7 +100,11 @@ function createStateStore(initialSlices) {
|
|
|
93
100
|
if (useDeepEqualCheck && deepEqual(current, next)) return;
|
|
94
101
|
} else {
|
|
95
102
|
if (typeof current === "object" && current !== null && typeof payload === "object" && payload !== null) {
|
|
96
|
-
|
|
103
|
+
if (Array.isArray(current) || Array.isArray(payload)) {
|
|
104
|
+
next = payload;
|
|
105
|
+
} else {
|
|
106
|
+
next = { ...current, ...payload };
|
|
107
|
+
}
|
|
97
108
|
if (useDeepEqualCheck && deepEqual(current, next)) return;
|
|
98
109
|
} else {
|
|
99
110
|
if (Object.is(current, payload)) return;
|
|
@@ -114,14 +125,14 @@ function createStateStore(initialSlices) {
|
|
|
114
125
|
selector ?? ((s) => s),
|
|
115
126
|
options?.isEqual ?? Object.is
|
|
116
127
|
);
|
|
117
|
-
const
|
|
128
|
+
const setState = (0, import_react.useCallback)(
|
|
118
129
|
(payload) => updateSlice(slice, payload, !!options?.useDeepEqualCheck),
|
|
119
130
|
[slice, options?.useDeepEqualCheck]
|
|
120
131
|
);
|
|
121
|
-
return { state: selected, setState
|
|
132
|
+
return { state: selected, setState };
|
|
122
133
|
}
|
|
123
134
|
function useSRASMAsync(slice, fetcher, tags = []) {
|
|
124
|
-
const { state, setState
|
|
135
|
+
const { state, setState } = useSRASM(slice);
|
|
125
136
|
const [loading, setLoading] = import_react.default.useState(false);
|
|
126
137
|
const [error, setError] = import_react.default.useState(null);
|
|
127
138
|
const fetchData = (0, import_react.useCallback)(async () => {
|
|
@@ -129,16 +140,16 @@ function createStateStore(initialSlices) {
|
|
|
129
140
|
setError(null);
|
|
130
141
|
try {
|
|
131
142
|
const data = await fetcher();
|
|
132
|
-
|
|
143
|
+
setState(data);
|
|
133
144
|
} catch (err) {
|
|
134
145
|
setError(err);
|
|
135
146
|
} finally {
|
|
136
147
|
setLoading(false);
|
|
137
148
|
}
|
|
138
|
-
}, [slice, fetcher,
|
|
149
|
+
}, [slice, fetcher, setState]);
|
|
139
150
|
import_react.default.useEffect(() => {
|
|
140
151
|
fetchData();
|
|
141
|
-
}, [JSON.stringify(tags)]);
|
|
152
|
+
}, [fetchData, JSON.stringify(tags)]);
|
|
142
153
|
return {
|
|
143
154
|
data: state,
|
|
144
155
|
loading,
|
|
@@ -149,21 +160,21 @@ function createStateStore(initialSlices) {
|
|
|
149
160
|
const SRASMProvider = ({ children }) => {
|
|
150
161
|
return children;
|
|
151
162
|
};
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
163
|
+
const storeAPI = {
|
|
164
|
+
getState: () => storeState,
|
|
165
|
+
// Subscribe to ALL slices
|
|
166
|
+
subscribe: (listener) => {
|
|
167
|
+
const unsubs = Object.keys(storeState).map(
|
|
168
|
+
(k) => subscribeSlice(k, listener)
|
|
169
|
+
);
|
|
170
|
+
return () => unsubs.forEach((u) => u());
|
|
171
|
+
},
|
|
172
|
+
// THIS is the ONLY setter that should mutate state
|
|
173
|
+
setState: (key, payload, useDeepEqualCheck = false) => {
|
|
174
|
+
updateSlice(key, payload, useDeepEqualCheck);
|
|
175
|
+
}
|
|
161
176
|
};
|
|
162
|
-
tryRegisterSRASMDevtools("default",
|
|
163
|
-
getState,
|
|
164
|
-
subscribe,
|
|
165
|
-
setState
|
|
166
|
-
});
|
|
177
|
+
tryRegisterSRASMDevtools("default", storeAPI);
|
|
167
178
|
return { SRASMProvider, useSRASM, useSRASMAsync };
|
|
168
179
|
}
|
|
169
180
|
|
package/dist/index.js
CHANGED
|
@@ -21,10 +21,17 @@ function deepEqual(obj1, obj2) {
|
|
|
21
21
|
// src/utils/tryRegisterSRASMDevtools.ts
|
|
22
22
|
function tryRegisterSRASMDevtools(storeName, api) {
|
|
23
23
|
if (typeof window === "undefined") return;
|
|
24
|
-
const
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
24
|
+
const w = window;
|
|
25
|
+
let tries = 0;
|
|
26
|
+
const t = setInterval(() => {
|
|
27
|
+
const dt = w.__SRASM_DEVTOOLS__;
|
|
28
|
+
if (dt && typeof dt.registerStore === "function") {
|
|
29
|
+
dt.registerStore(storeName, api);
|
|
30
|
+
clearInterval(t);
|
|
31
|
+
}
|
|
32
|
+
tries++;
|
|
33
|
+
if (tries > 50) clearInterval(t);
|
|
34
|
+
}, 100);
|
|
28
35
|
}
|
|
29
36
|
|
|
30
37
|
// src/context/createStateStore.ts
|
|
@@ -57,7 +64,11 @@ function createStateStore(initialSlices) {
|
|
|
57
64
|
if (useDeepEqualCheck && deepEqual(current, next)) return;
|
|
58
65
|
} else {
|
|
59
66
|
if (typeof current === "object" && current !== null && typeof payload === "object" && payload !== null) {
|
|
60
|
-
|
|
67
|
+
if (Array.isArray(current) || Array.isArray(payload)) {
|
|
68
|
+
next = payload;
|
|
69
|
+
} else {
|
|
70
|
+
next = { ...current, ...payload };
|
|
71
|
+
}
|
|
61
72
|
if (useDeepEqualCheck && deepEqual(current, next)) return;
|
|
62
73
|
} else {
|
|
63
74
|
if (Object.is(current, payload)) return;
|
|
@@ -78,14 +89,14 @@ function createStateStore(initialSlices) {
|
|
|
78
89
|
selector ?? ((s) => s),
|
|
79
90
|
options?.isEqual ?? Object.is
|
|
80
91
|
);
|
|
81
|
-
const
|
|
92
|
+
const setState = useCallback(
|
|
82
93
|
(payload) => updateSlice(slice, payload, !!options?.useDeepEqualCheck),
|
|
83
94
|
[slice, options?.useDeepEqualCheck]
|
|
84
95
|
);
|
|
85
|
-
return { state: selected, setState
|
|
96
|
+
return { state: selected, setState };
|
|
86
97
|
}
|
|
87
98
|
function useSRASMAsync(slice, fetcher, tags = []) {
|
|
88
|
-
const { state, setState
|
|
99
|
+
const { state, setState } = useSRASM(slice);
|
|
89
100
|
const [loading, setLoading] = React.useState(false);
|
|
90
101
|
const [error, setError] = React.useState(null);
|
|
91
102
|
const fetchData = useCallback(async () => {
|
|
@@ -93,16 +104,16 @@ function createStateStore(initialSlices) {
|
|
|
93
104
|
setError(null);
|
|
94
105
|
try {
|
|
95
106
|
const data = await fetcher();
|
|
96
|
-
|
|
107
|
+
setState(data);
|
|
97
108
|
} catch (err) {
|
|
98
109
|
setError(err);
|
|
99
110
|
} finally {
|
|
100
111
|
setLoading(false);
|
|
101
112
|
}
|
|
102
|
-
}, [slice, fetcher,
|
|
113
|
+
}, [slice, fetcher, setState]);
|
|
103
114
|
React.useEffect(() => {
|
|
104
115
|
fetchData();
|
|
105
|
-
}, [JSON.stringify(tags)]);
|
|
116
|
+
}, [fetchData, JSON.stringify(tags)]);
|
|
106
117
|
return {
|
|
107
118
|
data: state,
|
|
108
119
|
loading,
|
|
@@ -113,21 +124,21 @@ function createStateStore(initialSlices) {
|
|
|
113
124
|
const SRASMProvider = ({ children }) => {
|
|
114
125
|
return children;
|
|
115
126
|
};
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
127
|
+
const storeAPI = {
|
|
128
|
+
getState: () => storeState,
|
|
129
|
+
// Subscribe to ALL slices
|
|
130
|
+
subscribe: (listener) => {
|
|
131
|
+
const unsubs = Object.keys(storeState).map(
|
|
132
|
+
(k) => subscribeSlice(k, listener)
|
|
133
|
+
);
|
|
134
|
+
return () => unsubs.forEach((u) => u());
|
|
135
|
+
},
|
|
136
|
+
// THIS is the ONLY setter that should mutate state
|
|
137
|
+
setState: (key, payload, useDeepEqualCheck = false) => {
|
|
138
|
+
updateSlice(key, payload, useDeepEqualCheck);
|
|
139
|
+
}
|
|
125
140
|
};
|
|
126
|
-
tryRegisterSRASMDevtools("default",
|
|
127
|
-
getState,
|
|
128
|
-
subscribe,
|
|
129
|
-
setState
|
|
130
|
-
});
|
|
141
|
+
tryRegisterSRASMDevtools("default", storeAPI);
|
|
131
142
|
return { SRASMProvider, useSRASM, useSRASMAsync };
|
|
132
143
|
}
|
|
133
144
|
|