@asaidimu/utils-remote-store 1.2.9 → 1.3.1

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.
Files changed (4) hide show
  1. package/README.md +921 -567
  2. package/index.d.mts +25 -72
  3. package/index.d.ts +25 -72
  4. package/package.json +2 -2
package/index.d.mts CHANGED
@@ -1,51 +1,4 @@
1
- interface SimplePersistence<T> {
2
- /**
3
- * Persists data to storage.
4
- *
5
- * @param id The **unique identifier of the *consumer instance*** making the change. This is NOT the ID of the data (`T`) itself.
6
- * Think of it as the ID of the specific browser tab, component, or module that's currently interacting with the persistence layer.
7
- * It should typically be a **UUID** generated once at the consumer instance's instantiation.
8
- * This `id` is crucial for the `subscribe` method, helping to differentiate updates originating from the current instance versus other instances/tabs, thereby preventing self-triggered notification loops.
9
- * @param state The state (of type T) to persist. This state is generally considered the **global or shared state** that all instances interact with.
10
- * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations (like `IndexedDBPersistence`), this returns a `Promise<boolean>`.
11
- */
12
- set(id: string, state: T): boolean | Promise<boolean>;
13
- /**
14
- * Retrieves the global persisted data from storage.
15
- *
16
- * @returns The retrieved state of type `T`, or `null` if no data is found or if an error occurs during retrieval/parsing.
17
- * For asynchronous implementations, this returns a `Promise<T | null>`.
18
- */
19
- get(): (T | null) | Promise<T | null>;
20
- /**
21
- * Subscribes to changes in the global persisted data that originate from *other* instances of your application (e.g., other tabs or independent components using the same persistence layer).
22
- *
23
- * @param id The **unique identifier of the *consumer instance* subscribing**. This allows the persistence implementation to filter out notifications that were initiated by the subscribing instance itself.
24
- * @param callback The function to call when the global persisted data changes from *another* source. The new state (`T`) is passed as an argument to this callback.
25
- * @returns A function that, when called, will unsubscribe the provided callback from future updates. Call this when your component or instance is no longer active to prevent memory leaks.
26
- */
27
- subscribe(id: string, callback: (state: T) => void): () => void;
28
- /**
29
- * Clears (removes) the entire global persisted data from storage.
30
- *
31
- * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations, this returns a `Promise<boolean>`.
32
- */
33
- clear(): boolean | Promise<boolean>;
34
- /**
35
- * Returns metadata about the persistence layer.
36
- *
37
- * This is useful for distinguishing between multiple apps running on the same host
38
- * (e.g., several apps served at `localhost:3000` that share the same storage key).
39
- *
40
- * @returns An object containing:
41
- * - `version`: The semantic version string of the persistence schema or application.
42
- * - `id`: A unique identifier for the application using this persistence instance.
43
- */
44
- stats(): {
45
- version: string;
46
- id: string;
47
- };
48
- }
1
+ import { SimplePersistence } from '@core/persistence';
49
2
 
50
3
  interface CacheOptions {
51
4
  staleTime?: number;
@@ -85,51 +38,51 @@ type CacheEventBase<Type extends string, Payload = {}> = {
85
38
  key: string;
86
39
  timestamp: number;
87
40
  } & Payload;
88
- type CacheReadHitEvent<T = any> = CacheEventBase<'cache:read:hit', {
41
+ type CacheReadHitEvent<T = any> = CacheEventBase<"cache:read:hit", {
89
42
  data: T;
90
43
  isStale: boolean;
91
44
  }>;
92
- type CacheReadMissEvent = CacheEventBase<'cache:read:miss'>;
93
- type CacheFetchStartEvent = CacheEventBase<'cache:fetch:start', {
45
+ type CacheReadMissEvent = CacheEventBase<"cache:read:miss">;
46
+ type CacheFetchStartEvent = CacheEventBase<"cache:fetch:start", {
94
47
  attempt: number;
95
48
  }>;
96
- type CacheFetchSuccessEvent<T = any> = CacheEventBase<'cache:fetch:success', {
49
+ type CacheFetchSuccessEvent<T = any> = CacheEventBase<"cache:fetch:success", {
97
50
  data: T;
98
51
  }>;
99
- type CacheFetchErrorEvent = CacheEventBase<'cache:fetch:error', {
52
+ type CacheFetchErrorEvent = CacheEventBase<"cache:fetch:error", {
100
53
  error: Error;
101
54
  attempt: number;
102
55
  }>;
103
- type CacheDataEvictEvent = CacheEventBase<'cache:data:evict', {
56
+ type CacheDataEvictEvent = CacheEventBase<"cache:data:evict", {
104
57
  reason?: string;
105
58
  }>;
106
- type CacheDataInvalidateEvent = CacheEventBase<'cache:data:invalidate'>;
107
- type CacheDataSetEvent<T = any> = CacheEventBase<'cache:data:set', {
59
+ type CacheDataInvalidateEvent = CacheEventBase<"cache:data:invalidate">;
60
+ type CacheDataSetEvent<T = any> = CacheEventBase<"cache:data:set", {
108
61
  newData: T;
109
62
  oldData?: T;
110
63
  }>;
111
- type CachePersistenceLoadSuccessEvent = CacheEventBase<'cache:persistence:load:success', {
64
+ type CachePersistenceLoadSuccessEvent = CacheEventBase<"cache:persistence:load:success", {
112
65
  message?: string;
113
66
  }>;
114
- type CachePersistenceLoadErrorEvent = CacheEventBase<'cache:persistence:load:error', {
67
+ type CachePersistenceLoadErrorEvent = CacheEventBase<"cache:persistence:load:error", {
115
68
  message?: string;
116
69
  error?: any;
117
70
  }>;
118
- type CachePersistenceSaveSuccessEvent = CacheEventBase<'cache:persistence:save:success'>;
119
- type CachePersistenceSaveErrorEvent = CacheEventBase<'cache:persistence:save:error', {
71
+ type CachePersistenceSaveSuccessEvent = CacheEventBase<"cache:persistence:save:success">;
72
+ type CachePersistenceSaveErrorEvent = CacheEventBase<"cache:persistence:save:error", {
120
73
  message?: string;
121
74
  error?: any;
122
75
  }>;
123
- type CachePersistenceClearSuccessEvent = CacheEventBase<'cache:persistence:clear:success'>;
124
- type CachePersistenceClearErrorEvent = CacheEventBase<'cache:persistence:clear:error', {
76
+ type CachePersistenceClearSuccessEvent = CacheEventBase<"cache:persistence:clear:success">;
77
+ type CachePersistenceClearErrorEvent = CacheEventBase<"cache:persistence:clear:error", {
125
78
  message?: string;
126
79
  error?: any;
127
80
  }>;
128
- type CachePersistenceSyncEvent = CacheEventBase<'cache:persistence:sync', {
81
+ type CachePersistenceSyncEvent = CacheEventBase<"cache:persistence:sync", {
129
82
  message?: string;
130
83
  }>;
131
84
  type CacheEvent = CacheReadHitEvent | CacheReadMissEvent | CacheFetchStartEvent | CacheFetchSuccessEvent | CacheFetchErrorEvent | CacheDataEvictEvent | CacheDataInvalidateEvent | CacheDataSetEvent | CachePersistenceLoadSuccessEvent | CachePersistenceLoadErrorEvent | CachePersistenceSaveSuccessEvent | CachePersistenceSaveErrorEvent | CachePersistenceClearSuccessEvent | CachePersistenceClearErrorEvent | CachePersistenceSyncEvent;
132
- type CacheEventType = CacheEvent['type'];
85
+ type CacheEventType = CacheEvent["type"];
133
86
 
134
87
  declare class QueryCache {
135
88
  private cache;
@@ -233,7 +186,7 @@ interface StoreEvent {
233
186
  /**
234
187
  * Defines the types of mutation operations that can occur.
235
188
  */
236
- type MutationOperation = 'create' | 'update' | 'delete' | 'upload';
189
+ type MutationOperation = "create" | "update" | "delete" | "upload";
237
190
  /**
238
191
  * A correlator function that determines which active queries should be invalidated
239
192
  * based on a mutation operation.
@@ -342,7 +295,7 @@ interface BaseStore<T extends StoreRecord, TFindOptions = Record<string, unknown
342
295
  /** A function to call to cancel the ongoing stream. */
343
296
  cancel: () => void;
344
297
  /** A getter function that returns the current status of the stream ('active', 'cancelled', or 'completed'). */
345
- status: () => 'active' | 'cancelled' | 'completed';
298
+ status: () => "active" | "cancelled" | "completed";
346
299
  };
347
300
  }
348
301
  /**
@@ -512,12 +465,12 @@ declare class ReactiveRemoteStore<T extends StoreRecord, TFindOptions = Record<s
512
465
  file: File;
513
466
  options?: TUploadOptions;
514
467
  }): Promise<T | undefined>;
515
- refresh(operation: 'read', params: TReadOptions): Promise<T | undefined>;
516
- refresh(operation: 'list', params: TListOptions): Promise<Page<T> | undefined>;
517
- refresh(operation: 'find', query: TFindOptions): Promise<Page<T> | undefined>;
518
- prefetch(operation: 'read', params: TReadOptions): void;
519
- prefetch(operation: 'list', params: TListOptions): void;
520
- prefetch(operation: 'find', params: TFindOptions): void;
468
+ refresh(operation: "read", params: TReadOptions): Promise<T | undefined>;
469
+ refresh(operation: "list", params: TListOptions): Promise<Page<T> | undefined>;
470
+ refresh(operation: "find", query: TFindOptions): Promise<Page<T> | undefined>;
471
+ prefetch(operation: "read", params: TReadOptions): void;
472
+ prefetch(operation: "list", params: TListOptions): void;
473
+ prefetch(operation: "find", params: TFindOptions): void;
521
474
  invalidate(operation: string, params: any): Promise<void>;
522
475
  invalidateAll(): Promise<void>;
523
476
  getStats(): {
package/index.d.ts CHANGED
@@ -1,51 +1,4 @@
1
- interface SimplePersistence<T> {
2
- /**
3
- * Persists data to storage.
4
- *
5
- * @param id The **unique identifier of the *consumer instance*** making the change. This is NOT the ID of the data (`T`) itself.
6
- * Think of it as the ID of the specific browser tab, component, or module that's currently interacting with the persistence layer.
7
- * It should typically be a **UUID** generated once at the consumer instance's instantiation.
8
- * This `id` is crucial for the `subscribe` method, helping to differentiate updates originating from the current instance versus other instances/tabs, thereby preventing self-triggered notification loops.
9
- * @param state The state (of type T) to persist. This state is generally considered the **global or shared state** that all instances interact with.
10
- * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations (like `IndexedDBPersistence`), this returns a `Promise<boolean>`.
11
- */
12
- set(id: string, state: T): boolean | Promise<boolean>;
13
- /**
14
- * Retrieves the global persisted data from storage.
15
- *
16
- * @returns The retrieved state of type `T`, or `null` if no data is found or if an error occurs during retrieval/parsing.
17
- * For asynchronous implementations, this returns a `Promise<T | null>`.
18
- */
19
- get(): (T | null) | Promise<T | null>;
20
- /**
21
- * Subscribes to changes in the global persisted data that originate from *other* instances of your application (e.g., other tabs or independent components using the same persistence layer).
22
- *
23
- * @param id The **unique identifier of the *consumer instance* subscribing**. This allows the persistence implementation to filter out notifications that were initiated by the subscribing instance itself.
24
- * @param callback The function to call when the global persisted data changes from *another* source. The new state (`T`) is passed as an argument to this callback.
25
- * @returns A function that, when called, will unsubscribe the provided callback from future updates. Call this when your component or instance is no longer active to prevent memory leaks.
26
- */
27
- subscribe(id: string, callback: (state: T) => void): () => void;
28
- /**
29
- * Clears (removes) the entire global persisted data from storage.
30
- *
31
- * @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations, this returns a `Promise<boolean>`.
32
- */
33
- clear(): boolean | Promise<boolean>;
34
- /**
35
- * Returns metadata about the persistence layer.
36
- *
37
- * This is useful for distinguishing between multiple apps running on the same host
38
- * (e.g., several apps served at `localhost:3000` that share the same storage key).
39
- *
40
- * @returns An object containing:
41
- * - `version`: The semantic version string of the persistence schema or application.
42
- * - `id`: A unique identifier for the application using this persistence instance.
43
- */
44
- stats(): {
45
- version: string;
46
- id: string;
47
- };
48
- }
1
+ import { SimplePersistence } from '@core/persistence';
49
2
 
50
3
  interface CacheOptions {
51
4
  staleTime?: number;
@@ -85,51 +38,51 @@ type CacheEventBase<Type extends string, Payload = {}> = {
85
38
  key: string;
86
39
  timestamp: number;
87
40
  } & Payload;
88
- type CacheReadHitEvent<T = any> = CacheEventBase<'cache:read:hit', {
41
+ type CacheReadHitEvent<T = any> = CacheEventBase<"cache:read:hit", {
89
42
  data: T;
90
43
  isStale: boolean;
91
44
  }>;
92
- type CacheReadMissEvent = CacheEventBase<'cache:read:miss'>;
93
- type CacheFetchStartEvent = CacheEventBase<'cache:fetch:start', {
45
+ type CacheReadMissEvent = CacheEventBase<"cache:read:miss">;
46
+ type CacheFetchStartEvent = CacheEventBase<"cache:fetch:start", {
94
47
  attempt: number;
95
48
  }>;
96
- type CacheFetchSuccessEvent<T = any> = CacheEventBase<'cache:fetch:success', {
49
+ type CacheFetchSuccessEvent<T = any> = CacheEventBase<"cache:fetch:success", {
97
50
  data: T;
98
51
  }>;
99
- type CacheFetchErrorEvent = CacheEventBase<'cache:fetch:error', {
52
+ type CacheFetchErrorEvent = CacheEventBase<"cache:fetch:error", {
100
53
  error: Error;
101
54
  attempt: number;
102
55
  }>;
103
- type CacheDataEvictEvent = CacheEventBase<'cache:data:evict', {
56
+ type CacheDataEvictEvent = CacheEventBase<"cache:data:evict", {
104
57
  reason?: string;
105
58
  }>;
106
- type CacheDataInvalidateEvent = CacheEventBase<'cache:data:invalidate'>;
107
- type CacheDataSetEvent<T = any> = CacheEventBase<'cache:data:set', {
59
+ type CacheDataInvalidateEvent = CacheEventBase<"cache:data:invalidate">;
60
+ type CacheDataSetEvent<T = any> = CacheEventBase<"cache:data:set", {
108
61
  newData: T;
109
62
  oldData?: T;
110
63
  }>;
111
- type CachePersistenceLoadSuccessEvent = CacheEventBase<'cache:persistence:load:success', {
64
+ type CachePersistenceLoadSuccessEvent = CacheEventBase<"cache:persistence:load:success", {
112
65
  message?: string;
113
66
  }>;
114
- type CachePersistenceLoadErrorEvent = CacheEventBase<'cache:persistence:load:error', {
67
+ type CachePersistenceLoadErrorEvent = CacheEventBase<"cache:persistence:load:error", {
115
68
  message?: string;
116
69
  error?: any;
117
70
  }>;
118
- type CachePersistenceSaveSuccessEvent = CacheEventBase<'cache:persistence:save:success'>;
119
- type CachePersistenceSaveErrorEvent = CacheEventBase<'cache:persistence:save:error', {
71
+ type CachePersistenceSaveSuccessEvent = CacheEventBase<"cache:persistence:save:success">;
72
+ type CachePersistenceSaveErrorEvent = CacheEventBase<"cache:persistence:save:error", {
120
73
  message?: string;
121
74
  error?: any;
122
75
  }>;
123
- type CachePersistenceClearSuccessEvent = CacheEventBase<'cache:persistence:clear:success'>;
124
- type CachePersistenceClearErrorEvent = CacheEventBase<'cache:persistence:clear:error', {
76
+ type CachePersistenceClearSuccessEvent = CacheEventBase<"cache:persistence:clear:success">;
77
+ type CachePersistenceClearErrorEvent = CacheEventBase<"cache:persistence:clear:error", {
125
78
  message?: string;
126
79
  error?: any;
127
80
  }>;
128
- type CachePersistenceSyncEvent = CacheEventBase<'cache:persistence:sync', {
81
+ type CachePersistenceSyncEvent = CacheEventBase<"cache:persistence:sync", {
129
82
  message?: string;
130
83
  }>;
131
84
  type CacheEvent = CacheReadHitEvent | CacheReadMissEvent | CacheFetchStartEvent | CacheFetchSuccessEvent | CacheFetchErrorEvent | CacheDataEvictEvent | CacheDataInvalidateEvent | CacheDataSetEvent | CachePersistenceLoadSuccessEvent | CachePersistenceLoadErrorEvent | CachePersistenceSaveSuccessEvent | CachePersistenceSaveErrorEvent | CachePersistenceClearSuccessEvent | CachePersistenceClearErrorEvent | CachePersistenceSyncEvent;
132
- type CacheEventType = CacheEvent['type'];
85
+ type CacheEventType = CacheEvent["type"];
133
86
 
134
87
  declare class QueryCache {
135
88
  private cache;
@@ -233,7 +186,7 @@ interface StoreEvent {
233
186
  /**
234
187
  * Defines the types of mutation operations that can occur.
235
188
  */
236
- type MutationOperation = 'create' | 'update' | 'delete' | 'upload';
189
+ type MutationOperation = "create" | "update" | "delete" | "upload";
237
190
  /**
238
191
  * A correlator function that determines which active queries should be invalidated
239
192
  * based on a mutation operation.
@@ -342,7 +295,7 @@ interface BaseStore<T extends StoreRecord, TFindOptions = Record<string, unknown
342
295
  /** A function to call to cancel the ongoing stream. */
343
296
  cancel: () => void;
344
297
  /** A getter function that returns the current status of the stream ('active', 'cancelled', or 'completed'). */
345
- status: () => 'active' | 'cancelled' | 'completed';
298
+ status: () => "active" | "cancelled" | "completed";
346
299
  };
347
300
  }
348
301
  /**
@@ -512,12 +465,12 @@ declare class ReactiveRemoteStore<T extends StoreRecord, TFindOptions = Record<s
512
465
  file: File;
513
466
  options?: TUploadOptions;
514
467
  }): Promise<T | undefined>;
515
- refresh(operation: 'read', params: TReadOptions): Promise<T | undefined>;
516
- refresh(operation: 'list', params: TListOptions): Promise<Page<T> | undefined>;
517
- refresh(operation: 'find', query: TFindOptions): Promise<Page<T> | undefined>;
518
- prefetch(operation: 'read', params: TReadOptions): void;
519
- prefetch(operation: 'list', params: TListOptions): void;
520
- prefetch(operation: 'find', params: TFindOptions): void;
468
+ refresh(operation: "read", params: TReadOptions): Promise<T | undefined>;
469
+ refresh(operation: "list", params: TListOptions): Promise<Page<T> | undefined>;
470
+ refresh(operation: "find", query: TFindOptions): Promise<Page<T> | undefined>;
471
+ prefetch(operation: "read", params: TReadOptions): void;
472
+ prefetch(operation: "list", params: TListOptions): void;
473
+ prefetch(operation: "find", params: TFindOptions): void;
521
474
  invalidate(operation: string, params: any): Promise<void>;
522
475
  invalidateAll(): Promise<void>;
523
476
  getStats(): {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@asaidimu/utils-remote-store",
3
- "version": "1.2.9",
3
+ "version": "1.3.1",
4
4
  "description": "A reactive store for remote data, built on top of @asaidimu/utils-cache",
5
5
  "main": "index.js",
6
6
  "module": "index.mjs",
@@ -9,7 +9,7 @@
9
9
  "./*"
10
10
  ],
11
11
  "dependencies": {
12
- "@asaidimu/utils-cache": "3.0.5",
12
+ "@asaidimu/utils-cache": "3.1.0",
13
13
  "eventsource": "^4.0.0"
14
14
  },
15
15
  "exports": {