react-native-nitro-storage 0.5.0 → 0.5.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/README.md +42 -8
- package/docs/api-reference.md +95 -31
- package/docs/batch-transactions-migrations.md +23 -9
- package/docs/benchmarks.md +1 -1
- package/docs/recipes.md +30 -9
- package/docs/secure-storage.md +20 -1
- package/lib/commonjs/index.js +214 -13
- package/lib/commonjs/index.js.map +1 -1
- package/lib/commonjs/index.web.js +166 -11
- package/lib/commonjs/index.web.js.map +1 -1
- package/lib/commonjs/indexeddb-backend.js +7 -0
- package/lib/commonjs/indexeddb-backend.js.map +1 -1
- package/lib/commonjs/storage-events.js +117 -0
- package/lib/commonjs/storage-events.js.map +1 -0
- package/lib/module/index.js +214 -13
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +166 -11
- package/lib/module/index.web.js.map +1 -1
- package/lib/module/indexeddb-backend.js +7 -0
- package/lib/module/indexeddb-backend.js.map +1 -1
- package/lib/module/storage-events.js +112 -0
- package/lib/module/storage-events.js.map +1 -0
- package/lib/typescript/index.d.ts +14 -0
- package/lib/typescript/index.d.ts.map +1 -1
- package/lib/typescript/index.web.d.ts +14 -0
- package/lib/typescript/index.web.d.ts.map +1 -1
- package/lib/typescript/indexeddb-backend.d.ts.map +1 -1
- package/lib/typescript/storage-events.d.ts +37 -0
- package/lib/typescript/storage-events.d.ts.map +1 -0
- package/package.json +5 -4
- package/src/index.ts +534 -13
- package/src/index.web.ts +459 -22
- package/src/indexeddb-backend.ts +8 -0
- package/src/storage-events.ts +184 -0
package/src/indexeddb-backend.ts
CHANGED
|
@@ -72,6 +72,7 @@ export async function createIndexedDBBackend(
|
|
|
72
72
|
const db = await openDB(dbName, storeName);
|
|
73
73
|
const cache = new Map<string, string>();
|
|
74
74
|
const pendingWrites = new Set<Promise<void>>();
|
|
75
|
+
const pendingErrors: Error[] = [];
|
|
75
76
|
const subscribers = new Set<(event: WebStorageChangeEvent) => void>();
|
|
76
77
|
const sourceId = `nitro-storage-${Math.random().toString(36).slice(2)}`;
|
|
77
78
|
const channelName =
|
|
@@ -92,6 +93,7 @@ export async function createIndexedDBBackend(
|
|
|
92
93
|
error instanceof Error
|
|
93
94
|
? error
|
|
94
95
|
: new Error(String(error ?? "Unknown IndexedDB error"));
|
|
96
|
+
pendingErrors.push(normalized);
|
|
95
97
|
options.onError?.(normalized);
|
|
96
98
|
}
|
|
97
99
|
|
|
@@ -277,6 +279,12 @@ export async function createIndexedDBBackend(
|
|
|
277
279
|
},
|
|
278
280
|
async flush(): Promise<void> {
|
|
279
281
|
await Promise.all(Array.from(pendingWrites));
|
|
282
|
+
if (pendingErrors.length === 0) {
|
|
283
|
+
return;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
const [error] = pendingErrors.splice(0);
|
|
287
|
+
throw error;
|
|
280
288
|
},
|
|
281
289
|
};
|
|
282
290
|
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
import { StorageScope } from "./Storage.types";
|
|
2
|
+
|
|
3
|
+
export type StorageChangeOperation =
|
|
4
|
+
| "set"
|
|
5
|
+
| "remove"
|
|
6
|
+
| "clear"
|
|
7
|
+
| "clearNamespace"
|
|
8
|
+
| "setBatch"
|
|
9
|
+
| "removeBatch"
|
|
10
|
+
| "import"
|
|
11
|
+
| "external";
|
|
12
|
+
|
|
13
|
+
export type StorageChangeSource = "memory" | "native" | "web" | "external";
|
|
14
|
+
|
|
15
|
+
export type StorageKeyChangeEvent = {
|
|
16
|
+
type: "key";
|
|
17
|
+
scope: StorageScope;
|
|
18
|
+
key: string;
|
|
19
|
+
oldValue: string | undefined;
|
|
20
|
+
newValue: string | undefined;
|
|
21
|
+
operation: StorageChangeOperation;
|
|
22
|
+
source: StorageChangeSource;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
export type StorageBatchChangeEvent = {
|
|
26
|
+
type: "batch";
|
|
27
|
+
scope: StorageScope;
|
|
28
|
+
operation: StorageChangeOperation;
|
|
29
|
+
source: StorageChangeSource;
|
|
30
|
+
changes: StorageKeyChangeEvent[];
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export type StorageChangeEvent =
|
|
34
|
+
| StorageKeyChangeEvent
|
|
35
|
+
| StorageBatchChangeEvent;
|
|
36
|
+
|
|
37
|
+
export type StorageEventListener = (event: StorageChangeEvent) => void;
|
|
38
|
+
|
|
39
|
+
type PrefixListener = {
|
|
40
|
+
prefix: string;
|
|
41
|
+
listener: StorageEventListener;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
function addListener<T>(
|
|
45
|
+
registry: Map<StorageScope, Set<T>>,
|
|
46
|
+
scope: StorageScope,
|
|
47
|
+
listener: T,
|
|
48
|
+
): () => void {
|
|
49
|
+
let listeners = registry.get(scope);
|
|
50
|
+
if (!listeners) {
|
|
51
|
+
listeners = new Set();
|
|
52
|
+
registry.set(scope, listeners);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
listeners.add(listener);
|
|
56
|
+
return () => {
|
|
57
|
+
const scopedListeners = registry.get(scope);
|
|
58
|
+
if (!scopedListeners) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
scopedListeners.delete(listener);
|
|
62
|
+
if (scopedListeners.size === 0) {
|
|
63
|
+
registry.delete(scope);
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export class StorageEventRegistry {
|
|
69
|
+
private readonly scopeListeners = new Map<
|
|
70
|
+
StorageScope,
|
|
71
|
+
Set<StorageEventListener>
|
|
72
|
+
>();
|
|
73
|
+
private readonly keyListeners = new Map<
|
|
74
|
+
StorageScope,
|
|
75
|
+
Map<string, Set<StorageEventListener>>
|
|
76
|
+
>();
|
|
77
|
+
private readonly prefixListeners = new Map<
|
|
78
|
+
StorageScope,
|
|
79
|
+
Set<PrefixListener>
|
|
80
|
+
>();
|
|
81
|
+
|
|
82
|
+
subscribe(scope: StorageScope, listener: StorageEventListener): () => void {
|
|
83
|
+
return addListener(this.scopeListeners, scope, listener);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
hasListeners(scope: StorageScope): boolean {
|
|
87
|
+
return (
|
|
88
|
+
(this.scopeListeners.get(scope)?.size ?? 0) > 0 ||
|
|
89
|
+
(this.keyListeners.get(scope)?.size ?? 0) > 0 ||
|
|
90
|
+
(this.prefixListeners.get(scope)?.size ?? 0) > 0
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
subscribeKey(
|
|
95
|
+
scope: StorageScope,
|
|
96
|
+
key: string,
|
|
97
|
+
listener: StorageEventListener,
|
|
98
|
+
): () => void {
|
|
99
|
+
let scopedListeners = this.keyListeners.get(scope);
|
|
100
|
+
if (!scopedListeners) {
|
|
101
|
+
scopedListeners = new Map();
|
|
102
|
+
this.keyListeners.set(scope, scopedListeners);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
let keyListeners = scopedListeners.get(key);
|
|
106
|
+
if (!keyListeners) {
|
|
107
|
+
keyListeners = new Set();
|
|
108
|
+
scopedListeners.set(key, keyListeners);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
keyListeners.add(listener);
|
|
112
|
+
return () => {
|
|
113
|
+
const currentScopedListeners = this.keyListeners.get(scope);
|
|
114
|
+
const currentKeyListeners = currentScopedListeners?.get(key);
|
|
115
|
+
if (!currentKeyListeners) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
currentKeyListeners.delete(listener);
|
|
119
|
+
if (currentKeyListeners.size === 0) {
|
|
120
|
+
currentScopedListeners?.delete(key);
|
|
121
|
+
}
|
|
122
|
+
if (currentScopedListeners?.size === 0) {
|
|
123
|
+
this.keyListeners.delete(scope);
|
|
124
|
+
}
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
subscribePrefix(
|
|
129
|
+
scope: StorageScope,
|
|
130
|
+
prefix: string,
|
|
131
|
+
listener: StorageEventListener,
|
|
132
|
+
): () => void {
|
|
133
|
+
const entry: PrefixListener = { prefix, listener };
|
|
134
|
+
return addListener(this.prefixListeners, scope, entry);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
emitKey(event: StorageKeyChangeEvent): void {
|
|
138
|
+
this.emitToScope(event);
|
|
139
|
+
this.emitToKey(event);
|
|
140
|
+
this.emitToPrefixes(event);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
emitBatch(event: StorageBatchChangeEvent): void {
|
|
144
|
+
this.emitToScope(event);
|
|
145
|
+
event.changes.forEach((change) => {
|
|
146
|
+
this.emitToKey(change);
|
|
147
|
+
});
|
|
148
|
+
this.emitBatchToPrefixes(event);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
private emitToScope(event: StorageChangeEvent): void {
|
|
152
|
+
this.scopeListeners.get(event.scope)?.forEach((listener) => {
|
|
153
|
+
listener(event);
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private emitToKey(event: StorageKeyChangeEvent): void {
|
|
158
|
+
this.keyListeners
|
|
159
|
+
.get(event.scope)
|
|
160
|
+
?.get(event.key)
|
|
161
|
+
?.forEach((listener) => {
|
|
162
|
+
listener(event);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
private emitToPrefixes(event: StorageKeyChangeEvent): void {
|
|
167
|
+
this.prefixListeners.get(event.scope)?.forEach(({ prefix, listener }) => {
|
|
168
|
+
if (event.key.startsWith(prefix)) {
|
|
169
|
+
listener(event);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
private emitBatchToPrefixes(event: StorageBatchChangeEvent): void {
|
|
175
|
+
this.prefixListeners.get(event.scope)?.forEach(({ prefix, listener }) => {
|
|
176
|
+
const changes = event.changes.filter((change) =>
|
|
177
|
+
change.key.startsWith(prefix),
|
|
178
|
+
);
|
|
179
|
+
if (changes.length > 0) {
|
|
180
|
+
listener({ ...event, changes });
|
|
181
|
+
}
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
}
|