@choksheak/react-utils 0.3.5 → 0.3.7
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/NoSSR.cjs +37 -0
- package/NoSSR.d.mts +24 -0
- package/NoSSR.d.ts +20 -0
- package/NoSSR.min.cjs +2 -0
- package/NoSSR.min.cjs.map +1 -0
- package/NoSSR.min.mjs +2 -0
- package/NoSSR.min.mjs.map +1 -0
- package/NoSSR.mjs +12 -0
- package/README.md +10 -1
- package/fetcher.cjs +3 -3
- package/fetcher.d.mts +5 -4
- package/fetcher.d.ts +4 -3
- package/fetcher.min.cjs +1 -1
- package/fetcher.min.cjs.map +1 -1
- package/fetcher.min.mjs +1 -1
- package/fetcher.min.mjs.map +1 -1
- package/fetcher.mjs +2 -2
- package/package.json +21 -3
- package/sharedQuery.cjs +461 -471
- package/sharedQuery.d.mts +64 -61
- package/sharedQuery.d.ts +62 -59
- package/sharedQuery.min.cjs +1 -1
- package/sharedQuery.min.cjs.map +1 -1
- package/sharedQuery.min.mjs +1 -1
- package/sharedQuery.min.mjs.map +1 -1
- package/sharedQuery.mjs +462 -471
- package/sharedState.cjs +200 -189
- package/sharedState.d.mts +49 -37
- package/sharedState.d.ts +46 -35
- package/sharedState.min.cjs +1 -1
- package/sharedState.min.cjs.map +1 -1
- package/sharedState.min.mjs +1 -1
- package/sharedState.min.mjs.map +1 -1
- package/sharedState.mjs +201 -189
- package/storage.cjs +78 -65
- package/storage.d.mts +39 -2
- package/storage.d.ts +37 -1
- package/storage.min.cjs +1 -1
- package/storage.min.cjs.map +1 -1
- package/storage.min.mjs +1 -1
- package/storage.min.mjs.map +1 -1
- package/storage.mjs +76 -65
- package/stringify.d.mts +9 -0
- package/stringify.d.ts +9 -0
- package/stringify.min.cjs.map +1 -1
- package/stringify.min.mjs.map +1 -1
- package/useDeepMemo.cjs +1 -1
- package/useDeepMemo.d.mts +11 -0
- package/useDeepMemo.d.ts +11 -0
- package/useDeepMemo.min.cjs +1 -1
- package/useDeepMemo.min.cjs.map +1 -1
- package/useDeepMemo.min.mjs +1 -1
- package/useDeepMemo.min.mjs.map +1 -1
- package/useDeepMemo.mjs +1 -1
package/sharedState.mjs
CHANGED
|
@@ -4,7 +4,7 @@ import { useSyncExternalStore } from "react";
|
|
|
4
4
|
|
|
5
5
|
// src/storage.ts
|
|
6
6
|
import { kvStore } from "@choksheak/ts-utils/kvStore";
|
|
7
|
-
import {
|
|
7
|
+
import { createLocalStore } from "@choksheak/ts-utils/localStore";
|
|
8
8
|
var IS_NOT_BROWSER = typeof window === "undefined";
|
|
9
9
|
function getStorageAdapter(options, defaultStoreExpiryMs) {
|
|
10
10
|
const { store } = options ?? {};
|
|
@@ -13,70 +13,12 @@ function getStorageAdapter(options, defaultStoreExpiryMs) {
|
|
|
13
13
|
}
|
|
14
14
|
if ("persistTo" in store && typeof store.persistTo === "string" && "key" in store && typeof store.key === "string") {
|
|
15
15
|
const expiryMs = store.expiryMs || defaultStoreExpiryMs;
|
|
16
|
-
const
|
|
16
|
+
const config = { ...store, expiryMs };
|
|
17
17
|
switch (store.persistTo) {
|
|
18
|
-
case "localStorage":
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
return {
|
|
23
|
-
set: (key, value) => {
|
|
24
|
-
if (IS_NOT_BROWSER) return;
|
|
25
|
-
adapter.set(key, value);
|
|
26
|
-
},
|
|
27
|
-
get: (key) => {
|
|
28
|
-
if (IS_NOT_BROWSER) return void 0;
|
|
29
|
-
const value = adapter.get(key);
|
|
30
|
-
if (isValid && !isValid(value)) {
|
|
31
|
-
console.warn(
|
|
32
|
-
`localStorage adapter: Auto-discard invalid value for ${key}: ${JSON.stringify(value)}`
|
|
33
|
-
);
|
|
34
|
-
adapter.delete(key);
|
|
35
|
-
return void 0;
|
|
36
|
-
}
|
|
37
|
-
return value;
|
|
38
|
-
},
|
|
39
|
-
delete: (key) => {
|
|
40
|
-
if (IS_NOT_BROWSER) return;
|
|
41
|
-
adapter.delete(key);
|
|
42
|
-
},
|
|
43
|
-
clear: () => {
|
|
44
|
-
if (IS_NOT_BROWSER) return;
|
|
45
|
-
adapter.clear();
|
|
46
|
-
}
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
case "indexedDb": {
|
|
50
|
-
const keyPrefix = store.key + ":";
|
|
51
|
-
const isValid2 = store.isValid;
|
|
52
|
-
return {
|
|
53
|
-
set: async (key, value) => {
|
|
54
|
-
if (IS_NOT_BROWSER) return;
|
|
55
|
-
await kvStore.set(keyPrefix + key, value, expiryMs);
|
|
56
|
-
},
|
|
57
|
-
get: async (key) => {
|
|
58
|
-
if (IS_NOT_BROWSER) return void 0;
|
|
59
|
-
const fullKey = keyPrefix + key;
|
|
60
|
-
const value = await kvStore.get(fullKey);
|
|
61
|
-
if (isValid2 && !isValid2(value)) {
|
|
62
|
-
console.warn(
|
|
63
|
-
`indexedDb adapter: Auto-discard invalid value for ${key}: ${JSON.stringify(value)}`
|
|
64
|
-
);
|
|
65
|
-
await kvStore.delete(fullKey);
|
|
66
|
-
return void 0;
|
|
67
|
-
}
|
|
68
|
-
return value;
|
|
69
|
-
},
|
|
70
|
-
delete: async (key) => {
|
|
71
|
-
if (IS_NOT_BROWSER) return;
|
|
72
|
-
await kvStore.delete(keyPrefix + key);
|
|
73
|
-
},
|
|
74
|
-
clear: async () => {
|
|
75
|
-
if (IS_NOT_BROWSER) return;
|
|
76
|
-
await kvStore.clear();
|
|
77
|
-
}
|
|
78
|
-
};
|
|
79
|
-
}
|
|
18
|
+
case "localStorage":
|
|
19
|
+
return localStoreAdapter(config);
|
|
20
|
+
case "indexedDb":
|
|
21
|
+
return kvStoreAdapter(config);
|
|
80
22
|
default:
|
|
81
23
|
store.persistTo;
|
|
82
24
|
throw new Error(`Unknown store.adapter ${store.persistTo}`);
|
|
@@ -84,6 +26,73 @@ function getStorageAdapter(options, defaultStoreExpiryMs) {
|
|
|
84
26
|
}
|
|
85
27
|
return store;
|
|
86
28
|
}
|
|
29
|
+
function localStoreAdapter({
|
|
30
|
+
key,
|
|
31
|
+
expiryMs,
|
|
32
|
+
isValid
|
|
33
|
+
}) {
|
|
34
|
+
const adapter = createLocalStore(key, { defaultExpiryMs: expiryMs });
|
|
35
|
+
return {
|
|
36
|
+
set: (key2, value) => {
|
|
37
|
+
if (IS_NOT_BROWSER) return;
|
|
38
|
+
adapter.set(key2, value);
|
|
39
|
+
},
|
|
40
|
+
get: (key2) => {
|
|
41
|
+
if (IS_NOT_BROWSER) return void 0;
|
|
42
|
+
const value = adapter.get(key2);
|
|
43
|
+
if (isValid && !isValid(value)) {
|
|
44
|
+
console.warn(
|
|
45
|
+
`localStorage adapter: Auto-discard invalid value for ${key2}: ${JSON.stringify(value)}`
|
|
46
|
+
);
|
|
47
|
+
adapter.delete(key2);
|
|
48
|
+
return void 0;
|
|
49
|
+
}
|
|
50
|
+
return value;
|
|
51
|
+
},
|
|
52
|
+
delete: (key2) => {
|
|
53
|
+
if (IS_NOT_BROWSER) return;
|
|
54
|
+
adapter.delete(key2);
|
|
55
|
+
},
|
|
56
|
+
clear: () => {
|
|
57
|
+
if (IS_NOT_BROWSER) return;
|
|
58
|
+
adapter.clear();
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
function kvStoreAdapter({
|
|
63
|
+
key,
|
|
64
|
+
expiryMs,
|
|
65
|
+
isValid
|
|
66
|
+
}) {
|
|
67
|
+
const keyPrefix = key + ":";
|
|
68
|
+
return {
|
|
69
|
+
set: async (key2, value) => {
|
|
70
|
+
if (IS_NOT_BROWSER) return;
|
|
71
|
+
await kvStore.set(keyPrefix + key2, value, expiryMs);
|
|
72
|
+
},
|
|
73
|
+
get: async (key2) => {
|
|
74
|
+
if (IS_NOT_BROWSER) return void 0;
|
|
75
|
+
const fullKey = keyPrefix + key2;
|
|
76
|
+
const value = await kvStore.get(fullKey);
|
|
77
|
+
if (isValid && !isValid(value)) {
|
|
78
|
+
console.warn(
|
|
79
|
+
`indexedDb adapter: Auto-discard invalid value for ${key2}: ${JSON.stringify(value)}`
|
|
80
|
+
);
|
|
81
|
+
await kvStore.delete(fullKey);
|
|
82
|
+
return void 0;
|
|
83
|
+
}
|
|
84
|
+
return value;
|
|
85
|
+
},
|
|
86
|
+
delete: async (key2) => {
|
|
87
|
+
if (IS_NOT_BROWSER) return;
|
|
88
|
+
await kvStore.delete(keyPrefix + key2);
|
|
89
|
+
},
|
|
90
|
+
clear: async () => {
|
|
91
|
+
if (IS_NOT_BROWSER) return;
|
|
92
|
+
await kvStore.clear();
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
87
96
|
|
|
88
97
|
// src/sharedState.ts
|
|
89
98
|
var SharedStateConfig = {
|
|
@@ -101,41 +110,11 @@ var SharedStateConfig = {
|
|
|
101
110
|
function configureSharedState(config) {
|
|
102
111
|
Object.assign(SharedStateConfig, config);
|
|
103
112
|
}
|
|
104
|
-
|
|
105
|
-
dataByKey = /* @__PURE__ */ new Map();
|
|
106
|
-
subscribersByKey = /* @__PURE__ */ new Map();
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
get(key) {
|
|
110
|
-
return this.dataByKey.get(key);
|
|
111
|
-
}
|
|
112
|
-
set(key, value) {
|
|
113
|
-
const prev = this.dataByKey.get(key);
|
|
114
|
-
if (prev === value) return;
|
|
115
|
-
this.dataByKey.set(key, value);
|
|
116
|
-
this.notify(key, value, prev);
|
|
117
|
-
}
|
|
118
|
-
delete(key) {
|
|
119
|
-
this.dataByKey.delete(key);
|
|
120
|
-
}
|
|
121
|
-
setNoNotify(key, value) {
|
|
122
|
-
this.dataByKey.set(key, value);
|
|
123
|
-
}
|
|
124
|
-
// Subscribe to changes of a specific key
|
|
125
|
-
subscribe(key, subscriber) {
|
|
126
|
-
let subscribers = this.subscribersByKey.get(key);
|
|
127
|
-
if (!subscribers) {
|
|
128
|
-
subscribers = /* @__PURE__ */ new Set();
|
|
129
|
-
this.subscribersByKey.set(key, subscribers);
|
|
130
|
-
}
|
|
131
|
-
subscribers.add(subscriber);
|
|
132
|
-
return () => {
|
|
133
|
-
subscribers.delete(subscriber);
|
|
134
|
-
if (subscribers.size === 0) this.subscribersByKey.delete(key);
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
notify(key, next, prev) {
|
|
138
|
-
const subscribers = this.subscribersByKey.get(key);
|
|
113
|
+
function createPubSubStore() {
|
|
114
|
+
const dataByKey = /* @__PURE__ */ new Map();
|
|
115
|
+
const subscribersByKey = /* @__PURE__ */ new Map();
|
|
116
|
+
function notify(key, next, prev) {
|
|
117
|
+
const subscribers = subscribersByKey.get(key);
|
|
139
118
|
if (!subscribers) return;
|
|
140
119
|
const copy = Array.from(subscribers.values());
|
|
141
120
|
for (const subscriber of copy) {
|
|
@@ -146,115 +125,148 @@ var PubSubStore = class {
|
|
|
146
125
|
}
|
|
147
126
|
}
|
|
148
127
|
}
|
|
149
|
-
|
|
150
|
-
|
|
128
|
+
return {
|
|
129
|
+
// This could be undefined if T includes undefined, else it should never
|
|
130
|
+
// return undefined because the user has to always specify the default value.
|
|
131
|
+
get(key) {
|
|
132
|
+
return dataByKey.get(key);
|
|
133
|
+
},
|
|
134
|
+
set(key, value) {
|
|
135
|
+
const prev = dataByKey.get(key);
|
|
136
|
+
if (prev === value) return;
|
|
137
|
+
dataByKey.set(key, value);
|
|
138
|
+
notify(key, value, prev);
|
|
139
|
+
},
|
|
140
|
+
delete(key) {
|
|
141
|
+
dataByKey.delete(key);
|
|
142
|
+
},
|
|
143
|
+
setNoNotify(key, value) {
|
|
144
|
+
dataByKey.set(key, value);
|
|
145
|
+
},
|
|
146
|
+
// Subscribe to changes of a specific key
|
|
147
|
+
subscribe(key, subscriber) {
|
|
148
|
+
let subscribers = subscribersByKey.get(key);
|
|
149
|
+
if (!subscribers) {
|
|
150
|
+
subscribers = /* @__PURE__ */ new Set();
|
|
151
|
+
subscribersByKey.set(key, subscribers);
|
|
152
|
+
}
|
|
153
|
+
subscribers.add(subscriber);
|
|
154
|
+
return () => {
|
|
155
|
+
subscribers.delete(subscriber);
|
|
156
|
+
if (subscribers.size === 0) subscribersByKey.delete(key);
|
|
157
|
+
};
|
|
158
|
+
}
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
var pubSubStore = createPubSubStore();
|
|
151
162
|
var STORAGE_KEY = "state";
|
|
152
163
|
var stateKey = 0;
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
164
|
+
function sharedState(defaultValue, options) {
|
|
165
|
+
let readyResolve = () => {
|
|
166
|
+
};
|
|
167
|
+
const readyPromise = new Promise((resolve) => {
|
|
168
|
+
readyResolve = resolve;
|
|
169
|
+
});
|
|
170
|
+
const resolveReadyPromise = readyResolve;
|
|
171
|
+
const pubSubKey = String(stateKey++);
|
|
172
|
+
const storageAdapter = getStorageAdapter(
|
|
173
|
+
options,
|
|
174
|
+
SharedStateConfig.storeExpiryMs
|
|
175
|
+
);
|
|
176
|
+
pubSubStore.setNoNotify(pubSubKey, defaultValue);
|
|
177
|
+
const lazyLoad = options?.lazyLoad;
|
|
178
|
+
const lazy = lazyLoad !== void 0 ? lazyLoad : SharedStateConfig.lazyLoad;
|
|
179
|
+
const obj = {
|
|
180
|
+
/** Allow users to await for the completion of data init. */
|
|
181
|
+
readyPromise,
|
|
182
|
+
lazy,
|
|
183
|
+
initStarted: false,
|
|
184
|
+
initDone: false,
|
|
185
|
+
/** `subscribe` function required by useSyncExternalStore. */
|
|
186
|
+
subscribe(subscriber) {
|
|
187
|
+
return pubSubStore.subscribe(pubSubKey, () => subscriber());
|
|
188
|
+
},
|
|
189
|
+
/** Returns true if there is a backing store. */
|
|
190
|
+
hasStore() {
|
|
191
|
+
return Boolean(storageAdapter);
|
|
192
|
+
},
|
|
193
|
+
/**
|
|
194
|
+
* Set a value directly into the store. Does nothing if there is no
|
|
195
|
+
* backing store.
|
|
196
|
+
*
|
|
197
|
+
* Warning: If you want to set the value properly, you should use
|
|
198
|
+
* `setValue()` instead which also updates the in-memory copy.
|
|
199
|
+
*/
|
|
200
|
+
async setInStore(value) {
|
|
201
|
+
return await storageAdapter?.set(STORAGE_KEY, value);
|
|
202
|
+
},
|
|
203
|
+
/**
|
|
204
|
+
* Get a value directly from the store. Returns undefined if there is no
|
|
205
|
+
* backing store.
|
|
206
|
+
*/
|
|
207
|
+
async getFromStore() {
|
|
208
|
+
return await storageAdapter?.get(STORAGE_KEY);
|
|
209
|
+
},
|
|
210
|
+
/**
|
|
211
|
+
* Get the value of the shared state.
|
|
212
|
+
* `getSnapshot` function — must return same ref if value unchanged.
|
|
213
|
+
* Note that this would return the default value if data init from store is
|
|
214
|
+
* not completed yet.
|
|
215
|
+
*/
|
|
216
|
+
getSnapshot() {
|
|
217
|
+
return pubSubStore.get(pubSubKey);
|
|
218
|
+
},
|
|
219
|
+
/** Set the value of the shared state. */
|
|
220
|
+
setValue(next) {
|
|
221
|
+
if (typeof next === "function") {
|
|
222
|
+
const prev = pubSubStore.get(pubSubKey);
|
|
223
|
+
next = next(prev);
|
|
224
|
+
}
|
|
225
|
+
pubSubStore.set(pubSubKey, next);
|
|
226
|
+
void obj.setInStore(next);
|
|
227
|
+
},
|
|
228
|
+
/**
|
|
229
|
+
* Remove the value of the shared state. Actually this just sets the value
|
|
230
|
+
* back to the given default value.
|
|
231
|
+
*/
|
|
232
|
+
delete() {
|
|
233
|
+
pubSubStore.set(pubSubKey, defaultValue);
|
|
234
|
+
void storageAdapter?.set(STORAGE_KEY, defaultValue);
|
|
235
|
+
},
|
|
236
|
+
/** Initial default handling. */
|
|
237
|
+
async initDefaultValueOnce() {
|
|
238
|
+
if (obj.initDone || obj.initStarted) return;
|
|
239
|
+
obj.initStarted = true;
|
|
240
|
+
const stored = await obj.getFromStore();
|
|
241
|
+
const value = stored !== void 0 ? stored : defaultValue;
|
|
242
|
+
pubSubStore.set(pubSubKey, value);
|
|
243
|
+
obj.initDone = true;
|
|
244
|
+
resolveReadyPromise();
|
|
215
245
|
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Remove the value of the shared state. Actually this just sets the value
|
|
221
|
-
* back to the given default value.
|
|
222
|
-
*/
|
|
223
|
-
delete() {
|
|
224
|
-
pubSubStore.set(this.pubSubKey, this.defaultValue);
|
|
225
|
-
void this.storageAdapter?.set(STORAGE_KEY, this.defaultValue);
|
|
226
|
-
}
|
|
227
|
-
/** Initial default handling. */
|
|
228
|
-
async initDefaultValueOnce() {
|
|
229
|
-
if (this.initDone || this.initStarted) return;
|
|
230
|
-
this.initStarted = true;
|
|
231
|
-
const stored = await this.getFromStore();
|
|
232
|
-
const value = stored !== void 0 ? stored : this.defaultValue;
|
|
233
|
-
pubSubStore.set(this.pubSubKey, value);
|
|
234
|
-
this.initDone = true;
|
|
246
|
+
};
|
|
247
|
+
if (!lazy) {
|
|
248
|
+
void obj.initDefaultValueOnce();
|
|
235
249
|
}
|
|
236
|
-
|
|
237
|
-
function sharedState(defaultValue, options) {
|
|
238
|
-
return new SharedState(defaultValue, options);
|
|
250
|
+
return obj;
|
|
239
251
|
}
|
|
240
252
|
function useSharedState(state) {
|
|
241
|
-
state.initDefaultValueOnce();
|
|
253
|
+
void state.initDefaultValueOnce();
|
|
242
254
|
const value = useSyncExternalStore(
|
|
243
255
|
(onStoreChange) => state.subscribe(onStoreChange),
|
|
244
256
|
() => state.getSnapshot(),
|
|
245
257
|
// getServerSnapshot is needed to prevent SSR dev errors in nextjs.
|
|
246
258
|
() => state.getSnapshot()
|
|
247
259
|
);
|
|
248
|
-
return [value, state.
|
|
260
|
+
return [value, state.setValue];
|
|
249
261
|
}
|
|
250
262
|
function useSharedStateValue(state) {
|
|
251
263
|
return useSharedState(state)[0];
|
|
252
264
|
}
|
|
253
265
|
function useSharedStateSetter(state) {
|
|
254
|
-
|
|
266
|
+
void state.initDefaultValueOnce();
|
|
267
|
+
return state.setValue;
|
|
255
268
|
}
|
|
256
269
|
export {
|
|
257
|
-
SharedState,
|
|
258
270
|
SharedStateConfig,
|
|
259
271
|
configureSharedState,
|
|
260
272
|
sharedState,
|
package/storage.cjs
CHANGED
|
@@ -20,7 +20,9 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/storage.ts
|
|
21
21
|
var storage_exports = {};
|
|
22
22
|
__export(storage_exports, {
|
|
23
|
-
getStorageAdapter: () => getStorageAdapter
|
|
23
|
+
getStorageAdapter: () => getStorageAdapter,
|
|
24
|
+
kvStoreAdapter: () => kvStoreAdapter,
|
|
25
|
+
localStoreAdapter: () => localStoreAdapter
|
|
24
26
|
});
|
|
25
27
|
module.exports = __toCommonJS(storage_exports);
|
|
26
28
|
var import_kvStore = require("@choksheak/ts-utils/kvStore");
|
|
@@ -33,70 +35,12 @@ function getStorageAdapter(options, defaultStoreExpiryMs) {
|
|
|
33
35
|
}
|
|
34
36
|
if ("persistTo" in store && typeof store.persistTo === "string" && "key" in store && typeof store.key === "string") {
|
|
35
37
|
const expiryMs = store.expiryMs || defaultStoreExpiryMs;
|
|
36
|
-
const
|
|
38
|
+
const config = { ...store, expiryMs };
|
|
37
39
|
switch (store.persistTo) {
|
|
38
|
-
case "localStorage":
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
return {
|
|
43
|
-
set: (key, value) => {
|
|
44
|
-
if (IS_NOT_BROWSER) return;
|
|
45
|
-
adapter.set(key, value);
|
|
46
|
-
},
|
|
47
|
-
get: (key) => {
|
|
48
|
-
if (IS_NOT_BROWSER) return void 0;
|
|
49
|
-
const value = adapter.get(key);
|
|
50
|
-
if (isValid && !isValid(value)) {
|
|
51
|
-
console.warn(
|
|
52
|
-
`localStorage adapter: Auto-discard invalid value for ${key}: ${JSON.stringify(value)}`
|
|
53
|
-
);
|
|
54
|
-
adapter.delete(key);
|
|
55
|
-
return void 0;
|
|
56
|
-
}
|
|
57
|
-
return value;
|
|
58
|
-
},
|
|
59
|
-
delete: (key) => {
|
|
60
|
-
if (IS_NOT_BROWSER) return;
|
|
61
|
-
adapter.delete(key);
|
|
62
|
-
},
|
|
63
|
-
clear: () => {
|
|
64
|
-
if (IS_NOT_BROWSER) return;
|
|
65
|
-
adapter.clear();
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
case "indexedDb": {
|
|
70
|
-
const keyPrefix = store.key + ":";
|
|
71
|
-
const isValid2 = store.isValid;
|
|
72
|
-
return {
|
|
73
|
-
set: async (key, value) => {
|
|
74
|
-
if (IS_NOT_BROWSER) return;
|
|
75
|
-
await import_kvStore.kvStore.set(keyPrefix + key, value, expiryMs);
|
|
76
|
-
},
|
|
77
|
-
get: async (key) => {
|
|
78
|
-
if (IS_NOT_BROWSER) return void 0;
|
|
79
|
-
const fullKey = keyPrefix + key;
|
|
80
|
-
const value = await import_kvStore.kvStore.get(fullKey);
|
|
81
|
-
if (isValid2 && !isValid2(value)) {
|
|
82
|
-
console.warn(
|
|
83
|
-
`indexedDb adapter: Auto-discard invalid value for ${key}: ${JSON.stringify(value)}`
|
|
84
|
-
);
|
|
85
|
-
await import_kvStore.kvStore.delete(fullKey);
|
|
86
|
-
return void 0;
|
|
87
|
-
}
|
|
88
|
-
return value;
|
|
89
|
-
},
|
|
90
|
-
delete: async (key) => {
|
|
91
|
-
if (IS_NOT_BROWSER) return;
|
|
92
|
-
await import_kvStore.kvStore.delete(keyPrefix + key);
|
|
93
|
-
},
|
|
94
|
-
clear: async () => {
|
|
95
|
-
if (IS_NOT_BROWSER) return;
|
|
96
|
-
await import_kvStore.kvStore.clear();
|
|
97
|
-
}
|
|
98
|
-
};
|
|
99
|
-
}
|
|
40
|
+
case "localStorage":
|
|
41
|
+
return localStoreAdapter(config);
|
|
42
|
+
case "indexedDb":
|
|
43
|
+
return kvStoreAdapter(config);
|
|
100
44
|
default:
|
|
101
45
|
store.persistTo;
|
|
102
46
|
throw new Error(`Unknown store.adapter ${store.persistTo}`);
|
|
@@ -104,7 +48,76 @@ function getStorageAdapter(options, defaultStoreExpiryMs) {
|
|
|
104
48
|
}
|
|
105
49
|
return store;
|
|
106
50
|
}
|
|
51
|
+
function localStoreAdapter({
|
|
52
|
+
key,
|
|
53
|
+
expiryMs,
|
|
54
|
+
isValid
|
|
55
|
+
}) {
|
|
56
|
+
const adapter = (0, import_localStore.createLocalStore)(key, { defaultExpiryMs: expiryMs });
|
|
57
|
+
return {
|
|
58
|
+
set: (key2, value) => {
|
|
59
|
+
if (IS_NOT_BROWSER) return;
|
|
60
|
+
adapter.set(key2, value);
|
|
61
|
+
},
|
|
62
|
+
get: (key2) => {
|
|
63
|
+
if (IS_NOT_BROWSER) return void 0;
|
|
64
|
+
const value = adapter.get(key2);
|
|
65
|
+
if (isValid && !isValid(value)) {
|
|
66
|
+
console.warn(
|
|
67
|
+
`localStorage adapter: Auto-discard invalid value for ${key2}: ${JSON.stringify(value)}`
|
|
68
|
+
);
|
|
69
|
+
adapter.delete(key2);
|
|
70
|
+
return void 0;
|
|
71
|
+
}
|
|
72
|
+
return value;
|
|
73
|
+
},
|
|
74
|
+
delete: (key2) => {
|
|
75
|
+
if (IS_NOT_BROWSER) return;
|
|
76
|
+
adapter.delete(key2);
|
|
77
|
+
},
|
|
78
|
+
clear: () => {
|
|
79
|
+
if (IS_NOT_BROWSER) return;
|
|
80
|
+
adapter.clear();
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function kvStoreAdapter({
|
|
85
|
+
key,
|
|
86
|
+
expiryMs,
|
|
87
|
+
isValid
|
|
88
|
+
}) {
|
|
89
|
+
const keyPrefix = key + ":";
|
|
90
|
+
return {
|
|
91
|
+
set: async (key2, value) => {
|
|
92
|
+
if (IS_NOT_BROWSER) return;
|
|
93
|
+
await import_kvStore.kvStore.set(keyPrefix + key2, value, expiryMs);
|
|
94
|
+
},
|
|
95
|
+
get: async (key2) => {
|
|
96
|
+
if (IS_NOT_BROWSER) return void 0;
|
|
97
|
+
const fullKey = keyPrefix + key2;
|
|
98
|
+
const value = await import_kvStore.kvStore.get(fullKey);
|
|
99
|
+
if (isValid && !isValid(value)) {
|
|
100
|
+
console.warn(
|
|
101
|
+
`indexedDb adapter: Auto-discard invalid value for ${key2}: ${JSON.stringify(value)}`
|
|
102
|
+
);
|
|
103
|
+
await import_kvStore.kvStore.delete(fullKey);
|
|
104
|
+
return void 0;
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
107
|
+
},
|
|
108
|
+
delete: async (key2) => {
|
|
109
|
+
if (IS_NOT_BROWSER) return;
|
|
110
|
+
await import_kvStore.kvStore.delete(keyPrefix + key2);
|
|
111
|
+
},
|
|
112
|
+
clear: async () => {
|
|
113
|
+
if (IS_NOT_BROWSER) return;
|
|
114
|
+
await import_kvStore.kvStore.clear();
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
}
|
|
107
118
|
// Annotate the CommonJS export names for ESM import in node:
|
|
108
119
|
0 && (module.exports = {
|
|
109
|
-
getStorageAdapter
|
|
120
|
+
getStorageAdapter,
|
|
121
|
+
kvStoreAdapter,
|
|
122
|
+
localStoreAdapter
|
|
110
123
|
});
|
package/storage.d.mts
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
|
+
import { Duration } from '@choksheak/ts-utils/duration';
|
|
1
2
|
import { StorageAdapter } from '@choksheak/ts-utils/storageAdapter';
|
|
2
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @packageDocumentation
|
|
6
|
+
*
|
|
7
|
+
* Backing storage implementation for the shared state. Note that this uses the
|
|
8
|
+
* LocalStore and KvStore from package "@choksheak/ts-utils", but is customized
|
|
9
|
+
* to be used for shared states.
|
|
10
|
+
*
|
|
11
|
+
* Normally you will not need to use this module directly, but if you have a
|
|
12
|
+
* need for it, feel free to use it.
|
|
13
|
+
*/
|
|
14
|
+
|
|
3
15
|
/** List of all available pre-built persistence options. */
|
|
4
16
|
type PersistTo = "localStorage" | "indexedDb";
|
|
5
17
|
/** Options to configure a pre-built store adapter. */
|
|
@@ -9,10 +21,11 @@ type StoreOptions = {
|
|
|
9
21
|
/** Storage key to act as a namespace to store the data. */
|
|
10
22
|
key: string;
|
|
11
23
|
/** How long to wait before the value is considered expired. */
|
|
12
|
-
expiryMs?: number;
|
|
24
|
+
expiryMs?: number | Duration;
|
|
13
25
|
/** Returns true if the given value supposed to be of type T is valid. */
|
|
14
26
|
isValid?: (u: unknown) => boolean;
|
|
15
27
|
};
|
|
28
|
+
type StoreConstructorArgs = Omit<StoreOptions, "persistTo">;
|
|
16
29
|
/** Either use a pre-built store, or a custom one you provide. */
|
|
17
30
|
type StorageOptions<T> = {
|
|
18
31
|
/**
|
|
@@ -21,6 +34,30 @@ type StorageOptions<T> = {
|
|
|
21
34
|
*/
|
|
22
35
|
store?: StorageAdapter<T> | StoreOptions;
|
|
23
36
|
};
|
|
37
|
+
/**
|
|
38
|
+
* Convert a StorageOptions<T> config object into an actual storage adapter, or
|
|
39
|
+
* returns null if a storage adapter is not configured.
|
|
40
|
+
*/
|
|
24
41
|
declare function getStorageAdapter<T>(options: StorageOptions<T> | undefined, defaultStoreExpiryMs: number): StorageAdapter<T> | null;
|
|
42
|
+
/**
|
|
43
|
+
* Create a new SSR-safe local store adapter, which allows users to reuse the
|
|
44
|
+
* same adapter in multiple shared states or shared queries.
|
|
45
|
+
*/
|
|
46
|
+
declare function localStoreAdapter<T>({ key, expiryMs, isValid, }: StoreConstructorArgs): {
|
|
47
|
+
set: (key: string, value: T) => void;
|
|
48
|
+
get: (key: string) => T | undefined;
|
|
49
|
+
delete: (key: string) => void;
|
|
50
|
+
clear: () => void;
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* Create a new SSR-safe indexed DB KV store adapter, which allows users to
|
|
54
|
+
* reuse the same adapter in multiple shared states or shared queries.
|
|
55
|
+
*/
|
|
56
|
+
declare function kvStoreAdapter<T>({ key, expiryMs, isValid, }: StoreConstructorArgs): {
|
|
57
|
+
set: (key: string, value: T) => Promise<void>;
|
|
58
|
+
get: (key: string) => Promise<T | undefined>;
|
|
59
|
+
delete: (key: string) => Promise<void>;
|
|
60
|
+
clear: () => Promise<void>;
|
|
61
|
+
};
|
|
25
62
|
|
|
26
|
-
export { type PersistTo, type StorageOptions, type StoreOptions, getStorageAdapter };
|
|
63
|
+
export { type PersistTo, type StorageOptions, type StoreConstructorArgs, type StoreOptions, getStorageAdapter, kvStoreAdapter, localStoreAdapter };
|