preact-missing-hooks 3.0.0 → 4.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/.husky/pre-commit +1 -0
- package/.husky/pre-push +1 -0
- package/.prettierignore +3 -0
- package/.prettierrc +6 -0
- package/Readme.md +179 -131
- package/dist/entry.cjs +21 -0
- package/dist/entry.js +2 -0
- package/dist/entry.js.map +1 -0
- package/dist/entry.modern.mjs +2 -0
- package/dist/entry.modern.mjs.map +1 -0
- package/dist/entry.module.js +2 -0
- package/dist/entry.module.js.map +1 -0
- package/dist/entry.umd.js +2 -0
- package/dist/entry.umd.js.map +1 -0
- package/dist/index.d.ts +13 -12
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.modern.mjs +2 -0
- package/dist/index.modern.mjs.map +1 -0
- package/dist/index.module.js +1 -1
- package/dist/index.module.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/indexedDB/dbController.d.ts +2 -2
- package/dist/indexedDB/index.d.ts +6 -6
- package/dist/indexedDB/openDB.d.ts +1 -1
- package/dist/indexedDB/tableController.d.ts +1 -1
- package/dist/indexedDB/types.d.ts +1 -2
- package/dist/react.js +1 -0
- package/dist/react.modern.mjs +1 -0
- package/dist/react.module.js +1 -0
- package/dist/react.umd.js +1 -0
- package/dist/useEventBus.d.ts +1 -1
- package/dist/useIndexedDB.d.ts +3 -3
- package/dist/useMutationObserver.d.ts +1 -1
- package/dist/useNetworkState.d.ts +3 -3
- package/dist/usePreferredTheme.d.ts +1 -1
- package/dist/useRageClick.d.ts +1 -1
- package/dist/useThreadedWorker.d.ts +1 -1
- package/dist/useTransition.d.ts +4 -1
- package/dist/useWorkerNotifications.d.ts +57 -0
- package/dist/useWrappedChildren.d.ts +3 -3
- package/{demo → docs}/index.html +56 -0
- package/{demo → docs}/main.js +437 -312
- package/eslint.config.mjs +10 -0
- package/package.json +65 -6
- package/scripts/generate-entry.cjs +34 -0
- package/src/index.ts +13 -12
- package/src/indexedDB/dbController.ts +101 -92
- package/src/indexedDB/index.ts +16 -11
- package/src/indexedDB/openDB.ts +49 -49
- package/src/indexedDB/requestToPromise.ts +17 -16
- package/src/indexedDB/tableController.ts +331 -257
- package/src/indexedDB/types.ts +35 -35
- package/src/useClipboard.ts +99 -97
- package/src/useEventBus.ts +39 -36
- package/src/useIndexedDB.ts +111 -111
- package/src/useMutationObserver.ts +26 -26
- package/src/useNetworkState.ts +124 -122
- package/src/usePreferredTheme.ts +68 -68
- package/src/useRageClick.ts +103 -103
- package/src/useThreadedWorker.ts +165 -165
- package/src/useTransition.ts +22 -19
- package/src/useWasmCompute.ts +209 -204
- package/src/useWebRTCIP.ts +181 -176
- package/src/useWorkerNotifications.ts +203 -0
- package/src/useWrappedChildren.ts +72 -58
- package/tests/react-adapter.tsx +12 -0
- package/tests/setup-react.ts +4 -0
- package/tests/useClipboard.test.tsx +4 -2
- package/tests/useThreadedWorker.test.tsx +3 -1
- package/tests/useWasmCompute.test.tsx +1 -1
- package/tests/useWebRTCIP.test.tsx +3 -1
- package/tests/useWorkerNotifications.test.tsx +170 -0
- package/vite.config.ts +11 -4
- package/vitest.config.preact.ts +20 -0
- package/vitest.config.react.ts +36 -0
- package/vitest.workspace.ts +6 -0
- /package/{demo → docs}/add.wasm +0 -0
|
@@ -1,257 +1,331 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Table controller: insert, update, delete, exists, query, upsert, bulkInsert, clear, count.
|
|
3
|
-
* Works in standalone mode (opens its own transaction per op) or bound to a transaction.
|
|
4
|
-
* @module indexedDB/tableController
|
|
5
|
-
*/
|
|
6
|
-
|
|
7
|
-
import type { OperationCallbacks } from
|
|
8
|
-
import { requestToPromise } from
|
|
9
|
-
|
|
10
|
-
/** Runs optional callbacks and returns the result. */
|
|
11
|
-
function withCallbacks<T>(
|
|
12
|
-
promise: Promise<T>,
|
|
13
|
-
options?: OperationCallbacks<T>
|
|
14
|
-
): Promise<T> {
|
|
15
|
-
if (!options) return promise;
|
|
16
|
-
return promise
|
|
17
|
-
.then((result) => {
|
|
18
|
-
options.onSuccess?.(result);
|
|
19
|
-
return result;
|
|
20
|
-
})
|
|
21
|
-
.catch((err: DOMException) => {
|
|
22
|
-
options.onError?.(err);
|
|
23
|
-
throw err;
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Standalone table controller: opens a new transaction for each operation.
|
|
29
|
-
*/
|
|
30
|
-
function createStandaloneController(
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
const
|
|
45
|
-
return withCallbacks(
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
);
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
const
|
|
74
|
-
return withCallbacks(
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
const store = getStore();
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Table controller: insert, update, delete, exists, query, upsert, bulkInsert, clear, count.
|
|
3
|
+
* Works in standalone mode (opens its own transaction per op) or bound to a transaction.
|
|
4
|
+
* @module indexedDB/tableController
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { OperationCallbacks } from "./types";
|
|
8
|
+
import { requestToPromise } from "./requestToPromise";
|
|
9
|
+
|
|
10
|
+
/** Runs optional callbacks and returns the result. */
|
|
11
|
+
function withCallbacks<T>(
|
|
12
|
+
promise: Promise<T>,
|
|
13
|
+
options?: OperationCallbacks<T>
|
|
14
|
+
): Promise<T> {
|
|
15
|
+
if (!options) return promise;
|
|
16
|
+
return promise
|
|
17
|
+
.then((result) => {
|
|
18
|
+
options.onSuccess?.(result);
|
|
19
|
+
return result;
|
|
20
|
+
})
|
|
21
|
+
.catch((err: DOMException) => {
|
|
22
|
+
options.onError?.(err);
|
|
23
|
+
throw err;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Standalone table controller: opens a new transaction for each operation.
|
|
29
|
+
*/
|
|
30
|
+
function createStandaloneController(
|
|
31
|
+
db: IDBDatabase,
|
|
32
|
+
tableName: string
|
|
33
|
+
): ITableController {
|
|
34
|
+
function getStore(mode: IDBTransactionMode): IDBObjectStore {
|
|
35
|
+
const tx = db.transaction([tableName], mode);
|
|
36
|
+
return tx.objectStore(tableName);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
insert<T>(
|
|
41
|
+
data: T,
|
|
42
|
+
options?: OperationCallbacks<IDBValidKey>
|
|
43
|
+
): Promise<IDBValidKey> {
|
|
44
|
+
const store = getStore("readwrite");
|
|
45
|
+
return withCallbacks(requestToPromise(store.add(data)), options);
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
update<T>(
|
|
49
|
+
key: IDBValidKey,
|
|
50
|
+
updates: Partial<T>,
|
|
51
|
+
options?: OperationCallbacks<void>
|
|
52
|
+
): Promise<void> {
|
|
53
|
+
const store = getStore("readwrite");
|
|
54
|
+
const getReq = store.get(key);
|
|
55
|
+
return withCallbacks(
|
|
56
|
+
requestToPromise(getReq)
|
|
57
|
+
.then((existing) => {
|
|
58
|
+
if (existing === undefined) {
|
|
59
|
+
throw new DOMException("Key not found", "NotFoundError");
|
|
60
|
+
}
|
|
61
|
+
const merged = { ...existing, ...updates } as T;
|
|
62
|
+
return requestToPromise(store.put(merged));
|
|
63
|
+
})
|
|
64
|
+
.then(() => undefined),
|
|
65
|
+
options
|
|
66
|
+
);
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
delete(
|
|
70
|
+
key: IDBValidKey,
|
|
71
|
+
options?: OperationCallbacks<void>
|
|
72
|
+
): Promise<void> {
|
|
73
|
+
const store = getStore("readwrite");
|
|
74
|
+
return withCallbacks(
|
|
75
|
+
requestToPromise(store.delete(key)).then(() => undefined),
|
|
76
|
+
options
|
|
77
|
+
);
|
|
78
|
+
},
|
|
79
|
+
|
|
80
|
+
exists(key: IDBValidKey): Promise<boolean> {
|
|
81
|
+
const store = getStore("readonly");
|
|
82
|
+
return requestToPromise(store.getKey(key)).then((k) => k !== undefined);
|
|
83
|
+
},
|
|
84
|
+
|
|
85
|
+
query<T>(
|
|
86
|
+
filterFn: (item: T) => boolean,
|
|
87
|
+
options?: OperationCallbacks<T[]>
|
|
88
|
+
): Promise<T[]> {
|
|
89
|
+
const store = getStore("readonly");
|
|
90
|
+
const request = store.openCursor();
|
|
91
|
+
const results: T[] = [];
|
|
92
|
+
return withCallbacks(
|
|
93
|
+
new Promise<T[]>((resolve, reject) => {
|
|
94
|
+
request.onsuccess = () => {
|
|
95
|
+
const cursor = request.result;
|
|
96
|
+
if (cursor) {
|
|
97
|
+
if (filterFn(cursor.value as T)) results.push(cursor.value as T);
|
|
98
|
+
cursor.continue();
|
|
99
|
+
} else {
|
|
100
|
+
resolve(results);
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
request.onerror = () =>
|
|
104
|
+
reject(request.error ?? new DOMException("Unknown error"));
|
|
105
|
+
}),
|
|
106
|
+
options
|
|
107
|
+
);
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
upsert<T>(
|
|
111
|
+
data: T,
|
|
112
|
+
options?: OperationCallbacks<IDBValidKey>
|
|
113
|
+
): Promise<IDBValidKey> {
|
|
114
|
+
const store = getStore("readwrite");
|
|
115
|
+
return withCallbacks(requestToPromise(store.put(data)), options);
|
|
116
|
+
},
|
|
117
|
+
|
|
118
|
+
bulkInsert<T>(
|
|
119
|
+
items: T[],
|
|
120
|
+
options?: OperationCallbacks<IDBValidKey[]>
|
|
121
|
+
): Promise<IDBValidKey[]> {
|
|
122
|
+
const store = getStore("readwrite");
|
|
123
|
+
const keys: IDBValidKey[] = [];
|
|
124
|
+
if (items.length === 0) {
|
|
125
|
+
return withCallbacks(Promise.resolve(keys), options);
|
|
126
|
+
}
|
|
127
|
+
let completed = 0;
|
|
128
|
+
const promise = new Promise<IDBValidKey[]>((resolve, reject) => {
|
|
129
|
+
const onDone = () => {
|
|
130
|
+
completed++;
|
|
131
|
+
if (completed === items.length) resolve(keys);
|
|
132
|
+
};
|
|
133
|
+
items.forEach((item, i) => {
|
|
134
|
+
const req = store.add(item);
|
|
135
|
+
req.onsuccess = () => {
|
|
136
|
+
keys[i] = req.result;
|
|
137
|
+
onDone();
|
|
138
|
+
};
|
|
139
|
+
req.onerror = () =>
|
|
140
|
+
reject(req.error ?? new DOMException("Unknown error"));
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
return withCallbacks(promise, options);
|
|
144
|
+
},
|
|
145
|
+
|
|
146
|
+
clear(options?: OperationCallbacks<void>): Promise<void> {
|
|
147
|
+
const store = getStore("readwrite");
|
|
148
|
+
return withCallbacks(
|
|
149
|
+
requestToPromise(store.clear()).then(() => undefined),
|
|
150
|
+
options
|
|
151
|
+
);
|
|
152
|
+
},
|
|
153
|
+
|
|
154
|
+
count(options?: OperationCallbacks<number>): Promise<number> {
|
|
155
|
+
const store = getStore("readonly");
|
|
156
|
+
return withCallbacks(requestToPromise(store.count()), options ?? {});
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Transaction-scoped table controller: uses the given transaction (no new transaction).
|
|
163
|
+
*/
|
|
164
|
+
function createTransactionController(
|
|
165
|
+
tx: IDBTransaction,
|
|
166
|
+
tableName: string
|
|
167
|
+
): ITableController {
|
|
168
|
+
function getStore(): IDBObjectStore {
|
|
169
|
+
return tx.objectStore(tableName);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return {
|
|
173
|
+
insert<T>(
|
|
174
|
+
data: T,
|
|
175
|
+
options?: OperationCallbacks<IDBValidKey>
|
|
176
|
+
): Promise<IDBValidKey> {
|
|
177
|
+
const store = getStore();
|
|
178
|
+
return withCallbacks(requestToPromise(store.add(data)), options);
|
|
179
|
+
},
|
|
180
|
+
|
|
181
|
+
update<T>(
|
|
182
|
+
key: IDBValidKey,
|
|
183
|
+
updates: Partial<T>,
|
|
184
|
+
options?: OperationCallbacks<void>
|
|
185
|
+
): Promise<void> {
|
|
186
|
+
const store = getStore();
|
|
187
|
+
return withCallbacks(
|
|
188
|
+
requestToPromise(store.get(key))
|
|
189
|
+
.then((existing) => {
|
|
190
|
+
if (existing === undefined) {
|
|
191
|
+
throw new DOMException("Key not found", "NotFoundError");
|
|
192
|
+
}
|
|
193
|
+
const merged = { ...existing, ...updates } as T;
|
|
194
|
+
return requestToPromise(store.put(merged));
|
|
195
|
+
})
|
|
196
|
+
.then(() => undefined),
|
|
197
|
+
options
|
|
198
|
+
);
|
|
199
|
+
},
|
|
200
|
+
|
|
201
|
+
delete(
|
|
202
|
+
key: IDBValidKey,
|
|
203
|
+
options?: OperationCallbacks<void>
|
|
204
|
+
): Promise<void> {
|
|
205
|
+
const store = getStore();
|
|
206
|
+
return withCallbacks(
|
|
207
|
+
requestToPromise(store.delete(key)).then(() => undefined),
|
|
208
|
+
options
|
|
209
|
+
);
|
|
210
|
+
},
|
|
211
|
+
|
|
212
|
+
exists(key: IDBValidKey): Promise<boolean> {
|
|
213
|
+
const store = getStore();
|
|
214
|
+
return requestToPromise(store.getKey(key)).then((k) => k !== undefined);
|
|
215
|
+
},
|
|
216
|
+
|
|
217
|
+
query<T>(
|
|
218
|
+
filterFn: (item: T) => boolean,
|
|
219
|
+
options?: OperationCallbacks<T[]>
|
|
220
|
+
): Promise<T[]> {
|
|
221
|
+
const store = getStore();
|
|
222
|
+
const request = store.openCursor();
|
|
223
|
+
const results: T[] = [];
|
|
224
|
+
return withCallbacks(
|
|
225
|
+
new Promise<T[]>((resolve, reject) => {
|
|
226
|
+
request.onsuccess = () => {
|
|
227
|
+
const cursor = request.result;
|
|
228
|
+
if (cursor) {
|
|
229
|
+
if (filterFn(cursor.value as T)) results.push(cursor.value as T);
|
|
230
|
+
cursor.continue();
|
|
231
|
+
} else {
|
|
232
|
+
resolve(results);
|
|
233
|
+
}
|
|
234
|
+
};
|
|
235
|
+
request.onerror = () =>
|
|
236
|
+
reject(request.error ?? new DOMException("Unknown error"));
|
|
237
|
+
}),
|
|
238
|
+
options
|
|
239
|
+
);
|
|
240
|
+
},
|
|
241
|
+
|
|
242
|
+
upsert<T>(
|
|
243
|
+
data: T,
|
|
244
|
+
options?: OperationCallbacks<IDBValidKey>
|
|
245
|
+
): Promise<IDBValidKey> {
|
|
246
|
+
const store = getStore();
|
|
247
|
+
return withCallbacks(requestToPromise(store.put(data)), options);
|
|
248
|
+
},
|
|
249
|
+
|
|
250
|
+
bulkInsert<T>(
|
|
251
|
+
items: T[],
|
|
252
|
+
options?: OperationCallbacks<IDBValidKey[]>
|
|
253
|
+
): Promise<IDBValidKey[]> {
|
|
254
|
+
const store = getStore();
|
|
255
|
+
const keys: IDBValidKey[] = [];
|
|
256
|
+
if (items.length === 0) {
|
|
257
|
+
return withCallbacks(Promise.resolve(keys), options);
|
|
258
|
+
}
|
|
259
|
+
let completed = 0;
|
|
260
|
+
const promise = new Promise<IDBValidKey[]>((resolve, reject) => {
|
|
261
|
+
items.forEach((item, i) => {
|
|
262
|
+
const req = store.add(item);
|
|
263
|
+
req.onsuccess = () => {
|
|
264
|
+
keys[i] = req.result;
|
|
265
|
+
completed++;
|
|
266
|
+
if (completed === items.length) resolve(keys);
|
|
267
|
+
};
|
|
268
|
+
req.onerror = () =>
|
|
269
|
+
reject(req.error ?? new DOMException("Unknown error"));
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
return withCallbacks(promise, options);
|
|
273
|
+
},
|
|
274
|
+
|
|
275
|
+
clear(options?: OperationCallbacks<void>): Promise<void> {
|
|
276
|
+
const store = getStore();
|
|
277
|
+
return withCallbacks(
|
|
278
|
+
requestToPromise(store.clear()).then(() => undefined),
|
|
279
|
+
options
|
|
280
|
+
);
|
|
281
|
+
},
|
|
282
|
+
|
|
283
|
+
count(options?: OperationCallbacks<number>): Promise<number> {
|
|
284
|
+
const store = getStore();
|
|
285
|
+
return withCallbacks(requestToPromise(store.count()), options ?? {});
|
|
286
|
+
},
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Public interface for a table controller (standalone or transaction-scoped). */
|
|
291
|
+
export interface ITableController {
|
|
292
|
+
insert<T>(
|
|
293
|
+
data: T,
|
|
294
|
+
options?: OperationCallbacks<IDBValidKey>
|
|
295
|
+
): Promise<IDBValidKey>;
|
|
296
|
+
update<T>(
|
|
297
|
+
key: IDBValidKey,
|
|
298
|
+
updates: Partial<T>,
|
|
299
|
+
options?: OperationCallbacks<void>
|
|
300
|
+
): Promise<void>;
|
|
301
|
+
delete(key: IDBValidKey, options?: OperationCallbacks<void>): Promise<void>;
|
|
302
|
+
exists(key: IDBValidKey): Promise<boolean>;
|
|
303
|
+
query<T>(
|
|
304
|
+
filterFn: (item: T) => boolean,
|
|
305
|
+
options?: OperationCallbacks<T[]>
|
|
306
|
+
): Promise<T[]>;
|
|
307
|
+
upsert<T>(
|
|
308
|
+
data: T,
|
|
309
|
+
options?: OperationCallbacks<IDBValidKey>
|
|
310
|
+
): Promise<IDBValidKey>;
|
|
311
|
+
bulkInsert<T>(
|
|
312
|
+
items: T[],
|
|
313
|
+
options?: OperationCallbacks<IDBValidKey[]>
|
|
314
|
+
): Promise<IDBValidKey[]>;
|
|
315
|
+
clear(options?: OperationCallbacks<void>): Promise<void>;
|
|
316
|
+
count(options?: OperationCallbacks<number>): Promise<number>;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
export function createTableController(
|
|
320
|
+
db: IDBDatabase,
|
|
321
|
+
tableName: string
|
|
322
|
+
): ITableController {
|
|
323
|
+
return createStandaloneController(db, tableName);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
export function createTransactionTableController(
|
|
327
|
+
tx: IDBTransaction,
|
|
328
|
+
tableName: string
|
|
329
|
+
): ITableController {
|
|
330
|
+
return createTransactionController(tx, tableName);
|
|
331
|
+
}
|
package/src/indexedDB/types.ts
CHANGED
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* IndexedDB hook system – shared types.
|
|
3
|
-
* @module indexedDB/types
|
|
4
|
-
*/
|
|
5
|
-
|
|
6
|
-
/** Table schema for a single object store. */
|
|
7
|
-
export interface TableSchema {
|
|
8
|
-
/** Key path (e.g. `"id"` or `["a", "b"]`). */
|
|
9
|
-
keyPath: string | string[];
|
|
10
|
-
/** Use auto-increment primary key. */
|
|
11
|
-
autoIncrement?: boolean;
|
|
12
|
-
/** Optional index names to create (each indexes the keyPath by default). */
|
|
13
|
-
indexes?: string[];
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
/** Configuration passed to useIndexedDB. */
|
|
17
|
-
export interface IndexedDBConfig {
|
|
18
|
-
/** Database name. */
|
|
19
|
-
name: string;
|
|
20
|
-
/** Schema version (increment to trigger onupgradeneeded). */
|
|
21
|
-
version: number;
|
|
22
|
-
/** Map of table name → store schema (keyPath, autoIncrement, indexes). */
|
|
23
|
-
tables: {
|
|
24
|
-
[tableName: string]: TableSchema;
|
|
25
|
-
};
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Optional callbacks for any operation. */
|
|
29
|
-
export interface OperationCallbacks<T = unknown> {
|
|
30
|
-
onSuccess?: (result: T) => void;
|
|
31
|
-
onError?: (error: DOMException) => void;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/** Options for transaction. */
|
|
35
|
-
export
|
|
1
|
+
/**
|
|
2
|
+
* IndexedDB hook system – shared types.
|
|
3
|
+
* @module indexedDB/types
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/** Table schema for a single object store. */
|
|
7
|
+
export interface TableSchema {
|
|
8
|
+
/** Key path (e.g. `"id"` or `["a", "b"]`). */
|
|
9
|
+
keyPath: string | string[];
|
|
10
|
+
/** Use auto-increment primary key. */
|
|
11
|
+
autoIncrement?: boolean;
|
|
12
|
+
/** Optional index names to create (each indexes the keyPath by default). */
|
|
13
|
+
indexes?: string[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Configuration passed to useIndexedDB. */
|
|
17
|
+
export interface IndexedDBConfig {
|
|
18
|
+
/** Database name. */
|
|
19
|
+
name: string;
|
|
20
|
+
/** Schema version (increment to trigger onupgradeneeded). */
|
|
21
|
+
version: number;
|
|
22
|
+
/** Map of table name → store schema (keyPath, autoIncrement, indexes). */
|
|
23
|
+
tables: {
|
|
24
|
+
[tableName: string]: TableSchema;
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Optional callbacks for any operation. */
|
|
29
|
+
export interface OperationCallbacks<T = unknown> {
|
|
30
|
+
onSuccess?: (result: T) => void;
|
|
31
|
+
onError?: (error: DOMException) => void;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** Options for transaction. */
|
|
35
|
+
export type TransactionOptions = OperationCallbacks<void>;
|