@superutils/rx 0.1.1 → 0.1.2

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
@@ -278,8 +278,6 @@ var DataStorage = class {
278
278
  * ```
279
279
  */
280
280
  constructor(name, options) {
281
- /** Debounce and throttle related options */
282
- // readonly deferOptions
283
281
  this.initialized = false;
284
282
  this.subscriptions = {
285
283
  subject: void 0,
@@ -337,9 +335,9 @@ var DataStorage = class {
337
335
  if (!(0, import_core6.isMap)(data)) return this.subject.next(/* @__PURE__ */ new Map());
338
336
  this.write(data);
339
337
  (0, import_core6.fallbackIfFails)(
340
- async () => {
338
+ () => {
341
339
  var _a2;
342
- return await ((_a2 = this.onChange) == null ? void 0 : _a2.call(this, data));
340
+ return (_a2 = this.onChange) == null ? void 0 : _a2.call(this, data);
343
341
  },
344
342
  [],
345
343
  this.triggerOnError("onChange")
@@ -409,12 +407,17 @@ var DataStorage = class {
409
407
  this.triggerOnError("stringify-json")
410
408
  );
411
409
  };
410
+ this.toObject = () => {
411
+ const obj = {};
412
+ for (const [key, value] of this.getAll()) obj[key] = value;
413
+ return obj;
414
+ };
412
415
  this.toString = (data) => this.toJSON(void 0, void 0, data);
413
416
  this.triggerOnError = (type) => (err) => {
414
417
  this.onError && (0, import_core6.fallbackIfFails)(this.onError, [err, type], "");
415
418
  };
416
419
  this.unsubscribe = () => unsubscribeAll_default(this.subscriptions);
417
- this.values = () => [...this.getAll().values()];
420
+ this.values = () => (0, import_core6.getValues)(this.getAll());
418
421
  this.write = (data) => {
419
422
  try {
420
423
  !this.initialized && this.init();
package/dist/index.d.cts CHANGED
@@ -102,7 +102,7 @@ type ValueModifier<T = unknown, TCopy = T> = (newValue: T, previousValue: TCopy
102
102
  * null,
103
103
  * // copy and transform the value from number$
104
104
  * newValue => newValue % 2 === 0,
105
- * // debounce/throttle value changes to even$t
105
+ * // debounce/throttle value changes to even$
106
106
  * // {
107
107
  * // delay: 300 //
108
108
  * // throttle: false,
@@ -160,7 +160,7 @@ type StorageMap<K, V extends StorageValue> = <T>(callback: (value: V, key: K, da
160
160
  type StorageOnChangeFn<K, V extends StorageValue> = (data: Map<K, V>) => ValueOrPromise<void | Map<K, V>>;
161
161
  type StorageOnErrorFn = (err: unknown, type: OnErrorType) => ValueOrPromise<void>;
162
162
  /** Initial options provided through the constructor */
163
- type StorageOptions<Key, Value extends StorageValue, CacheDisabled extends boolean = false> = {
163
+ type StorageOptions<Key extends StorageKey, Value extends StorageValue, CacheDisabled extends boolean = false> = {
164
164
  /** value to set, only if storage is empty. Default: `new Map()` */
165
165
  initialValue?: Map<Key, Value>;
166
166
  } & Pick<Partial<IDataStorage<Key, Value, CacheDisabled>>, 'cacheDisabled' | 'onChange' | 'onError' | 'parse' | 'spaces' | 'storage' | 'stringify'> & (CacheDisabled extends false ? Pick<Partial<IDataStorage<Key, Value, CacheDisabled>>, 'delay' | 'delayOptions'> : {
@@ -184,8 +184,9 @@ type StorageSortOptions = SortOptions & {
184
184
  };
185
185
  type StorageStringifyFn<K, V extends StorageValue> = (data: Map<K, V>) => string;
186
186
  type StorageToJSON<K, V> = (replacer?: null | ((key: K, value: V) => unknown), spacing?: string | number, data?: Map<K, V>) => string;
187
- type StorageValue = Record<PropertyKey, unknown>;
188
- interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends boolean = false> {
187
+ type StorageKey = string;
188
+ type StorageValue = Record<StorageKey, unknown>;
189
+ interface IDataStorage<Key extends StorageKey, Value extends StorageValue, CacheDisabled extends boolean = false> {
189
190
  /** Disable in-memory cache and only directly read/write from storage (local storage or JSON fle) */
190
191
  readonly cacheDisabled: CacheDisabled;
191
192
  /**
@@ -227,6 +228,15 @@ interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends bo
227
228
  * - node: `undefined` (in-memory mode)
228
229
  */
229
230
  readonly storage?: StorageCompact | null;
231
+ /**
232
+ * The underlying RxJS Subject that serves as the primary reactive interface for observing data modifications.
233
+ *
234
+ * Its implementation type is determined by the caching strategy:
235
+ * - **BehaviorSubject**: Used when caching is enabled.
236
+ * It maintains the current state and emits it immediately to new subscribers.
237
+ * - **Subject**: Used when caching is disabled.
238
+ * It acts as a pure event pipe, emitting updates only at the moment they occur without retaining an in-memory copy.
239
+ */
230
240
  readonly subject: CacheDisabled extends true ? Subject<Map<Key, Value>> : BehaviorSubject<Map<Key, Value>>;
231
241
  /** Clear all items */
232
242
  readonly clear: () => IDataStorage<Key, Value, CacheDisabled>;
@@ -346,6 +356,8 @@ interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends bo
346
356
  readonly toArray: () => [Key, Value][];
347
357
  /** Convert list of items (Map) to JSON string of 2D Array */
348
358
  readonly toJSON: StorageToJSON<Key, Value>;
359
+ /** Convert list of items into an object */
360
+ readonly toObject: () => Record<Key, Value>;
349
361
  /** Convert list of items (Map) to JSON string of 2D Array */
350
362
  readonly toString: () => string;
351
363
  /**
@@ -357,7 +369,7 @@ interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends bo
357
369
  * - The instance stopping listening to force update cache triggers.
358
370
  */
359
371
  readonly unsubscribe: () => void;
360
- /** Get all values */
372
+ /** Get all values as an array */
361
373
  readonly values: () => Value[];
362
374
  /**
363
375
  * Write data to the underlying storage (localStorage or file).
@@ -391,11 +403,11 @@ interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends bo
391
403
  * ```
392
404
  */
393
405
  declare const forceUpdateCache$: Subject<string | boolean | string[]>;
394
- declare class DataStorage<Key, Value extends StorageValue, CacheDisabled extends boolean = false> implements IDataStorage<Key, Value, CacheDisabled> {
406
+ declare class DataStorage<Key extends StorageKey, Value extends StorageValue, CacheDisabled extends boolean = false> implements IDataStorage<Key, Value, CacheDisabled> {
395
407
  readonly cacheDisabled: CacheDisabled;
396
408
  readonly delay: number;
397
- readonly delayOptions?: DelayOptions;
398
409
  /** Debounce and throttle related options */
410
+ readonly delayOptions?: DelayOptions;
399
411
  readonly initialized: boolean;
400
412
  readonly name: string;
401
413
  get size(): number;
@@ -533,6 +545,7 @@ declare class DataStorage<Key, Value extends StorageValue, CacheDisabled extends
533
545
  readonly stringify?: StorageStringifyFn<Key, Value>;
534
546
  readonly toArray: () => [Key, Value][];
535
547
  readonly toJSON: StorageToJSON<Key, Value>;
548
+ readonly toObject: () => Record<Key, Value>;
536
549
  readonly toString: (data?: Map<Key, Value>) => string;
537
550
  private triggerOnError;
538
551
  readonly unsubscribe: () => void;
@@ -727,4 +740,4 @@ declare const isSubscriptionLike: (value: unknown, strict?: boolean) => value is
727
740
  */
728
741
  declare const unsubscribeAll: (unsub?: UnsubscribeCandidates, onError?: (err: unknown) => void) => void;
729
742
 
730
- export { type CopyRxSubjectOptions, DataStorage, type DelayOptions, type IDataStorage, IGNORE_UPDATE_SYMBOL, IntervalRunner, IntervalSubject, type OnErrorType, type OnResultType, type StorageCompact, type StorageFilter, type StorageFind, type StorageMap, type StorageOnChangeFn, type StorageOnErrorFn, type StorageOptions, type StorageParseFn, type StorageSearch, type StorageSort, type StorageSortByComparator, type StorageSortByKey, type StorageSortByPropertyName, type StorageSortOptions, type StorageStringifyFn, type StorageToJSON, type StorageValue, type SubjectLike, type SubscriptionLike, type Unsubscribe, type UnsubscribeCandidates, type UnwrapSubjectValue, type ValueModifier, asPromise, copyRxSubject, forceUpdateCache$, isSubjectLike, isSubscriptionLike, type onBeforeExecType, unsubscribeAll };
743
+ export { type CopyRxSubjectOptions, DataStorage, type DelayOptions, type IDataStorage, IGNORE_UPDATE_SYMBOL, IntervalRunner, IntervalSubject, type OnErrorType, type OnResultType, type StorageCompact, type StorageFilter, type StorageFind, type StorageKey, type StorageMap, type StorageOnChangeFn, type StorageOnErrorFn, type StorageOptions, type StorageParseFn, type StorageSearch, type StorageSort, type StorageSortByComparator, type StorageSortByKey, type StorageSortByPropertyName, type StorageSortOptions, type StorageStringifyFn, type StorageToJSON, type StorageValue, type SubjectLike, type SubscriptionLike, type Unsubscribe, type UnsubscribeCandidates, type UnwrapSubjectValue, type ValueModifier, asPromise, copyRxSubject, forceUpdateCache$, isSubjectLike, isSubscriptionLike, type onBeforeExecType, unsubscribeAll };
package/dist/index.d.ts CHANGED
@@ -102,7 +102,7 @@ type ValueModifier<T = unknown, TCopy = T> = (newValue: T, previousValue: TCopy
102
102
  * null,
103
103
  * // copy and transform the value from number$
104
104
  * newValue => newValue % 2 === 0,
105
- * // debounce/throttle value changes to even$t
105
+ * // debounce/throttle value changes to even$
106
106
  * // {
107
107
  * // delay: 300 //
108
108
  * // throttle: false,
@@ -160,7 +160,7 @@ type StorageMap<K, V extends StorageValue> = <T>(callback: (value: V, key: K, da
160
160
  type StorageOnChangeFn<K, V extends StorageValue> = (data: Map<K, V>) => ValueOrPromise<void | Map<K, V>>;
161
161
  type StorageOnErrorFn = (err: unknown, type: OnErrorType) => ValueOrPromise<void>;
162
162
  /** Initial options provided through the constructor */
163
- type StorageOptions<Key, Value extends StorageValue, CacheDisabled extends boolean = false> = {
163
+ type StorageOptions<Key extends StorageKey, Value extends StorageValue, CacheDisabled extends boolean = false> = {
164
164
  /** value to set, only if storage is empty. Default: `new Map()` */
165
165
  initialValue?: Map<Key, Value>;
166
166
  } & Pick<Partial<IDataStorage<Key, Value, CacheDisabled>>, 'cacheDisabled' | 'onChange' | 'onError' | 'parse' | 'spaces' | 'storage' | 'stringify'> & (CacheDisabled extends false ? Pick<Partial<IDataStorage<Key, Value, CacheDisabled>>, 'delay' | 'delayOptions'> : {
@@ -184,8 +184,9 @@ type StorageSortOptions = SortOptions & {
184
184
  };
185
185
  type StorageStringifyFn<K, V extends StorageValue> = (data: Map<K, V>) => string;
186
186
  type StorageToJSON<K, V> = (replacer?: null | ((key: K, value: V) => unknown), spacing?: string | number, data?: Map<K, V>) => string;
187
- type StorageValue = Record<PropertyKey, unknown>;
188
- interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends boolean = false> {
187
+ type StorageKey = string;
188
+ type StorageValue = Record<StorageKey, unknown>;
189
+ interface IDataStorage<Key extends StorageKey, Value extends StorageValue, CacheDisabled extends boolean = false> {
189
190
  /** Disable in-memory cache and only directly read/write from storage (local storage or JSON fle) */
190
191
  readonly cacheDisabled: CacheDisabled;
191
192
  /**
@@ -227,6 +228,15 @@ interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends bo
227
228
  * - node: `undefined` (in-memory mode)
228
229
  */
229
230
  readonly storage?: StorageCompact | null;
231
+ /**
232
+ * The underlying RxJS Subject that serves as the primary reactive interface for observing data modifications.
233
+ *
234
+ * Its implementation type is determined by the caching strategy:
235
+ * - **BehaviorSubject**: Used when caching is enabled.
236
+ * It maintains the current state and emits it immediately to new subscribers.
237
+ * - **Subject**: Used when caching is disabled.
238
+ * It acts as a pure event pipe, emitting updates only at the moment they occur without retaining an in-memory copy.
239
+ */
230
240
  readonly subject: CacheDisabled extends true ? Subject<Map<Key, Value>> : BehaviorSubject<Map<Key, Value>>;
231
241
  /** Clear all items */
232
242
  readonly clear: () => IDataStorage<Key, Value, CacheDisabled>;
@@ -346,6 +356,8 @@ interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends bo
346
356
  readonly toArray: () => [Key, Value][];
347
357
  /** Convert list of items (Map) to JSON string of 2D Array */
348
358
  readonly toJSON: StorageToJSON<Key, Value>;
359
+ /** Convert list of items into an object */
360
+ readonly toObject: () => Record<Key, Value>;
349
361
  /** Convert list of items (Map) to JSON string of 2D Array */
350
362
  readonly toString: () => string;
351
363
  /**
@@ -357,7 +369,7 @@ interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends bo
357
369
  * - The instance stopping listening to force update cache triggers.
358
370
  */
359
371
  readonly unsubscribe: () => void;
360
- /** Get all values */
372
+ /** Get all values as an array */
361
373
  readonly values: () => Value[];
362
374
  /**
363
375
  * Write data to the underlying storage (localStorage or file).
@@ -391,11 +403,11 @@ interface IDataStorage<Key, Value extends StorageValue, CacheDisabled extends bo
391
403
  * ```
392
404
  */
393
405
  declare const forceUpdateCache$: Subject<string | boolean | string[]>;
394
- declare class DataStorage<Key, Value extends StorageValue, CacheDisabled extends boolean = false> implements IDataStorage<Key, Value, CacheDisabled> {
406
+ declare class DataStorage<Key extends StorageKey, Value extends StorageValue, CacheDisabled extends boolean = false> implements IDataStorage<Key, Value, CacheDisabled> {
395
407
  readonly cacheDisabled: CacheDisabled;
396
408
  readonly delay: number;
397
- readonly delayOptions?: DelayOptions;
398
409
  /** Debounce and throttle related options */
410
+ readonly delayOptions?: DelayOptions;
399
411
  readonly initialized: boolean;
400
412
  readonly name: string;
401
413
  get size(): number;
@@ -533,6 +545,7 @@ declare class DataStorage<Key, Value extends StorageValue, CacheDisabled extends
533
545
  readonly stringify?: StorageStringifyFn<Key, Value>;
534
546
  readonly toArray: () => [Key, Value][];
535
547
  readonly toJSON: StorageToJSON<Key, Value>;
548
+ readonly toObject: () => Record<Key, Value>;
536
549
  readonly toString: (data?: Map<Key, Value>) => string;
537
550
  private triggerOnError;
538
551
  readonly unsubscribe: () => void;
@@ -727,4 +740,4 @@ declare const isSubscriptionLike: (value: unknown, strict?: boolean) => value is
727
740
  */
728
741
  declare const unsubscribeAll: (unsub?: UnsubscribeCandidates, onError?: (err: unknown) => void) => void;
729
742
 
730
- export { type CopyRxSubjectOptions, DataStorage, type DelayOptions, type IDataStorage, IGNORE_UPDATE_SYMBOL, IntervalRunner, IntervalSubject, type OnErrorType, type OnResultType, type StorageCompact, type StorageFilter, type StorageFind, type StorageMap, type StorageOnChangeFn, type StorageOnErrorFn, type StorageOptions, type StorageParseFn, type StorageSearch, type StorageSort, type StorageSortByComparator, type StorageSortByKey, type StorageSortByPropertyName, type StorageSortOptions, type StorageStringifyFn, type StorageToJSON, type StorageValue, type SubjectLike, type SubscriptionLike, type Unsubscribe, type UnsubscribeCandidates, type UnwrapSubjectValue, type ValueModifier, asPromise, copyRxSubject, forceUpdateCache$, isSubjectLike, isSubscriptionLike, type onBeforeExecType, unsubscribeAll };
743
+ export { type CopyRxSubjectOptions, DataStorage, type DelayOptions, type IDataStorage, IGNORE_UPDATE_SYMBOL, IntervalRunner, IntervalSubject, type OnErrorType, type OnResultType, type StorageCompact, type StorageFilter, type StorageFind, type StorageKey, type StorageMap, type StorageOnChangeFn, type StorageOnErrorFn, type StorageOptions, type StorageParseFn, type StorageSearch, type StorageSort, type StorageSortByComparator, type StorageSortByKey, type StorageSortByPropertyName, type StorageSortOptions, type StorageStringifyFn, type StorageToJSON, type StorageValue, type SubjectLike, type SubscriptionLike, type Unsubscribe, type UnsubscribeCandidates, type UnwrapSubjectValue, type ValueModifier, asPromise, copyRxSubject, forceUpdateCache$, isSubjectLike, isSubscriptionLike, type onBeforeExecType, unsubscribeAll };
package/dist/index.js CHANGED
@@ -164,6 +164,7 @@ import {
164
164
  find,
165
165
  getEntries,
166
166
  getKeys,
167
+ getValues,
167
168
  isArr as isArr2,
168
169
  isFn as isFn5,
169
170
  isMap,
@@ -264,8 +265,6 @@ var DataStorage = class {
264
265
  * ```
265
266
  */
266
267
  constructor(name, options) {
267
- /** Debounce and throttle related options */
268
- // readonly deferOptions
269
268
  this.initialized = false;
270
269
  this.subscriptions = {
271
270
  subject: void 0,
@@ -323,9 +322,9 @@ var DataStorage = class {
323
322
  if (!isMap(data)) return this.subject.next(/* @__PURE__ */ new Map());
324
323
  this.write(data);
325
324
  fallbackIfFails3(
326
- async () => {
325
+ () => {
327
326
  var _a2;
328
- return await ((_a2 = this.onChange) == null ? void 0 : _a2.call(this, data));
327
+ return (_a2 = this.onChange) == null ? void 0 : _a2.call(this, data);
329
328
  },
330
329
  [],
331
330
  this.triggerOnError("onChange")
@@ -395,12 +394,17 @@ var DataStorage = class {
395
394
  this.triggerOnError("stringify-json")
396
395
  );
397
396
  };
397
+ this.toObject = () => {
398
+ const obj = {};
399
+ for (const [key, value] of this.getAll()) obj[key] = value;
400
+ return obj;
401
+ };
398
402
  this.toString = (data) => this.toJSON(void 0, void 0, data);
399
403
  this.triggerOnError = (type) => (err) => {
400
404
  this.onError && fallbackIfFails3(this.onError, [err, type], "");
401
405
  };
402
406
  this.unsubscribe = () => unsubscribeAll_default(this.subscriptions);
403
- this.values = () => [...this.getAll().values()];
407
+ this.values = () => getValues(this.getAll());
404
408
  this.write = (data) => {
405
409
  try {
406
410
  !this.initialized && this.init();
package/package.json CHANGED
@@ -49,5 +49,6 @@
49
49
  "module": "./dist/index.js",
50
50
  "type": "module",
51
51
  "types": "./dist/index.d.ts",
52
- "version": "0.1.1"
52
+ "version": "0.1.2",
53
+ "gitHead": "09d571c44fb7f4d785916d2fd5113077827b73a1"
53
54
  }