@verdant-web/react 15.0.1 → 16.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/dist/esm/hooks.js CHANGED
@@ -1,337 +1,337 @@
1
- var __rest = (this && this.__rest) || function (s, e) {
2
- var t = {};
3
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
- t[p] = s[p];
5
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
- t[p[i]] = s[p[i]];
9
- }
10
- return t;
11
- };
12
- import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
- import { EntityFile, } from '@verdant-web/store';
14
- import { createContext, Suspense, useCallback, useContext, useEffect, useMemo, useState, useSyncExternalStore, } from 'react';
15
- import { suspend } from 'suspend-react';
16
- import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector.js';
17
- function useLiveQuery(liveQuery) {
18
- if (liveQuery && liveQuery.status === 'initial') {
19
- suspend(() => liveQuery.resolved, [liveQuery]);
20
- }
21
- return useSyncExternalStore((callback) => {
22
- if (liveQuery) {
23
- return liveQuery.subscribe(callback);
24
- }
25
- else {
26
- return () => { };
27
- }
28
- }, () => {
29
- return liveQuery ? liveQuery.current : null;
30
- });
31
- }
32
- function capitalize(str) {
33
- return (str.charAt(0).toUpperCase() + str.slice(1));
34
- }
35
- export function createHooks(schema) {
36
- const Context = createContext(null);
37
- function useStorage() {
38
- const ctx = useContext(Context);
39
- if (!ctx) {
40
- throw new Error('No verdant provider was found');
41
- }
42
- return suspend(() => ctx.readyPromise, ['lofi_' + ctx.namespace]);
43
- }
44
- function useWatch(liveObject, prop) {
45
- return useSyncExternalStore((handler) => {
46
- if (liveObject) {
47
- return liveObject.subscribe('change', handler);
48
- }
49
- return () => { };
50
- }, () => {
51
- if (liveObject) {
52
- if (liveObject instanceof EntityFile) {
53
- return liveObject.url;
54
- }
55
- else {
56
- if (prop) {
57
- return liveObject.get(prop);
58
- }
59
- return liveObject.getAll();
60
- }
61
- }
62
- return undefined;
63
- });
64
- }
65
- function useSelf() {
66
- const storage = useStorage();
67
- return useSyncExternalStore((callback) => storage.sync.presence.subscribe('selfChanged', callback), () => storage.sync.presence.self);
68
- }
69
- function usePeerIds() {
70
- const storage = useStorage();
71
- return useSyncExternalStore((callback) => storage.sync.presence.subscribe('peersChanged', callback), () => storage.sync.presence.peerIds);
72
- }
73
- function usePeer(peerId) {
74
- const storage = useStorage();
75
- return useSyncExternalStore((callback) => {
76
- const unsubs = [];
77
- unsubs.push(storage.sync.presence.subscribe('peerChanged', (id, user) => {
78
- if (id === peerId) {
79
- callback();
80
- }
81
- }));
82
- unsubs.push(storage.sync.presence.subscribe('peerLeft', (id) => {
83
- if (id === peerId) {
84
- callback();
85
- }
86
- }));
87
- return () => {
88
- unsubs.forEach((unsub) => unsub());
89
- };
90
- }, () => { var _a; return (peerId ? (_a = storage.sync.presence.peers[peerId]) !== null && _a !== void 0 ? _a : null : null); });
91
- }
92
- function useFindPeer(query, options) {
93
- const storage = useStorage();
94
- return useSyncExternalStore((callback) => {
95
- const unsubs = [];
96
- unsubs.push(storage.sync.presence.subscribe('peerChanged', (id, user) => {
97
- if (query(user)) {
98
- callback();
99
- }
100
- }));
101
- unsubs.push(storage.sync.presence.subscribe('peerLeft', (id) => {
102
- if (query(storage.sync.presence.peers[id])) {
103
- callback();
104
- }
105
- }));
106
- if (options === null || options === void 0 ? void 0 : options.includeSelf) {
107
- unsubs.push(storage.sync.presence.subscribe('selfChanged', (user) => {
108
- if (query(user)) {
109
- callback();
110
- }
111
- }));
112
- }
113
- return () => {
114
- unsubs.forEach((unsub) => unsub());
115
- };
116
- }, () => {
117
- const peers = Object.values(storage.sync.presence.peers);
118
- if (options === null || options === void 0 ? void 0 : options.includeSelf) {
119
- peers.push(storage.sync.presence.self);
120
- }
121
- return peers.find(query) || null;
122
- });
123
- }
124
- function useFindPeers(query, options) {
125
- const storage = useStorage();
126
- return useSyncExternalStoreWithSelector((callback) => {
127
- const unsubs = [];
128
- unsubs.push(storage.sync.presence.subscribe('peerChanged', (id, user) => {
129
- if (query(user)) {
130
- callback();
131
- }
132
- }));
133
- unsubs.push(storage.sync.presence.subscribe('peerLeft', (id) => {
134
- callback();
135
- }));
136
- if (options === null || options === void 0 ? void 0 : options.includeSelf) {
137
- unsubs.push(storage.sync.presence.subscribe('selfChanged', (user) => {
138
- if (query(user)) {
139
- callback();
140
- }
141
- }));
142
- }
143
- return () => {
144
- unsubs.forEach((unsub) => unsub());
145
- };
146
- }, () => {
147
- const peers = Object.values(storage.sync.presence.peers).filter(Boolean);
148
- if (options === null || options === void 0 ? void 0 : options.includeSelf) {
149
- peers.push(storage.sync.presence.self);
150
- }
151
- return peers.filter(query);
152
- }, () => [], (peers) => peers, (a, b) => a.length === b.length && a.every((peer, i) => peer === b[i]));
153
- }
154
- function useSyncStatus() {
155
- const storage = useStorage();
156
- return useSyncExternalStore((callback) => storage.sync.subscribe('onlineChange', callback), () => storage.sync.isConnected);
157
- }
158
- function useUndo() {
159
- const storage = useStorage();
160
- return useCallback(() => storage.undoHistory.undo(), [storage]);
161
- }
162
- function useRedo() {
163
- const storage = useStorage();
164
- return useCallback(() => storage.undoHistory.redo(), [storage]);
165
- }
166
- function useCanUndo() {
167
- const storage = useStorage();
168
- return useSyncExternalStore((callback) => storage.undoHistory.subscribe('change', callback), () => storage.undoHistory.canUndo);
169
- }
170
- function useCanRedo() {
171
- const storage = useStorage();
172
- return useSyncExternalStore((callback) => storage.undoHistory.subscribe('change', callback), () => storage.undoHistory.canRedo);
173
- }
174
- function useUnsuspendedClient() {
175
- const desc = useContext(Context);
176
- const client = desc === null || desc === void 0 ? void 0 : desc.current;
177
- const [_, forceUpdate] = useState(0);
178
- if (desc && !client) {
179
- desc.readyPromise.then(() => forceUpdate((n) => n + 1));
180
- }
181
- return client || null;
182
- }
183
- /**
184
- * Non-suspending hook which allows declarative sync start/stop
185
- * control.
186
- *
187
- * You can optionally configure parameters as part of this as well.
188
- */
189
- function useSync(isOn, config = {}) {
190
- const client = useUnsuspendedClient();
191
- useEffect(() => {
192
- if (client) {
193
- if (isOn) {
194
- client.sync.start();
195
- }
196
- else {
197
- client.sync.stop();
198
- }
199
- }
200
- }, [client, isOn]);
201
- useEffect(() => {
202
- if (client) {
203
- if (config.mode !== undefined) {
204
- client.sync.setMode(config.mode);
205
- }
206
- }
207
- }, [client, config.mode]);
208
- useEffect(() => {
209
- if (client) {
210
- if (config.pullInterval !== undefined) {
211
- client.sync.setPullInterval(config.pullInterval);
212
- }
213
- }
214
- }, [client, config.pullInterval]);
215
- }
216
- function SyncController({ isOn }) {
217
- useSync(isOn);
218
- return null;
219
- }
220
- const hooks = {
221
- useStorage,
222
- useClient: useStorage,
223
- useUnsuspendedClient,
224
- useWatch,
225
- useSelf,
226
- usePeerIds,
227
- usePeer,
228
- useFindPeer,
229
- useFindPeers,
230
- useSyncStatus,
231
- useUndo,
232
- useRedo,
233
- useCanUndo,
234
- useCanRedo,
235
- useSync,
236
- Context,
237
- Provider: (_a) => {
238
- var { value, children, sync, suspenseFallback } = _a, rest = __rest(_a, ["value", "children", "sync", "suspenseFallback"]);
239
- // auto-open storage when used in provider
240
- useMemo(() => {
241
- value.open();
242
- }, [value]);
243
- return (_jsx(Context.Provider, Object.assign({ value: value }, rest, { children: _jsxs(Suspense, Object.assign({ fallback: suspenseFallback || null }, { children: [children, sync !== undefined && _jsx(SyncController, { isOn: sync })] })) })));
244
- },
245
- };
246
- const collectionNames = Object.keys(schema.collections);
247
- for (const name of collectionNames) {
248
- const collection = schema.collections[name];
249
- const getOneHookName = `use${capitalize(collection.name)}`;
250
- hooks[getOneHookName] = function useIndividual(id, { skip } = {}) {
251
- const storage = useStorage();
252
- const liveQuery = useMemo(() => {
253
- return skip ? null : storage[name].get(id);
254
- }, [id, skip]);
255
- const data = useLiveQuery(liveQuery);
256
- return data;
257
- };
258
- const findOneHookName = `useOne${capitalize(collection.name)}`;
259
- hooks[findOneHookName] = function useOne({ skip, index, key, } = {}) {
260
- const storage = useStorage();
261
- const liveQuery = useMemo(() => {
262
- return skip ? null : storage[name].findOne({ index, key });
263
- }, [index, skip]);
264
- const data = useLiveQuery(liveQuery);
265
- return data;
266
- };
267
- const getAllHookName = `useAll${capitalize(collection.pluralName || collection.name + 's')}`;
268
- hooks[getAllHookName] = function useAll({ index, skip, key, } = {}) {
269
- const storage = useStorage();
270
- // assumptions: this query getter is fast and returns the same
271
- // query identity for subsequent calls.
272
- const liveQuery = useMemo(() => (skip ? null : storage[name].findAll({ index, key })), [index, skip]);
273
- const data = useLiveQuery(liveQuery);
274
- return data || [];
275
- };
276
- const getAllPaginatedHookName = `useAll${capitalize(collection.pluralName || collection.name + 's')}Paginated`;
277
- hooks[getAllPaginatedHookName] = function useAllPaginated({ index, skip, pageSize = 10, key, } = {}) {
278
- const storage = useStorage();
279
- // assumptions: this query getter is fast and returns the same
280
- // query identity for subsequent calls.
281
- const liveQuery = useMemo(() => skip
282
- ? null
283
- : storage[name].findPage({
284
- index,
285
- pageSize,
286
- page: 0,
287
- key: key || getAllPaginatedHookName,
288
- }), [index, skip, pageSize]);
289
- const data = useLiveQuery(liveQuery);
290
- const tools = useMemo(() => ({
291
- next: () => liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.nextPage(),
292
- previous: () => liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.previousPage(),
293
- setPage: (page) => liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.setPage(page),
294
- get hasPrevious() {
295
- return liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.hasPreviousPage;
296
- },
297
- get hasNext() {
298
- return liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.hasNextPage;
299
- },
300
- }), [liveQuery]);
301
- return [data, tools];
302
- };
303
- const getAllInfiniteHookName = `useAll${capitalize(collection.pluralName || collection.name + 's')}Infinite`;
304
- hooks[getAllInfiniteHookName] = function useAllInfinite({ index, skip, pageSize = 10, key, } = {}) {
305
- const storage = useStorage();
306
- // assumptions: this query getter is fast and returns the same
307
- // query identity for subsequent calls.
308
- const liveQuery = useMemo(() => skip
309
- ? null
310
- : storage[name].findAllInfinite({
311
- index,
312
- pageSize,
313
- key: key || getAllInfiniteHookName,
314
- }), [index, skip, pageSize]);
315
- const data = useLiveQuery(liveQuery);
316
- const tools = useMemo(() => ({
317
- loadMore: () => liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.loadMore(),
318
- get hasMore() {
319
- return liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.hasMore;
320
- },
321
- }), [liveQuery]);
322
- return [data, tools];
323
- };
324
- }
325
- hooks.withMutations = (mutations) => {
326
- const augmentedHooks = Object.assign({}, hooks);
327
- for (const [name, subHook] of Object.entries(mutations)) {
328
- augmentedHooks[name] = (...args) => {
329
- const client = hooks.useClient();
330
- return subHook(client, ...args);
331
- };
332
- }
333
- return augmentedHooks;
334
- };
335
- return hooks;
336
- }
1
+ var __rest = (this && this.__rest) || function (s, e) {
2
+ var t = {};
3
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
4
+ t[p] = s[p];
5
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
6
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
7
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
8
+ t[p[i]] = s[p[i]];
9
+ }
10
+ return t;
11
+ };
12
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
13
+ import { EntityFile, } from '@verdant-web/store';
14
+ import { createContext, Suspense, useCallback, useContext, useEffect, useMemo, useState, useSyncExternalStore, } from 'react';
15
+ import { suspend } from 'suspend-react';
16
+ import { useSyncExternalStoreWithSelector } from 'use-sync-external-store/with-selector.js';
17
+ function useLiveQuery(liveQuery) {
18
+ if (liveQuery && liveQuery.status === 'initial') {
19
+ suspend(() => liveQuery.resolved, [liveQuery]);
20
+ }
21
+ return useSyncExternalStore((callback) => {
22
+ if (liveQuery) {
23
+ return liveQuery.subscribe(callback);
24
+ }
25
+ else {
26
+ return () => { };
27
+ }
28
+ }, () => {
29
+ return liveQuery ? liveQuery.current : null;
30
+ });
31
+ }
32
+ function capitalize(str) {
33
+ return (str.charAt(0).toUpperCase() + str.slice(1));
34
+ }
35
+ export function createHooks(schema) {
36
+ const Context = createContext(null);
37
+ function useStorage() {
38
+ const ctx = useContext(Context);
39
+ if (!ctx) {
40
+ throw new Error('No verdant provider was found');
41
+ }
42
+ return suspend(() => ctx.readyPromise, ['lofi_' + ctx.namespace]);
43
+ }
44
+ function useWatch(liveObject, prop) {
45
+ return useSyncExternalStore((handler) => {
46
+ if (liveObject) {
47
+ return liveObject.subscribe('change', handler);
48
+ }
49
+ return () => { };
50
+ }, () => {
51
+ if (liveObject) {
52
+ if (liveObject instanceof EntityFile) {
53
+ return liveObject.url;
54
+ }
55
+ else {
56
+ if (prop) {
57
+ return liveObject.get(prop);
58
+ }
59
+ return liveObject.getAll();
60
+ }
61
+ }
62
+ return undefined;
63
+ });
64
+ }
65
+ function useSelf() {
66
+ const storage = useStorage();
67
+ return useSyncExternalStore((callback) => storage.sync.presence.subscribe('selfChanged', callback), () => storage.sync.presence.self);
68
+ }
69
+ function usePeerIds() {
70
+ const storage = useStorage();
71
+ return useSyncExternalStore((callback) => storage.sync.presence.subscribe('peersChanged', callback), () => storage.sync.presence.peerIds);
72
+ }
73
+ function usePeer(peerId) {
74
+ const storage = useStorage();
75
+ return useSyncExternalStore((callback) => {
76
+ const unsubs = [];
77
+ unsubs.push(storage.sync.presence.subscribe('peerChanged', (id, user) => {
78
+ if (id === peerId) {
79
+ callback();
80
+ }
81
+ }));
82
+ unsubs.push(storage.sync.presence.subscribe('peerLeft', (id) => {
83
+ if (id === peerId) {
84
+ callback();
85
+ }
86
+ }));
87
+ return () => {
88
+ unsubs.forEach((unsub) => unsub());
89
+ };
90
+ }, () => { var _a; return (peerId ? (_a = storage.sync.presence.peers[peerId]) !== null && _a !== void 0 ? _a : null : null); });
91
+ }
92
+ function useFindPeer(query, options) {
93
+ const storage = useStorage();
94
+ return useSyncExternalStore((callback) => {
95
+ const unsubs = [];
96
+ unsubs.push(storage.sync.presence.subscribe('peerChanged', (id, user) => {
97
+ if (query(user)) {
98
+ callback();
99
+ }
100
+ }));
101
+ unsubs.push(storage.sync.presence.subscribe('peerLeft', (id) => {
102
+ if (query(storage.sync.presence.peers[id])) {
103
+ callback();
104
+ }
105
+ }));
106
+ if (options === null || options === void 0 ? void 0 : options.includeSelf) {
107
+ unsubs.push(storage.sync.presence.subscribe('selfChanged', (user) => {
108
+ if (query(user)) {
109
+ callback();
110
+ }
111
+ }));
112
+ }
113
+ return () => {
114
+ unsubs.forEach((unsub) => unsub());
115
+ };
116
+ }, () => {
117
+ const peers = Object.values(storage.sync.presence.peers);
118
+ if (options === null || options === void 0 ? void 0 : options.includeSelf) {
119
+ peers.push(storage.sync.presence.self);
120
+ }
121
+ return peers.find(query) || null;
122
+ });
123
+ }
124
+ function useFindPeers(query, options) {
125
+ const storage = useStorage();
126
+ return useSyncExternalStoreWithSelector((callback) => {
127
+ const unsubs = [];
128
+ unsubs.push(storage.sync.presence.subscribe('peerChanged', (id, user) => {
129
+ if (query(user)) {
130
+ callback();
131
+ }
132
+ }));
133
+ unsubs.push(storage.sync.presence.subscribe('peerLeft', (id) => {
134
+ callback();
135
+ }));
136
+ if (options === null || options === void 0 ? void 0 : options.includeSelf) {
137
+ unsubs.push(storage.sync.presence.subscribe('selfChanged', (user) => {
138
+ if (query(user)) {
139
+ callback();
140
+ }
141
+ }));
142
+ }
143
+ return () => {
144
+ unsubs.forEach((unsub) => unsub());
145
+ };
146
+ }, () => {
147
+ const peers = Object.values(storage.sync.presence.peers).filter(Boolean);
148
+ if (options === null || options === void 0 ? void 0 : options.includeSelf) {
149
+ peers.push(storage.sync.presence.self);
150
+ }
151
+ return peers.filter(query);
152
+ }, () => [], (peers) => peers, (a, b) => a.length === b.length && a.every((peer, i) => peer === b[i]));
153
+ }
154
+ function useSyncStatus() {
155
+ const storage = useStorage();
156
+ return useSyncExternalStore((callback) => storage.sync.subscribe('onlineChange', callback), () => storage.sync.isConnected);
157
+ }
158
+ function useUndo() {
159
+ const storage = useStorage();
160
+ return useCallback(() => storage.undoHistory.undo(), [storage]);
161
+ }
162
+ function useRedo() {
163
+ const storage = useStorage();
164
+ return useCallback(() => storage.undoHistory.redo(), [storage]);
165
+ }
166
+ function useCanUndo() {
167
+ const storage = useStorage();
168
+ return useSyncExternalStore((callback) => storage.undoHistory.subscribe('change', callback), () => storage.undoHistory.canUndo);
169
+ }
170
+ function useCanRedo() {
171
+ const storage = useStorage();
172
+ return useSyncExternalStore((callback) => storage.undoHistory.subscribe('change', callback), () => storage.undoHistory.canRedo);
173
+ }
174
+ function useUnsuspendedClient() {
175
+ const desc = useContext(Context);
176
+ const client = desc === null || desc === void 0 ? void 0 : desc.current;
177
+ const [_, forceUpdate] = useState(0);
178
+ if (desc && !client) {
179
+ desc.readyPromise.then(() => forceUpdate((n) => n + 1));
180
+ }
181
+ return client || null;
182
+ }
183
+ /**
184
+ * Non-suspending hook which allows declarative sync start/stop
185
+ * control.
186
+ *
187
+ * You can optionally configure parameters as part of this as well.
188
+ */
189
+ function useSync(isOn, config = {}) {
190
+ const client = useUnsuspendedClient();
191
+ useEffect(() => {
192
+ if (client) {
193
+ if (isOn) {
194
+ client.sync.start();
195
+ }
196
+ else {
197
+ client.sync.stop();
198
+ }
199
+ }
200
+ }, [client, isOn]);
201
+ useEffect(() => {
202
+ if (client) {
203
+ if (config.mode !== undefined) {
204
+ client.sync.setMode(config.mode);
205
+ }
206
+ }
207
+ }, [client, config.mode]);
208
+ useEffect(() => {
209
+ if (client) {
210
+ if (config.pullInterval !== undefined) {
211
+ client.sync.setPullInterval(config.pullInterval);
212
+ }
213
+ }
214
+ }, [client, config.pullInterval]);
215
+ }
216
+ function SyncController({ isOn }) {
217
+ useSync(isOn);
218
+ return null;
219
+ }
220
+ const hooks = {
221
+ useStorage,
222
+ useClient: useStorage,
223
+ useUnsuspendedClient,
224
+ useWatch,
225
+ useSelf,
226
+ usePeerIds,
227
+ usePeer,
228
+ useFindPeer,
229
+ useFindPeers,
230
+ useSyncStatus,
231
+ useUndo,
232
+ useRedo,
233
+ useCanUndo,
234
+ useCanRedo,
235
+ useSync,
236
+ Context,
237
+ Provider: (_a) => {
238
+ var { value, children, sync, suspenseFallback } = _a, rest = __rest(_a, ["value", "children", "sync", "suspenseFallback"]);
239
+ // auto-open storage when used in provider
240
+ useMemo(() => {
241
+ value.open();
242
+ }, [value]);
243
+ return (_jsx(Context.Provider, Object.assign({ value: value }, rest, { children: _jsxs(Suspense, Object.assign({ fallback: suspenseFallback || null }, { children: [children, sync !== undefined && _jsx(SyncController, { isOn: sync })] })) })));
244
+ },
245
+ };
246
+ const collectionNames = Object.keys(schema.collections);
247
+ for (const name of collectionNames) {
248
+ const collection = schema.collections[name];
249
+ const getOneHookName = `use${capitalize(collection.name)}`;
250
+ hooks[getOneHookName] = function useIndividual(id, { skip } = {}) {
251
+ const storage = useStorage();
252
+ const liveQuery = useMemo(() => {
253
+ return skip ? null : storage[name].get(id);
254
+ }, [id, skip]);
255
+ const data = useLiveQuery(liveQuery);
256
+ return data;
257
+ };
258
+ const findOneHookName = `useOne${capitalize(collection.name)}`;
259
+ hooks[findOneHookName] = function useOne({ skip, index, key, } = {}) {
260
+ const storage = useStorage();
261
+ const liveQuery = useMemo(() => {
262
+ return skip ? null : storage[name].findOne({ index, key });
263
+ }, [index, skip]);
264
+ const data = useLiveQuery(liveQuery);
265
+ return data;
266
+ };
267
+ const getAllHookName = `useAll${capitalize(collection.pluralName || collection.name + 's')}`;
268
+ hooks[getAllHookName] = function useAll({ index, skip, key, } = {}) {
269
+ const storage = useStorage();
270
+ // assumptions: this query getter is fast and returns the same
271
+ // query identity for subsequent calls.
272
+ const liveQuery = useMemo(() => (skip ? null : storage[name].findAll({ index, key })), [index, skip]);
273
+ const data = useLiveQuery(liveQuery);
274
+ return data || [];
275
+ };
276
+ const getAllPaginatedHookName = `useAll${capitalize(collection.pluralName || collection.name + 's')}Paginated`;
277
+ hooks[getAllPaginatedHookName] = function useAllPaginated({ index, skip, pageSize = 10, key, } = {}) {
278
+ const storage = useStorage();
279
+ // assumptions: this query getter is fast and returns the same
280
+ // query identity for subsequent calls.
281
+ const liveQuery = useMemo(() => skip
282
+ ? null
283
+ : storage[name].findPage({
284
+ index,
285
+ pageSize,
286
+ page: 0,
287
+ key: key || getAllPaginatedHookName,
288
+ }), [index, skip, pageSize]);
289
+ const data = useLiveQuery(liveQuery);
290
+ const tools = useMemo(() => ({
291
+ next: () => liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.nextPage(),
292
+ previous: () => liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.previousPage(),
293
+ setPage: (page) => liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.setPage(page),
294
+ get hasPrevious() {
295
+ return liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.hasPreviousPage;
296
+ },
297
+ get hasNext() {
298
+ return liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.hasNextPage;
299
+ },
300
+ }), [liveQuery]);
301
+ return [data, tools];
302
+ };
303
+ const getAllInfiniteHookName = `useAll${capitalize(collection.pluralName || collection.name + 's')}Infinite`;
304
+ hooks[getAllInfiniteHookName] = function useAllInfinite({ index, skip, pageSize = 10, key, } = {}) {
305
+ const storage = useStorage();
306
+ // assumptions: this query getter is fast and returns the same
307
+ // query identity for subsequent calls.
308
+ const liveQuery = useMemo(() => skip
309
+ ? null
310
+ : storage[name].findAllInfinite({
311
+ index,
312
+ pageSize,
313
+ key: key || getAllInfiniteHookName,
314
+ }), [index, skip, pageSize]);
315
+ const data = useLiveQuery(liveQuery);
316
+ const tools = useMemo(() => ({
317
+ loadMore: () => liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.loadMore(),
318
+ get hasMore() {
319
+ return liveQuery === null || liveQuery === void 0 ? void 0 : liveQuery.hasMore;
320
+ },
321
+ }), [liveQuery]);
322
+ return [data, tools];
323
+ };
324
+ }
325
+ hooks.withMutations = (mutations) => {
326
+ const augmentedHooks = Object.assign({}, hooks);
327
+ for (const [name, subHook] of Object.entries(mutations)) {
328
+ augmentedHooks[name] = (...args) => {
329
+ const client = hooks.useClient();
330
+ return subHook(client, ...args);
331
+ };
332
+ }
333
+ return augmentedHooks;
334
+ };
335
+ return hooks;
336
+ }
337
337
  //# sourceMappingURL=hooks.js.map