@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.js CHANGED
@@ -1,5 +1,5 @@
1
1
  // src/createObjectStore.ts
2
- import { objToMap, isObj as isObj2 } from "@superutils/core";
2
+ import { objToMap, isObj as isObj2, isMap as isMap2 } from "@superutils/core";
3
3
 
4
4
  // src/createStore.ts
5
5
  import { isFn as isFn2, isObj } from "@superutils/core";
@@ -85,7 +85,11 @@ var _Store = class _Store {
85
85
  this.handleSubjectChange = (data) => {
86
86
  var _a;
87
87
  if (!isMap(data)) return this.subject$.next(/* @__PURE__ */ new Map());
88
- fallbackIfFails(this.write, [data], null);
88
+ fallbackIfFails(
89
+ this.write,
90
+ [data],
91
+ this.triggerOnErrorCb("write" /* write */)
92
+ );
89
93
  fallbackIfFails(
90
94
  (_a = this.onChange) == null ? void 0 : _a.bind(this),
91
95
  [data],
@@ -247,7 +251,8 @@ var _Store = class _Store {
247
251
  return false;
248
252
  }
249
253
  };
250
- this.name = `${name != null ? name : ""}`.trim() || null;
254
+ var _a;
255
+ this.name = `${(_a = name != null ? name : options == null ? void 0 : options.name) != null ? _a : ""}`.trim() || null;
251
256
  const {
252
257
  cacheDisabled = false,
253
258
  delay,
@@ -258,7 +263,7 @@ var _Store = class _Store {
258
263
  parse,
259
264
  spaces,
260
265
  storage = fallbackIfFails(
261
- () => !name ? void 0 : globalThis.localStorage,
266
+ () => !this.name ? void 0 : globalThis.localStorage,
262
267
  [],
263
268
  void 0
264
269
  ),
@@ -309,37 +314,64 @@ var Store = _Store;
309
314
  var Store_default = Store;
310
315
 
311
316
  // src/createStore.ts
312
- function createStore(name, options) {
313
- if (isObj(name)) {
314
- options = name;
315
- name = null;
316
- }
317
- const store = new Store_default(name, options);
318
- Object.defineProperty(store, "context", {
319
- configurable: false,
320
- enumerable: false,
321
- value: isFn2(options == null ? void 0 : options.context) ? options.context(store) : options == null ? void 0 : options.context,
322
- writable: false
317
+ var isStoreKey = (store, key) => OPTIONAL_STORE_PROPS.includes(key) || store[key] !== void 0;
318
+ var OPTIONAL_STORE_PROPS = [
319
+ "delayOptions",
320
+ "name",
321
+ "onChange",
322
+ "onError",
323
+ "parse",
324
+ "spaces",
325
+ "storage",
326
+ "stringify",
327
+ "validate"
328
+ ];
329
+ function createStore(options, context) {
330
+ const store = new Store_default(options == null ? void 0 : options.name, options);
331
+ const _context = isFn2(context) ? context(store) : context;
332
+ if (!isObj(_context, false)) return store;
333
+ return new Proxy(store, {
334
+ get: (store2, key, receiver) => (
335
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-return
336
+ Reflect.get(
337
+ isStoreKey(store2, key) ? store2 : _context,
338
+ key,
339
+ receiver
340
+ )
341
+ ),
342
+ set: (store2, key, value, receiver) => Reflect.set(
343
+ isStoreKey(store2, key) ? store2 : _context,
344
+ key,
345
+ value,
346
+ receiver
347
+ )
323
348
  });
324
- return store;
325
349
  }
326
350
  var createStore_default = createStore;
327
351
 
328
352
  // src/createObjectStore.ts
329
- function createObjectStore(name, options) {
330
- if (isObj2(name)) {
331
- options = name;
332
- name = null;
333
- }
334
- const store = createStore_default(name, {
335
- parse: (str) => objToMap(JSON.parse(str != null ? str : "{}")),
336
- stringify: function(data) {
337
- return JSON.stringify(this.toObject(data));
353
+ function createObjectStore(options, context) {
354
+ options = {
355
+ // only required for persistent store and can be overriden by options
356
+ ...(options == null ? void 0 : options.name) && {
357
+ parse: (str) => objToMap(JSON.parse(str != null ? str : "{}")),
358
+ stringify(data) {
359
+ return JSON.stringify(this.toObject(data));
360
+ }
338
361
  },
339
362
  ...options,
340
363
  initialValue: !isObj2(options == null ? void 0 : options.initialValue, true) ? options == null ? void 0 : options.initialValue : objToMap(options.initialValue)
341
- });
364
+ };
365
+ const store = createStore_default(
366
+ ...[options, context]
367
+ );
342
368
  store.type = "object";
369
+ const setAll = store.setAll.bind(store);
370
+ store.setAll = (obj, replace) => {
371
+ if (obj && !isMap2(obj)) obj = objToMap(obj);
372
+ return setAll(obj, replace);
373
+ };
374
+ store.toMap = (data) => !data ? store.getAll() : objToMap(data);
343
375
  return store;
344
376
  }
345
377
 
@@ -347,6 +379,7 @@ function createObjectStore(name, options) {
347
379
  import { objToMap as objToMap2 } from "@superutils/core";
348
380
  var index_default = Store_default;
349
381
  export {
382
+ OPTIONAL_STORE_PROPS,
350
383
  Store,
351
384
  Store_OnErrorType,
352
385
  createObjectStore,
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "Toufiqur Rahaman Chowdhury",
3
3
  "description": "Generic, reactive, persistent and fully-typed Map-like data store with advanced search, filtering, and sorting capabilities.",
4
4
  "dependencies": {
5
- "@superutils/core": "^1.2.21",
5
+ "@superutils/core": "^1.2.22",
6
6
  "rxjs": "^7.8.2"
7
7
  },
8
8
  "exports": {
@@ -45,6 +45,6 @@
45
45
  "sideEffects": false,
46
46
  "type": "module",
47
47
  "types": "./dist/index.d.ts",
48
- "version": "0.1.18",
49
- "gitHead": "54a29f73ae09335819d591df271d9f57c92da457"
48
+ "version": "0.1.19",
49
+ "gitHead": "35722628393f7eface789fea15e80e521c509831"
50
50
  }