@asaidimu/utils-persistence 6.1.11 → 6.1.13
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/index.d.cts +304 -0
- package/index.d.ts +252 -248
- package/index.js +1 -1
- package/index.mjs +1 -1
- package/package.json +6 -4
- package/index.d.mts +0 -300
package/index.d.cts
ADDED
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
//#region src/persistence/types.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* configuration object for initializing a persistence store.
|
|
4
|
+
*
|
|
5
|
+
* this configuration allows you to:
|
|
6
|
+
* - define the **schema/application version** of the data being persisted.
|
|
7
|
+
* - distinguish between multiple **applications** that may run on the same host
|
|
8
|
+
* (so they don’t overwrite each other’s storage).
|
|
9
|
+
* - optionally provide an **upgrade handler** to migrate persisted data when
|
|
10
|
+
* the version changes.
|
|
11
|
+
*
|
|
12
|
+
* @template T the type of data being persisted.
|
|
13
|
+
*/
|
|
14
|
+
interface StoreConfig<T> {
|
|
15
|
+
/**
|
|
16
|
+
* the semantic version string (e.g., "1.0.0") of the data schema or application.
|
|
17
|
+
*
|
|
18
|
+
* this is used for version control and data migrations.
|
|
19
|
+
* when the persisted state’s version does not match the current version,
|
|
20
|
+
* the `onUpgrade` function will be invoked (if provided).
|
|
21
|
+
*/
|
|
22
|
+
version: string;
|
|
23
|
+
/**
|
|
24
|
+
* a unique application identifier (e.g., "chat-app" or "my-dashboard").
|
|
25
|
+
*
|
|
26
|
+
* this prevents collisions between different apps or modules
|
|
27
|
+
* that might share the same persistence backend (e.g., localstorage).
|
|
28
|
+
*
|
|
29
|
+
* example: if two apps both persist under the key "user", the `app`
|
|
30
|
+
* value ensures they can be distinguished.
|
|
31
|
+
*/
|
|
32
|
+
app: string;
|
|
33
|
+
/**
|
|
34
|
+
* optional handler for upgrading persisted state across versions.
|
|
35
|
+
*
|
|
36
|
+
* this function will be called when the persisted state’s `version`
|
|
37
|
+
* does not match the `version` specified in this config.
|
|
38
|
+
*
|
|
39
|
+
* @param state the existing state object, including:
|
|
40
|
+
* - `data`: the current persisted data (or null if none exists).
|
|
41
|
+
* - `version`: the version string stored with the data.
|
|
42
|
+
* - `app`: the application identifier stored with the data.
|
|
43
|
+
*
|
|
44
|
+
* @returns a new state object with:
|
|
45
|
+
* - `state`: the migrated data (or null if clearing/resetting).
|
|
46
|
+
* - `version`: the new version string (must match `config.version`).
|
|
47
|
+
*
|
|
48
|
+
* example:
|
|
49
|
+
* ```ts
|
|
50
|
+
* onupgrade: async ({ data, version, app }) => {
|
|
51
|
+
* if (version === "1.0.0") {
|
|
52
|
+
* // migrate from 1.0.0 to 2.0.0
|
|
53
|
+
* return { state: { ...data, newfield: "default" }, version: "2.0.0" };
|
|
54
|
+
* }
|
|
55
|
+
* return { state: data, version: "2.0.0" };
|
|
56
|
+
* }
|
|
57
|
+
* ```
|
|
58
|
+
*/
|
|
59
|
+
onUpgrade?: (state: {
|
|
60
|
+
data: T | null;
|
|
61
|
+
version: string;
|
|
62
|
+
app: string;
|
|
63
|
+
}) => Promise<{
|
|
64
|
+
state: T | null;
|
|
65
|
+
version: string;
|
|
66
|
+
}>;
|
|
67
|
+
/**
|
|
68
|
+
* enables or disables developer mode for the persistence store.
|
|
69
|
+
*
|
|
70
|
+
* when set to `true`, the store may provide additional logging,
|
|
71
|
+
* verbose error reporting, or bypass certain production constraints
|
|
72
|
+
* (such as strict cache validation) to aid in debugging.
|
|
73
|
+
*
|
|
74
|
+
* @default false
|
|
75
|
+
*/
|
|
76
|
+
dev?: boolean;
|
|
77
|
+
}
|
|
78
|
+
interface SimplePersistence<T> {
|
|
79
|
+
/**
|
|
80
|
+
* Persists data to storage.
|
|
81
|
+
*
|
|
82
|
+
* @param id The **unique identifier of the *consumer instance*** making the change. This is NOT the ID of the data (`T`) itself.
|
|
83
|
+
* Think of it as the ID of the specific browser tab, component, or module that's currently interacting with the persistence layer.
|
|
84
|
+
* It should typically be a **UUID** generated once at the consumer instance's instantiation.
|
|
85
|
+
* 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.
|
|
86
|
+
* @param state The state (of type T) to persist. This state is generally considered the **global or shared state** that all instances interact with.
|
|
87
|
+
* @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations (like `IndexedDBPersistence`), this returns a `Promise<boolean>`.
|
|
88
|
+
*/
|
|
89
|
+
set(id: string, state: T): boolean | Promise<boolean>;
|
|
90
|
+
/**
|
|
91
|
+
* Retrieves the global persisted data from storage.
|
|
92
|
+
*
|
|
93
|
+
* @returns The retrieved state of type `T`, or `null` if no data is found or if an error occurs during retrieval/parsing.
|
|
94
|
+
* For asynchronous implementations, this returns a `Promise<T | null>`.
|
|
95
|
+
*/
|
|
96
|
+
get(): (T | null) | Promise<T | null>;
|
|
97
|
+
/**
|
|
98
|
+
* 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).
|
|
99
|
+
*
|
|
100
|
+
* @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.
|
|
101
|
+
* @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.
|
|
102
|
+
* @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.
|
|
103
|
+
*/
|
|
104
|
+
subscribe(id: string, callback: (state: T) => void): () => void;
|
|
105
|
+
/**
|
|
106
|
+
* Clears (removes) the entire global persisted data from storage.
|
|
107
|
+
*
|
|
108
|
+
* @returns `true` if the operation was successful, `false` if an error occurred. For asynchronous implementations, this returns a `Promise<boolean>`.
|
|
109
|
+
*/
|
|
110
|
+
clear(): boolean | Promise<boolean>;
|
|
111
|
+
/**
|
|
112
|
+
* Returns metadata about the persistence layer.
|
|
113
|
+
*
|
|
114
|
+
* This is useful for distinguishing between multiple apps running on the same host
|
|
115
|
+
* (e.g., several apps served at `localhost:3000` that share the same storage key).
|
|
116
|
+
*
|
|
117
|
+
* @returns An object containing:
|
|
118
|
+
* - `version`: The semantic version string of the persistence schema or application.
|
|
119
|
+
* - `id`: A unique identifier for the application using this persistence instance.
|
|
120
|
+
*/
|
|
121
|
+
stats(): {
|
|
122
|
+
version: string;
|
|
123
|
+
id: string;
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
interface StorageEvents {
|
|
127
|
+
"store:updated": {
|
|
128
|
+
storageKey: string;
|
|
129
|
+
instanceId: string;
|
|
130
|
+
state: any;
|
|
131
|
+
timestamp?: number;
|
|
132
|
+
version: string;
|
|
133
|
+
app: string;
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
//#endregion
|
|
137
|
+
//#region src/persistence/webstorage.d.ts
|
|
138
|
+
/**
|
|
139
|
+
* Configuration options specific to the WebStoragePersistence adapter.
|
|
140
|
+
*/
|
|
141
|
+
interface WebStoragePersistenceConfig {
|
|
142
|
+
/**
|
|
143
|
+
* The key under which data is stored in web storage (e.g., 'user-profile').
|
|
144
|
+
*/
|
|
145
|
+
storageKey: string;
|
|
146
|
+
/**
|
|
147
|
+
* If true, uses sessionStorage instead of localStorage. Defaults to false.
|
|
148
|
+
*/
|
|
149
|
+
session?: boolean;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Implements SimplePersistence using web storage (localStorage or sessionStorage).
|
|
153
|
+
* Supports cross-tab synchronization and data versioning with upgrade handlers.
|
|
154
|
+
*/
|
|
155
|
+
declare class WebStoragePersistence<T> implements SimplePersistence<T> {
|
|
156
|
+
private readonly eventBus;
|
|
157
|
+
private readonly storage;
|
|
158
|
+
private readonly config;
|
|
159
|
+
private initialized;
|
|
160
|
+
private initializationCallbacks;
|
|
161
|
+
constructor(config: StoreConfig<T> & WebStoragePersistenceConfig);
|
|
162
|
+
private initialize;
|
|
163
|
+
private _onInitialized;
|
|
164
|
+
private initializeEventBus;
|
|
165
|
+
private getStoreName;
|
|
166
|
+
private setupStorageEventListener;
|
|
167
|
+
set(instanceId: string, state: T): boolean;
|
|
168
|
+
private _get;
|
|
169
|
+
get(): Promise<T | null>;
|
|
170
|
+
subscribe(instanceId: string, onStateChange: (state: T) => void): () => void;
|
|
171
|
+
clear(): boolean;
|
|
172
|
+
stats(): {
|
|
173
|
+
version: string;
|
|
174
|
+
id: string;
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
//#endregion
|
|
178
|
+
//#region src/persistence/indexedb.d.ts
|
|
179
|
+
interface IndexedDBPersistenceConfig {
|
|
180
|
+
store: string;
|
|
181
|
+
database: string;
|
|
182
|
+
collection: string;
|
|
183
|
+
enableTelemetry?: boolean;
|
|
184
|
+
}
|
|
185
|
+
declare class IndexedDBPersistence<T> implements SimplePersistence<T> {
|
|
186
|
+
private collection;
|
|
187
|
+
private collectionPromise;
|
|
188
|
+
private config;
|
|
189
|
+
private eventBus;
|
|
190
|
+
private initialized;
|
|
191
|
+
private _initializing;
|
|
192
|
+
private initializationCallbacks;
|
|
193
|
+
private doc;
|
|
194
|
+
private getStoreName;
|
|
195
|
+
constructor(config: StoreConfig<T> & IndexedDBPersistenceConfig);
|
|
196
|
+
private initialize;
|
|
197
|
+
private _onInitialized;
|
|
198
|
+
private getCollection;
|
|
199
|
+
/**
|
|
200
|
+
* Writes state. Waits for initialisation (migration) to complete first.
|
|
201
|
+
*/
|
|
202
|
+
set(id: string, state: T): Promise<boolean>;
|
|
203
|
+
private _read;
|
|
204
|
+
/**
|
|
205
|
+
* Returns the stored document data (after ensuring it has been
|
|
206
|
+
* fully read from the underlying store). The `document.read()`
|
|
207
|
+
* call populates internal state and we then return the document
|
|
208
|
+
* itself which has all fields available.
|
|
209
|
+
*/
|
|
210
|
+
private _get;
|
|
211
|
+
get(): Promise<T | null>;
|
|
212
|
+
subscribe(id: string, callback: (state: T) => void): () => void;
|
|
213
|
+
clear(): Promise<boolean>;
|
|
214
|
+
stats(): {
|
|
215
|
+
version: string;
|
|
216
|
+
id: string;
|
|
217
|
+
};
|
|
218
|
+
/**
|
|
219
|
+
* Closes this persistence instance, releasing our reference on the
|
|
220
|
+
* shared database. The actual database connection is closed only
|
|
221
|
+
* when every active store has been released.
|
|
222
|
+
*/
|
|
223
|
+
close(): Promise<void>;
|
|
224
|
+
}
|
|
225
|
+
//#endregion
|
|
226
|
+
//#region src/persistence/ephemeral.d.ts
|
|
227
|
+
/**
|
|
228
|
+
* Implements SimplePersistence using an in-memory store.
|
|
229
|
+
* Data is NOT persisted across page loads or application restarts.
|
|
230
|
+
*
|
|
231
|
+
* Synchronization: This class fully synchronizes its in-memory state across
|
|
232
|
+
* different browser tabs using an event bus with a Last Write Wins (LWW) strategy.
|
|
233
|
+
* When a state is set or cleared in one tab, the latest version (based on timestamp)
|
|
234
|
+
* will propagate to and update the in-memory state of all other tabs.
|
|
235
|
+
*
|
|
236
|
+
* IMPORTANT: While the in-memory state is synchronized *across tabs*, it remains
|
|
237
|
+
* ephemeral within each tab. Closing all tabs will result in data loss.
|
|
238
|
+
*
|
|
239
|
+
* Note: The best use case for this class is in tests where intergration with a
|
|
240
|
+
* SimplePersistence is required.
|
|
241
|
+
*/
|
|
242
|
+
declare class EphemeralPersistence<T> implements SimplePersistence<T> {
|
|
243
|
+
private readonly eventBus;
|
|
244
|
+
private inMemoryState;
|
|
245
|
+
private readonly config;
|
|
246
|
+
/**
|
|
247
|
+
* Initializes a new instance of EphemeralPersistence.
|
|
248
|
+
* @param config Configuration object for the persistence adapter.
|
|
249
|
+
*/
|
|
250
|
+
constructor(config: StoreConfig<T> & {
|
|
251
|
+
storageKey: string;
|
|
252
|
+
});
|
|
253
|
+
/**
|
|
254
|
+
* Initializes the event bus with configured settings for cross-tab communication.
|
|
255
|
+
* @returns Configured event bus instance.
|
|
256
|
+
*/
|
|
257
|
+
private initializeEventBus;
|
|
258
|
+
/**
|
|
259
|
+
* Sets up an internal listener to synchronize the in-memory state
|
|
260
|
+
* using the Last Write Wins (LWW) strategy.
|
|
261
|
+
*/
|
|
262
|
+
private setupLwwSynchronizationListener;
|
|
263
|
+
/**
|
|
264
|
+
* Persists the provided state to in-memory storage and notifies all subscribers
|
|
265
|
+
* across tabs. The state is synchronized using LWW.
|
|
266
|
+
* @param instanceId Unique identifier of the instance making the update.
|
|
267
|
+
* @param state The state to persist.
|
|
268
|
+
* @returns True if successful, false if an error occurs.
|
|
269
|
+
*/
|
|
270
|
+
set(instanceId: string, state: T): boolean;
|
|
271
|
+
/**
|
|
272
|
+
* Retrieves the current synchronized state from in-memory storage.
|
|
273
|
+
* @returns The synchronized state, or null if not set or if cleared.
|
|
274
|
+
*/
|
|
275
|
+
get(): T | null;
|
|
276
|
+
/**
|
|
277
|
+
* Subscribes to updates in the in-memory state.
|
|
278
|
+
* The callback is invoked when the state is updated by any instance
|
|
279
|
+
* (including other tabs) *excluding the subscribing instance's own 'set' or 'clear' calls*.
|
|
280
|
+
* @param instanceId Unique identifier of the subscribing instance.
|
|
281
|
+
* @param onStateChange Callback to invoke with the updated state.
|
|
282
|
+
* @returns Function to unsubscribe from updates.
|
|
283
|
+
*/
|
|
284
|
+
subscribe(instanceId: string, onStateChange: (state: T) => void): () => void;
|
|
285
|
+
/**
|
|
286
|
+
* Clears the in-memory state of this instance and synchronizes this clear
|
|
287
|
+
* across all tabs using the LWW strategy.
|
|
288
|
+
* @returns True if successful, false if an error occurs.
|
|
289
|
+
*/
|
|
290
|
+
clear(): boolean;
|
|
291
|
+
/**
|
|
292
|
+
* Returns metadata about the persistence layer.
|
|
293
|
+
*
|
|
294
|
+
* @returns An object containing:
|
|
295
|
+
* - `version`: The semantic version string of the persistence schema or application.
|
|
296
|
+
* - `id`: A unique identifier for the application using this persistence instance.
|
|
297
|
+
*/
|
|
298
|
+
stats(): {
|
|
299
|
+
version: string;
|
|
300
|
+
id: string;
|
|
301
|
+
};
|
|
302
|
+
}
|
|
303
|
+
//#endregion
|
|
304
|
+
export { EphemeralPersistence, IndexedDBPersistence, IndexedDBPersistenceConfig, SimplePersistence, StorageEvents, StoreConfig, WebStoragePersistence, WebStoragePersistenceConfig };
|