@createstore/createstore 0.0.0 → 1.1.0
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/README.md +30 -0
- package/dist/cjs/createStore.cjs +48 -9
- package/dist/cjs/createStore.cjs.map +1 -1
- package/dist/esm/createStore.js +48 -9
- package/dist/esm/createStore.js.map +1 -1
- package/package.json +23 -4
package/dist/esm/createStore.js
CHANGED
|
@@ -11,6 +11,15 @@ import {
|
|
|
11
11
|
ClientRoot
|
|
12
12
|
} from "./react-bindings.js";
|
|
13
13
|
const __SnapshotContext = RSC ? null : createContext(null);
|
|
14
|
+
let leavingPage = false;
|
|
15
|
+
if (typeof window !== "undefined" && typeof window.addEventListener === "function") {
|
|
16
|
+
window.addEventListener("pagehide", () => {
|
|
17
|
+
leavingPage = true;
|
|
18
|
+
});
|
|
19
|
+
window.addEventListener("pageshow", () => {
|
|
20
|
+
leavingPage = false;
|
|
21
|
+
});
|
|
22
|
+
}
|
|
14
23
|
const storesByName = /* @__PURE__ */ new Map();
|
|
15
24
|
const MERGE = /* @__PURE__ */ Symbol("createStore.merge");
|
|
16
25
|
const QUIET = /* @__PURE__ */ Symbol("createStore.quiet");
|
|
@@ -145,12 +154,48 @@ function defineEvents() {
|
|
|
145
154
|
return void 0;
|
|
146
155
|
}
|
|
147
156
|
function createStore(initialState, options = {}) {
|
|
157
|
+
checkStoreOptions(options);
|
|
148
158
|
if ("queries" in options)
|
|
149
159
|
throw new Error(
|
|
150
160
|
`[${options.name ?? "store"}] the queries option was replaced: declare each query in the state with query(fetch, options), e.g. { product: query((id, signal) => api.product(id, signal)) }, and read it with useGlobalState('product', id).`
|
|
151
161
|
);
|
|
152
162
|
return withScopes(initialState, options, makeBase(initialState, options));
|
|
153
163
|
}
|
|
164
|
+
const STORE_OPTIONS = /* @__PURE__ */ new Set([
|
|
165
|
+
"name",
|
|
166
|
+
"persist",
|
|
167
|
+
"syncTabs",
|
|
168
|
+
"history",
|
|
169
|
+
"devtools",
|
|
170
|
+
"shared",
|
|
171
|
+
"ssr",
|
|
172
|
+
"logger",
|
|
173
|
+
"onChange",
|
|
174
|
+
"offline",
|
|
175
|
+
"validate",
|
|
176
|
+
"events",
|
|
177
|
+
"computed",
|
|
178
|
+
"keyActions",
|
|
179
|
+
"actions",
|
|
180
|
+
"queries"
|
|
181
|
+
]);
|
|
182
|
+
const warnedOptions = /* @__PURE__ */ new Set();
|
|
183
|
+
function checkStoreOptions(opts) {
|
|
184
|
+
const options = opts;
|
|
185
|
+
const warn = (message) => {
|
|
186
|
+
if (warnedOptions.has(message)) return;
|
|
187
|
+
warnedOptions.add(message);
|
|
188
|
+
console.warn(message);
|
|
189
|
+
};
|
|
190
|
+
const id = options.name ?? "store";
|
|
191
|
+
for (const k of Object.keys(options))
|
|
192
|
+
if (!STORE_OPTIONS.has(k))
|
|
193
|
+
warn(`[${id}] createStore() has no option "${k}", so it is ignored. Options: ${[...STORE_OPTIONS].filter((o) => o !== "queries").join(", ")}.`);
|
|
194
|
+
if (!options.name && (options.persist || options.syncTabs || options.offline || options.shared))
|
|
195
|
+
warn(
|
|
196
|
+
`[store] persist, syncTabs, offline and shared use the store's name to tell stores apart. Without { name: '...' }, every unnamed store uses "store", so two of them would overwrite each other's saved data and mix up their tabs. Give this one a name.`
|
|
197
|
+
);
|
|
198
|
+
}
|
|
154
199
|
const warnedSharedOnServer = /* @__PURE__ */ new Set();
|
|
155
200
|
const warnedShape = /* @__PURE__ */ new Set();
|
|
156
201
|
const SHAPE = /* @__PURE__ */ Symbol("createStore.shape");
|
|
@@ -1245,6 +1290,7 @@ function build(initialState, options, sharedDefinition) {
|
|
|
1245
1290
|
updateHistoryInfo();
|
|
1246
1291
|
}
|
|
1247
1292
|
function batch(fn) {
|
|
1293
|
+
if (typeof fn !== "function") throw new Error(`[${name}] batch() takes a function that makes the changes: store.batch(() => { store.setState('a', 1); store.setState('b', 2); }).`);
|
|
1248
1294
|
batchDepth++;
|
|
1249
1295
|
if (batchDepth === 1 && historyLimit) batchChanges = /* @__PURE__ */ Object.create(null);
|
|
1250
1296
|
let result;
|
|
@@ -1572,15 +1618,6 @@ function build(initialState, options, sharedDefinition) {
|
|
|
1572
1618
|
ownPromises.add(promise);
|
|
1573
1619
|
return promise;
|
|
1574
1620
|
}
|
|
1575
|
-
let leavingPage = false;
|
|
1576
|
-
if (isBrowser && typeof window.addEventListener === "function") {
|
|
1577
|
-
window.addEventListener("pagehide", () => {
|
|
1578
|
-
leavingPage = true;
|
|
1579
|
-
});
|
|
1580
|
-
window.addEventListener("pageshow", () => {
|
|
1581
|
-
leavingPage = false;
|
|
1582
|
-
});
|
|
1583
|
-
}
|
|
1584
1621
|
const SERVER_LOADING = Object.freeze({
|
|
1585
1622
|
state: "loading",
|
|
1586
1623
|
loading: true,
|
|
@@ -2224,6 +2261,8 @@ function build(initialState, options, sharedDefinition) {
|
|
|
2224
2261
|
};
|
|
2225
2262
|
const api = Object.assign(Object.create(setterProtoOf(def, API_NAMES, "apiProto")), apiMembers);
|
|
2226
2263
|
Object.defineProperty(api, SETTER_OF, { value: (k) => setterFor(k) });
|
|
2264
|
+
if (options.actions != null && typeof options.actions !== "function")
|
|
2265
|
+
throw new Error(`[${name}] actions must be a function that receives the store's api and returns your actions: createStore(state, { actions: (api) => ({ add(x) { api.setState('items', (i) => [...i, x]); } }) }).`);
|
|
2227
2266
|
const actions = options.actions?.(api) ?? {};
|
|
2228
2267
|
const keyActionDefs = options.keyActions ?? {};
|
|
2229
2268
|
const ssrCfg = options.ssr;
|