@asaidimu/utils-persistence 2.0.0 → 2.0.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.
- package/LICENSE.md +21 -0
- package/index.d.mts +82 -0
- package/index.d.ts +82 -0
- package/index.js +2290 -0
- package/index.mjs +2282 -0
- package/package.json +3 -3
package/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2025 Saidimu
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
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 };
|