@asaidimu/utils-persistence 1.0.0

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.mts ADDED
@@ -0,0 +1,82 @@
1
+ import { S as SimplePersistence } from '../types-D26luDbE.js';
2
+ export { a as StorageEvents } from '../types-D26luDbE.js';
3
+
4
+ /**
5
+ * Implements SimplePersistence using web storage (localStorage or sessionStorage).
6
+ * Supports cross-tab synchronization via an event bus and native storage events when using localStorage.
7
+ */
8
+ declare class WebStoragePersistence<T extends object> implements SimplePersistence<T> {
9
+ private readonly storageKey;
10
+ private readonly eventBus;
11
+ private readonly storage;
12
+ /**
13
+ * Initializes a new instance of WebStoragePersistence.
14
+ * @param storageKey The key under which data is stored in web storage (e.g., 'user-profile').
15
+ * @param session Optional flag; if true, uses sessionStorage instead of localStorage (default: false).
16
+ */
17
+ constructor(storageKey: string, session?: boolean);
18
+ /**
19
+ * Initializes the event bus with configured settings.
20
+ * @returns Configured event bus instance.
21
+ */
22
+ private initializeEventBus;
23
+ /**
24
+ * Sets up a listener for native storage events to synchronize across tabs (localStorage only).
25
+ */
26
+ private setupStorageEventListener;
27
+ /**
28
+ * Persists the provided state to web storage and notifies subscribers.
29
+ * @param instanceId Unique identifier of the instance making the update.
30
+ * @param state The state to persist.
31
+ * @returns True if successful, false if an error occurs.
32
+ */
33
+ set(instanceId: string, state: T): boolean;
34
+ /**
35
+ * Retrieves the current state from web storage.
36
+ * @returns The persisted state, or null if not found or invalid.
37
+ */
38
+ get(): T | null;
39
+ /**
40
+ * Subscribes to updates in the persisted state, excluding the instance’s own changes.
41
+ * @param instanceId Unique identifier of the subscribing instance.
42
+ * @param onStateChange Callback to invoke with the updated state.
43
+ * @returns Function to unsubscribe from updates.
44
+ */
45
+ subscribe(instanceId: string, onStateChange: (state: T) => void): () => void;
46
+ /**
47
+ * Removes the persisted state from web storage.
48
+ * @returns True if successful, false if an error occurs.
49
+ */
50
+ clear(): boolean;
51
+ }
52
+
53
+ /**
54
+ * Configuration for database connections
55
+ */
56
+ interface DatabaseConfig {
57
+ store: string;
58
+ database: string;
59
+ collection: string;
60
+ enableTelemetry?: boolean;
61
+ }
62
+ /**
63
+ * IndexedDB persistence adapter with configurable database support
64
+ */
65
+ declare class IndexedDBPersistence<T> implements SimplePersistence<T> {
66
+ private collection;
67
+ private collectionPromise;
68
+ private config;
69
+ private eventBus;
70
+ constructor(config: DatabaseConfig);
71
+ private getCollection;
72
+ set(id: string, state: T): Promise<boolean>;
73
+ get(): Promise<T | null>;
74
+ subscribe(id: string, callback: (state: T) => void): () => void;
75
+ clear(): Promise<boolean>;
76
+ /**
77
+ * Close a specific database
78
+ */
79
+ close(): Promise<void>;
80
+ }
81
+
82
+ export { IndexedDBPersistence, SimplePersistence, WebStoragePersistence };
package/index.d.ts ADDED
@@ -0,0 +1,82 @@
1
+ import { S as SimplePersistence } from '../types-D26luDbE.js';
2
+ export { a as StorageEvents } from '../types-D26luDbE.js';
3
+
4
+ /**
5
+ * Implements SimplePersistence using web storage (localStorage or sessionStorage).
6
+ * Supports cross-tab synchronization via an event bus and native storage events when using localStorage.
7
+ */
8
+ declare class WebStoragePersistence<T extends object> implements SimplePersistence<T> {
9
+ private readonly storageKey;
10
+ private readonly eventBus;
11
+ private readonly storage;
12
+ /**
13
+ * Initializes a new instance of WebStoragePersistence.
14
+ * @param storageKey The key under which data is stored in web storage (e.g., 'user-profile').
15
+ * @param session Optional flag; if true, uses sessionStorage instead of localStorage (default: false).
16
+ */
17
+ constructor(storageKey: string, session?: boolean);
18
+ /**
19
+ * Initializes the event bus with configured settings.
20
+ * @returns Configured event bus instance.
21
+ */
22
+ private initializeEventBus;
23
+ /**
24
+ * Sets up a listener for native storage events to synchronize across tabs (localStorage only).
25
+ */
26
+ private setupStorageEventListener;
27
+ /**
28
+ * Persists the provided state to web storage and notifies subscribers.
29
+ * @param instanceId Unique identifier of the instance making the update.
30
+ * @param state The state to persist.
31
+ * @returns True if successful, false if an error occurs.
32
+ */
33
+ set(instanceId: string, state: T): boolean;
34
+ /**
35
+ * Retrieves the current state from web storage.
36
+ * @returns The persisted state, or null if not found or invalid.
37
+ */
38
+ get(): T | null;
39
+ /**
40
+ * Subscribes to updates in the persisted state, excluding the instance’s own changes.
41
+ * @param instanceId Unique identifier of the subscribing instance.
42
+ * @param onStateChange Callback to invoke with the updated state.
43
+ * @returns Function to unsubscribe from updates.
44
+ */
45
+ subscribe(instanceId: string, onStateChange: (state: T) => void): () => void;
46
+ /**
47
+ * Removes the persisted state from web storage.
48
+ * @returns True if successful, false if an error occurs.
49
+ */
50
+ clear(): boolean;
51
+ }
52
+
53
+ /**
54
+ * Configuration for database connections
55
+ */
56
+ interface DatabaseConfig {
57
+ store: string;
58
+ database: string;
59
+ collection: string;
60
+ enableTelemetry?: boolean;
61
+ }
62
+ /**
63
+ * IndexedDB persistence adapter with configurable database support
64
+ */
65
+ declare class IndexedDBPersistence<T> implements SimplePersistence<T> {
66
+ private collection;
67
+ private collectionPromise;
68
+ private config;
69
+ private eventBus;
70
+ constructor(config: DatabaseConfig);
71
+ private getCollection;
72
+ set(id: string, state: T): Promise<boolean>;
73
+ get(): Promise<T | null>;
74
+ subscribe(id: string, callback: (state: T) => void): () => void;
75
+ clear(): Promise<boolean>;
76
+ /**
77
+ * Close a specific database
78
+ */
79
+ close(): Promise<void>;
80
+ }
81
+
82
+ export { IndexedDBPersistence, SimplePersistence, WebStoragePersistence };