@superutils/store 0.1.18 → 0.1.19

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 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)(this.write, [data], null);
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
- this.name = `${name != null ? name : ""}`.trim() || null;
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,64 @@ var Store = _Store;
323
329
  var Store_default = Store;
324
330
 
325
331
  // src/createStore.ts
326
- function createStore(name, options) {
327
- if ((0, import_core2.isObj)(name)) {
328
- options = name;
329
- name = null;
330
- }
331
- const store = new Store_default(name, options);
332
- Object.defineProperty(store, "context", {
333
- configurable: false,
334
- enumerable: false,
335
- value: (0, import_core2.isFn)(options == null ? void 0 : options.context) ? options.context(store) : options == null ? void 0 : options.context,
336
- writable: false
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(name, options) {
344
- if ((0, import_core3.isObj)(name)) {
345
- options = name;
346
- name = null;
347
- }
348
- const store = createStore_default(name, {
349
- parse: (str) => (0, import_core3.objToMap)(JSON.parse(str != null ? str : "{}")),
350
- stringify: function(data) {
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, context]
382
+ );
356
383
  store.type = "object";
384
+ const setAll = store.setAll.bind(store);
385
+ store.setAll = (obj, replace) => {
386
+ if (obj && !(0, import_core3.isMap)(obj)) obj = (0, import_core3.objToMap)(obj);
387
+ return setAll(obj, replace);
388
+ };
389
+ store.toMap = (data) => !data ? store.getAll() : (0, import_core3.objToMap)(data);
357
390
  return store;
358
391
  }
359
392
 
@@ -362,6 +395,7 @@ var import_core4 = require("@superutils/core");
362
395
  var index_default = Store_default;
363
396
  // Annotate the CommonJS export names for ESM import in node:
364
397
  0 && (module.exports = {
398
+ OPTIONAL_STORE_PROPS,
365
399
  Store,
366
400
  Store_OnErrorType,
367
401
  createObjectStore,