@superutils/store 0.1.18 → 0.1.20
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 +68 -50
- package/dist/browser/index.min.js +2 -2
- package/dist/browser/index.min.js.map +1 -1
- package/dist/index.cjs +60 -25
- package/dist/index.d.cts +426 -330
- package/dist/index.d.ts +426 -330
- package/dist/index.js +60 -26
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -20,6 +20,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
20
20
|
// src/index.ts
|
|
21
21
|
var index_exports = {};
|
|
22
22
|
__export(index_exports, {
|
|
23
|
+
OPTIONAL_STORE_PROPS: () => OPTIONAL_STORE_PROPS,
|
|
23
24
|
Store: () => Store,
|
|
24
25
|
Store_OnErrorType: () => Store_OnErrorType,
|
|
25
26
|
createObjectStore: () => createObjectStore,
|
|
@@ -99,7 +100,11 @@ var _Store = class _Store {
|
|
|
99
100
|
this.handleSubjectChange = (data) => {
|
|
100
101
|
var _a;
|
|
101
102
|
if (!(0, import_core.isMap)(data)) return this.subject$.next(/* @__PURE__ */ new Map());
|
|
102
|
-
(0, import_core.fallbackIfFails)(
|
|
103
|
+
(0, import_core.fallbackIfFails)(
|
|
104
|
+
this.write,
|
|
105
|
+
[data],
|
|
106
|
+
this.triggerOnErrorCb("write" /* write */)
|
|
107
|
+
);
|
|
103
108
|
(0, import_core.fallbackIfFails)(
|
|
104
109
|
(_a = this.onChange) == null ? void 0 : _a.bind(this),
|
|
105
110
|
[data],
|
|
@@ -261,7 +266,8 @@ var _Store = class _Store {
|
|
|
261
266
|
return false;
|
|
262
267
|
}
|
|
263
268
|
};
|
|
264
|
-
|
|
269
|
+
var _a;
|
|
270
|
+
this.name = `${(_a = name != null ? name : options == null ? void 0 : options.name) != null ? _a : ""}`.trim() || null;
|
|
265
271
|
const {
|
|
266
272
|
cacheDisabled = false,
|
|
267
273
|
delay,
|
|
@@ -272,7 +278,7 @@ var _Store = class _Store {
|
|
|
272
278
|
parse,
|
|
273
279
|
spaces,
|
|
274
280
|
storage = (0, import_core.fallbackIfFails)(
|
|
275
|
-
() => !name ? void 0 : globalThis.localStorage,
|
|
281
|
+
() => !this.name ? void 0 : globalThis.localStorage,
|
|
276
282
|
[],
|
|
277
283
|
void 0
|
|
278
284
|
),
|
|
@@ -323,37 +329,65 @@ var Store = _Store;
|
|
|
323
329
|
var Store_default = Store;
|
|
324
330
|
|
|
325
331
|
// src/createStore.ts
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
332
|
+
var isStoreKey = (store, key) => OPTIONAL_STORE_PROPS.includes(key) || store[key] !== void 0;
|
|
333
|
+
var OPTIONAL_STORE_PROPS = [
|
|
334
|
+
"delayOptions",
|
|
335
|
+
"name",
|
|
336
|
+
"onChange",
|
|
337
|
+
"onError",
|
|
338
|
+
"parse",
|
|
339
|
+
"spaces",
|
|
340
|
+
"storage",
|
|
341
|
+
"stringify",
|
|
342
|
+
"validate"
|
|
343
|
+
];
|
|
344
|
+
function createStore(options, context) {
|
|
345
|
+
const store = new Store_default(options == null ? void 0 : options.name, options);
|
|
346
|
+
const _context = (0, import_core2.isFn)(context) ? context(store) : context;
|
|
347
|
+
if (!(0, import_core2.isObj)(_context, false)) return store;
|
|
348
|
+
return new Proxy(store, {
|
|
349
|
+
get: (store2, key, receiver) => (
|
|
350
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
|
351
|
+
Reflect.get(
|
|
352
|
+
isStoreKey(store2, key) ? store2 : _context,
|
|
353
|
+
key,
|
|
354
|
+
receiver
|
|
355
|
+
)
|
|
356
|
+
),
|
|
357
|
+
set: (store2, key, value, receiver) => Reflect.set(
|
|
358
|
+
isStoreKey(store2, key) ? store2 : _context,
|
|
359
|
+
key,
|
|
360
|
+
value,
|
|
361
|
+
receiver
|
|
362
|
+
)
|
|
337
363
|
});
|
|
338
|
-
return store;
|
|
339
364
|
}
|
|
340
365
|
var createStore_default = createStore;
|
|
341
366
|
|
|
342
367
|
// src/createObjectStore.ts
|
|
343
|
-
function createObjectStore(
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
name
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
return JSON.stringify(this.toObject(data));
|
|
368
|
+
function createObjectStore(options, context) {
|
|
369
|
+
options = {
|
|
370
|
+
// only required for persistent store and can be overriden by options
|
|
371
|
+
...(options == null ? void 0 : options.name) && {
|
|
372
|
+
parse: (str) => (0, import_core3.objToMap)(JSON.parse(str != null ? str : "{}")),
|
|
373
|
+
stringify(data) {
|
|
374
|
+
return JSON.stringify(this.toObject(data));
|
|
375
|
+
}
|
|
352
376
|
},
|
|
353
377
|
...options,
|
|
354
378
|
initialValue: !(0, import_core3.isObj)(options == null ? void 0 : options.initialValue, true) ? options == null ? void 0 : options.initialValue : (0, import_core3.objToMap)(options.initialValue)
|
|
355
|
-
}
|
|
379
|
+
};
|
|
380
|
+
const store = createStore_default(
|
|
381
|
+
options,
|
|
382
|
+
context
|
|
383
|
+
);
|
|
356
384
|
store.type = "object";
|
|
385
|
+
const setAll = store.setAll.bind(store);
|
|
386
|
+
store.setAll = (obj, replace) => {
|
|
387
|
+
if (obj && !(0, import_core3.isMap)(obj)) obj = (0, import_core3.objToMap)(obj);
|
|
388
|
+
return setAll(obj, replace);
|
|
389
|
+
};
|
|
390
|
+
store.toMap = (data) => !data ? store.getAll() : (0, import_core3.objToMap)(data);
|
|
357
391
|
return store;
|
|
358
392
|
}
|
|
359
393
|
|
|
@@ -362,6 +396,7 @@ var import_core4 = require("@superutils/core");
|
|
|
362
396
|
var index_default = Store_default;
|
|
363
397
|
// Annotate the CommonJS export names for ESM import in node:
|
|
364
398
|
0 && (module.exports = {
|
|
399
|
+
OPTIONAL_STORE_PROPS,
|
|
365
400
|
Store,
|
|
366
401
|
Store_OnErrorType,
|
|
367
402
|
createObjectStore,
|