@powerhousedao/reactor-browser 6.2.3-staging.0 → 6.3.0-dev.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/dist/{attachment-service-But1Xljo.d.ts → attachment-service-B-AmTrs2.d.ts} +605 -56
- package/dist/attachment-service-B-AmTrs2.d.ts.map +1 -0
- package/dist/auth-DRbzRdeC.d.ts +32 -0
- package/dist/auth-DRbzRdeC.d.ts.map +1 -0
- package/dist/{client-BiJAJ9dW.js → client-BFnMV-EO.js} +39 -22
- package/dist/client-BFnMV-EO.js.map +1 -0
- package/dist/{client-CngU4mtn.d.ts → client-BP0hXjqq.d.ts} +3 -2
- package/dist/client-BP0hXjqq.d.ts.map +1 -0
- package/dist/client-wwavHFfs.js +135 -0
- package/dist/client-wwavHFfs.js.map +1 -0
- package/dist/{dispatch-qRXS70fh.js → dispatch-Cylwl2Dk.js} +363 -44
- package/dist/dispatch-Cylwl2Dk.js.map +1 -0
- package/dist/{document-by-id-B_8DA4yZ.js → document-by-id-DdvqanGR.js} +2 -2
- package/dist/{document-by-id-B_8DA4yZ.js.map → document-by-id-DdvqanGR.js.map} +1 -1
- package/dist/document-model-modules-BG-61uke.js +710 -0
- package/dist/document-model-modules-BG-61uke.js.map +1 -0
- package/dist/{document-operations-Bt-1-ThH.js → document-operations-DRMMwmGZ.js} +2160 -181
- package/dist/document-operations-DRMMwmGZ.js.map +1 -0
- package/dist/global-iARBhxNI.d.ts +612 -0
- package/dist/global-iARBhxNI.d.ts.map +1 -0
- package/dist/{index-BABMK8cO.d.ts → index-CNS_9THu.d.ts} +14 -4
- package/dist/index-CNS_9THu.d.ts.map +1 -0
- package/dist/index.d.ts +560 -79
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +790 -317
- package/dist/index.js.map +1 -1
- package/dist/inspector-proxy-DODqs15F.d.ts +25 -0
- package/dist/inspector-proxy-DODqs15F.d.ts.map +1 -0
- package/dist/make-ph-event-functions-B5GU5xcR.js +180 -0
- package/dist/make-ph-event-functions-B5GU5xcR.js.map +1 -0
- package/dist/{relational-ChC_AWAK.js → relational-DZuyKp2y.js} +65 -5
- package/dist/relational-DZuyKp2y.js.map +1 -0
- package/dist/{renown-C9pYZRqf.js → renown-B3MUaiq7.js} +56 -25
- package/dist/renown-B3MUaiq7.js.map +1 -0
- package/dist/renown-CBoTdGrg.js +394 -0
- package/dist/renown-CBoTdGrg.js.map +1 -0
- package/dist/{client-VV9M73s_.js → schema-B5lftDPf.js} +295 -195
- package/dist/schema-B5lftDPf.js.map +1 -0
- package/dist/{types-B7-_l_wE.d.ts → schema-C6ECceaN.d.ts} +272 -82
- package/dist/schema-C6ECceaN.d.ts.map +1 -0
- package/dist/src/ai/index.d.ts +269 -24
- package/dist/src/ai/index.d.ts.map +1 -1
- package/dist/src/ai/index.js +1028 -168
- package/dist/src/ai/index.js.map +1 -1
- package/dist/src/analytics/index.js +3 -3
- package/dist/src/graphql/client.d.ts +2 -2
- package/dist/src/graphql/client.js +1 -1
- package/dist/src/graphql-client/entry.d.ts +6 -4
- package/dist/src/graphql-client/entry.js +4 -4
- package/dist/src/relational/index.d.ts +8 -1
- package/dist/src/relational/index.d.ts.map +1 -1
- package/dist/src/relational/index.js +1 -1
- package/dist/src/renown/index.d.ts +2 -2
- package/dist/src/renown/index.js +3 -3
- package/dist/src/rpc/index.d.ts +201 -144
- package/dist/src/rpc/index.d.ts.map +1 -1
- package/dist/src/rpc/index.js +562 -634
- package/dist/src/rpc/index.js.map +1 -1
- package/dist/switchboard-directory-vH6qtkh-.js +435 -0
- package/dist/switchboard-directory-vH6qtkh-.js.map +1 -0
- package/dist/switchboards-D-_JBUbF.d.ts +75 -0
- package/dist/switchboards-D-_JBUbF.d.ts.map +1 -0
- package/dist/types-BWx6FPO4.d.ts +63 -0
- package/dist/types-BWx6FPO4.d.ts.map +1 -0
- package/package.json +19 -17
- package/dist/attachment-service-But1Xljo.d.ts.map +0 -1
- package/dist/client-BiJAJ9dW.js.map +0 -1
- package/dist/client-CngU4mtn.d.ts.map +0 -1
- package/dist/client-VV9M73s_.js.map +0 -1
- package/dist/dispatch-qRXS70fh.js.map +0 -1
- package/dist/document-model-modules-D6I-UUXq.js +0 -386
- package/dist/document-model-modules-D6I-UUXq.js.map +0 -1
- package/dist/document-operations-Bt-1-ThH.js.map +0 -1
- package/dist/global-fql2bmb0.d.ts +0 -438
- package/dist/global-fql2bmb0.d.ts.map +0 -1
- package/dist/index-BABMK8cO.d.ts.map +0 -1
- package/dist/inspector-proxy-DCowaeeH.d.ts +0 -264
- package/dist/inspector-proxy-DCowaeeH.d.ts.map +0 -1
- package/dist/make-ph-event-functions-FzpvQtj6.js +0 -53
- package/dist/make-ph-event-functions-FzpvQtj6.js.map +0 -1
- package/dist/relational-ChC_AWAK.js.map +0 -1
- package/dist/renown-C9pYZRqf.js.map +0 -1
- package/dist/renown-Def5iO8k.js +0 -97
- package/dist/renown-Def5iO8k.js.map +0 -1
- package/dist/switchboard-CFyD_5_e.js +0 -323
- package/dist/switchboard-CFyD_5_e.js.map +0 -1
- package/dist/types-B7-_l_wE.d.ts.map +0 -1
|
@@ -1,25 +1,86 @@
|
|
|
1
|
-
import { t as makePHEventFunctions } from "./make-ph-event-functions-
|
|
1
|
+
import { c as getPHStore, t as makePHEventFunctions } from "./make-ph-event-functions-B5GU5xcR.js";
|
|
2
|
+
import { use, useCallback, useEffect, useMemo, useReducer, useRef, useSyncExternalStore } from "react";
|
|
2
3
|
import { logger } from "document-model";
|
|
3
|
-
import {
|
|
4
|
+
import { operationOutcome } from "@powerhousedao/shared/document-model";
|
|
5
|
+
//#region src/errors.ts
|
|
6
|
+
var UnsupportedDocumentTypeError = class extends Error {
|
|
7
|
+
constructor(documentType) {
|
|
8
|
+
super(`Document type ${documentType} is not supported`);
|
|
9
|
+
this.name = "UnsupportedDocumentTypeError";
|
|
10
|
+
}
|
|
11
|
+
static isError(error) {
|
|
12
|
+
return Error.isError(error) && error.name === "UnsupportedDocumentTypeError";
|
|
13
|
+
}
|
|
14
|
+
};
|
|
15
|
+
var DocumentModelNotFoundError = class extends Error {
|
|
16
|
+
documentType;
|
|
17
|
+
name = "DocumentModelNotFoundError";
|
|
18
|
+
constructor(documentType) {
|
|
19
|
+
super(`Document model module for type ${documentType} not found`);
|
|
20
|
+
this.documentType = documentType;
|
|
21
|
+
}
|
|
22
|
+
static isError(error) {
|
|
23
|
+
return Error.isError(error) && error.name === "DocumentModelNotFoundError";
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
var DocumentTypeMismatchError = class extends Error {
|
|
27
|
+
constructor(documentId, expectedType, actualType) {
|
|
28
|
+
super(`Document ${documentId} is not of type ${expectedType}. Actual type: ${actualType}`);
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
var NoSelectedDocumentError = class extends Error {
|
|
32
|
+
constructor() {
|
|
33
|
+
super("There is no selected document. Did you mean to call 'useSelectedDocumentSafe' instead?");
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
/** A dispatched action the document did not apply: its operation is stored at `index`, state
|
|
37
|
+
* unchanged, because the reducer threw (`reducer-error`) or the write was denied (`denied`). */
|
|
38
|
+
var OperationRejectedError = class OperationRejectedError extends Error {
|
|
39
|
+
documentId;
|
|
40
|
+
scope;
|
|
41
|
+
name = "OperationRejectedError";
|
|
42
|
+
actionId;
|
|
43
|
+
actionType;
|
|
44
|
+
index;
|
|
45
|
+
kind;
|
|
46
|
+
constructor(documentId, scope, operation, outcome) {
|
|
47
|
+
super(outcome.kind === "denied" ? outcome.reason : outcome.message);
|
|
48
|
+
this.documentId = documentId;
|
|
49
|
+
this.scope = scope;
|
|
50
|
+
this.actionId = operation.action.id;
|
|
51
|
+
this.actionType = operation.action.type;
|
|
52
|
+
this.index = operation.index;
|
|
53
|
+
this.kind = outcome.kind;
|
|
54
|
+
}
|
|
55
|
+
/** The rejection `operation` records, or undefined when it applied. */
|
|
56
|
+
static fromOperation(documentId, scope, operation) {
|
|
57
|
+
const outcome = operationOutcome(operation);
|
|
58
|
+
return outcome.kind === "applied" ? void 0 : new OperationRejectedError(documentId, scope, operation, outcome);
|
|
59
|
+
}
|
|
60
|
+
static isError(error) {
|
|
61
|
+
return Error.isError(error) && error.name === "OperationRejectedError";
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
//#endregion
|
|
4
65
|
//#region src/actions/queue.ts
|
|
5
66
|
async function queueActions(document, actionOrActions) {
|
|
6
67
|
if (!document) throw new Error("No document provided");
|
|
7
68
|
if (!actionOrActions) throw new Error("No actions provided");
|
|
8
69
|
const actions = Array.isArray(actionOrActions) ? actionOrActions : [actionOrActions];
|
|
9
70
|
if (actions.length === 0) throw new Error("No actions provided");
|
|
10
|
-
const reactorClient =
|
|
71
|
+
const reactorClient = getPHStore()?.reactorClient;
|
|
11
72
|
if (!reactorClient) throw new Error("ReactorClient not initialized");
|
|
12
|
-
return await reactorClient.execute(document.header.id, "main", actions);
|
|
73
|
+
return await (await reactorClient.write.execute(document.header.id, "main", actions)).document();
|
|
13
74
|
}
|
|
14
75
|
async function queueOperations(documentId, operationOrOperations) {
|
|
15
76
|
if (!documentId) throw new Error("No documentId provided");
|
|
16
77
|
if (!operationOrOperations) throw new Error("No operations provided");
|
|
17
78
|
const operations = Array.isArray(operationOrOperations) ? operationOrOperations : [operationOrOperations];
|
|
18
79
|
if (operations.length === 0) throw new Error("No operations provided");
|
|
19
|
-
const reactorClient =
|
|
80
|
+
const reactorClient = getPHStore()?.reactorClient;
|
|
20
81
|
if (!reactorClient) throw new Error("ReactorClient not initialized");
|
|
21
82
|
const actions = operations.map((op) => op.action);
|
|
22
|
-
return await reactorClient.execute(documentId, "main", actions);
|
|
83
|
+
return await (await reactorClient.write.execute(documentId, "main", actions)).document();
|
|
23
84
|
}
|
|
24
85
|
async function uploadOperations(documentId, allOperations, pushOperations, options) {
|
|
25
86
|
const operationsLimit = options?.operationsLimit || 50;
|
|
@@ -49,7 +110,7 @@ async function uploadOperations(documentId, allOperations, pushOperations, optio
|
|
|
49
110
|
//#endregion
|
|
50
111
|
//#region src/actions/sign.ts
|
|
51
112
|
function addActionContext(action) {
|
|
52
|
-
const renown =
|
|
113
|
+
const renown = getPHStore()?.renown;
|
|
53
114
|
if (!renown?.user) return action;
|
|
54
115
|
return {
|
|
55
116
|
context: { signer: {
|
|
@@ -89,20 +150,56 @@ function makeSignedActionsWithContext(actionOrActions, document) {
|
|
|
89
150
|
//#region src/actions/dispatch.ts
|
|
90
151
|
async function getDocument(documentId) {
|
|
91
152
|
try {
|
|
92
|
-
return await
|
|
153
|
+
return await getPHStore()?.reactorClient?.get(documentId);
|
|
93
154
|
} catch (error) {
|
|
94
155
|
logger.debug(`Failed to get document with id ${documentId}:`, error);
|
|
95
156
|
return;
|
|
96
157
|
}
|
|
97
158
|
}
|
|
98
|
-
function
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
159
|
+
async function getOperationsSince(documentId, scope, sinceRevision) {
|
|
160
|
+
const reactorClient = getPHStore()?.reactorClient;
|
|
161
|
+
if (!reactorClient) return [];
|
|
162
|
+
const operations = [];
|
|
163
|
+
let paging;
|
|
164
|
+
do {
|
|
165
|
+
const page = await reactorClient.getOperations(documentId, {
|
|
166
|
+
branch: "main",
|
|
167
|
+
scopes: [scope]
|
|
168
|
+
}, { sinceRevision }, paging);
|
|
169
|
+
operations.push(...page.results);
|
|
170
|
+
paging = page.nextCursor && page.results.length ? {
|
|
171
|
+
cursor: page.nextCursor,
|
|
172
|
+
limit: page.options.limit
|
|
173
|
+
} : void 0;
|
|
174
|
+
} while (paging);
|
|
175
|
+
return operations;
|
|
176
|
+
}
|
|
177
|
+
/** The operations the dispatch produced in `scope`: a remote client returns them on the
|
|
178
|
+
* document, the local clients do not, so they are read from the revision before it. */
|
|
179
|
+
async function getDispatchedOperations(before, result, scope, actionIds) {
|
|
180
|
+
const returned = result.operations?.[scope];
|
|
181
|
+
if (returned?.some((op) => actionIds.has(op.action.id))) return returned;
|
|
182
|
+
return getOperationsSince(result.header.id, scope, before.header.revision[scope] ?? 0);
|
|
183
|
+
}
|
|
184
|
+
async function getActionErrors(before, result, actions) {
|
|
185
|
+
const actionIds = new Set(actions.map((action) => action.id));
|
|
186
|
+
const errors = [];
|
|
187
|
+
for (const scope of new Set(actions.map((action) => action.scope))) {
|
|
188
|
+
let operations;
|
|
189
|
+
try {
|
|
190
|
+
operations = await getDispatchedOperations(before, result, scope, actionIds);
|
|
191
|
+
} catch (error) {
|
|
192
|
+
logger.warn("Could not read the dispatched operations of @scope: @error", scope, error);
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
for (const action of actions) {
|
|
196
|
+
if (action.scope !== scope) continue;
|
|
197
|
+
const operation = operations.findLast((op) => op.action.id === action.id);
|
|
198
|
+
const error = operation && OperationRejectedError.fromOperation(result.header.id, scope, operation);
|
|
199
|
+
if (error) errors.push(error);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
return errors;
|
|
106
203
|
}
|
|
107
204
|
async function dispatchActions(actionOrActions, documentOrDocumentId, onErrors, onSuccess) {
|
|
108
205
|
const document = typeof documentOrDocumentId === "string" ? await getDocument(documentOrDocumentId) : documentOrDocumentId;
|
|
@@ -120,11 +217,9 @@ async function dispatchActions(actionOrActions, documentOrDocumentId, onErrors,
|
|
|
120
217
|
return;
|
|
121
218
|
}
|
|
122
219
|
const result = await queueActions(document, signedActionsWithContext);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
}
|
|
127
|
-
if (onSuccess && result) onSuccess(result);
|
|
220
|
+
const rejected = onErrors || onSuccess ? await getActionErrors(document, result, signedActionsWithContext) : [];
|
|
221
|
+
if (onErrors && rejected.length) onErrors(rejected);
|
|
222
|
+
onSuccess?.(result, { rejected });
|
|
128
223
|
return result;
|
|
129
224
|
}
|
|
130
225
|
//#endregion
|
|
@@ -134,7 +229,7 @@ const IDLE_REFETCH_STATE = Object.freeze({
|
|
|
134
229
|
isRefetching: false,
|
|
135
230
|
error: void 0
|
|
136
231
|
});
|
|
137
|
-
function fulfilledPromise(value) {
|
|
232
|
+
function fulfilledPromise$1(value) {
|
|
138
233
|
const promise = Promise.resolve(value);
|
|
139
234
|
promise.status = "fulfilled";
|
|
140
235
|
promise.value = value;
|
|
@@ -147,7 +242,11 @@ function rejectedPromise(reason) {
|
|
|
147
242
|
promise.catch(() => void 0);
|
|
148
243
|
return promise;
|
|
149
244
|
}
|
|
150
|
-
const MISSING_DOCUMENT_ERRORS = [
|
|
245
|
+
const MISSING_DOCUMENT_ERRORS = [
|
|
246
|
+
"DocumentNotFoundError",
|
|
247
|
+
"DocumentPurgedError",
|
|
248
|
+
"DocumentDeletedError"
|
|
249
|
+
];
|
|
151
250
|
function isMissingDocumentError(error) {
|
|
152
251
|
if (!(error instanceof Error)) return false;
|
|
153
252
|
return MISSING_DOCUMENT_ERRORS.includes(error.name) || error.message.startsWith("Document not found");
|
|
@@ -206,7 +305,7 @@ var DocumentRefetcher = class {
|
|
|
206
305
|
const document = await this.host.fetch(id);
|
|
207
306
|
if (isCurrent()) {
|
|
208
307
|
this.host.onFetched?.(id, document);
|
|
209
|
-
this.host.documents.set(id, fulfilledPromise(document));
|
|
308
|
+
this.host.documents.set(id, fulfilledPromise$1(document));
|
|
210
309
|
this.finish(id, entry, void 0);
|
|
211
310
|
}
|
|
212
311
|
return document;
|
|
@@ -267,8 +366,12 @@ function isDuplicateModuleError(error) {
|
|
|
267
366
|
function isDuplicateManifestError(error) {
|
|
268
367
|
return Error.isError(error) && error.name === "DuplicateManifestError";
|
|
269
368
|
}
|
|
369
|
+
/** The error name a reactor reports for a job it does not know. */
|
|
370
|
+
const JOB_NOT_FOUND_ERROR_NAME = "JobNotFoundError";
|
|
270
371
|
//#endregion
|
|
271
372
|
//#region src/document-cache.ts
|
|
373
|
+
/** The most ids one `getMany` call is asked for. */
|
|
374
|
+
const GET_MANY_CHUNK_SIZE = 100;
|
|
272
375
|
function addPromiseState(promise) {
|
|
273
376
|
if ("status" in promise) return promise;
|
|
274
377
|
const promiseWithState = promise;
|
|
@@ -313,6 +416,39 @@ function isOperationCache(cache) {
|
|
|
313
416
|
const candidate = cache;
|
|
314
417
|
return typeof candidate.getOperationsState === "function" && typeof candidate.loadOperations === "function" && typeof candidate.loadMoreOperations === "function" && typeof candidate.invalidateOperations === "function" && typeof candidate.subscribeOperations === "function";
|
|
315
418
|
}
|
|
419
|
+
/** The snapshot every unloaded type reads as, frozen so `useSyncExternalStore` sees one value. */
|
|
420
|
+
const IDLE_DOCUMENTS_OF_TYPE_ENTRY = Object.freeze({
|
|
421
|
+
status: "idle",
|
|
422
|
+
ids: Object.freeze([]),
|
|
423
|
+
error: void 0,
|
|
424
|
+
hasNextPage: false
|
|
425
|
+
});
|
|
426
|
+
/** Whether a document cache also lists documents by type. */
|
|
427
|
+
function isDocumentTypeCache(cache) {
|
|
428
|
+
if (typeof cache !== "object" || cache === null) return false;
|
|
429
|
+
const candidate = cache;
|
|
430
|
+
return typeof candidate.getDocumentsOfTypeState === "function" && typeof candidate.loadDocumentsOfType === "function" && typeof candidate.loadMoreDocumentsOfType === "function" && typeof candidate.subscribeDocumentsOfType === "function";
|
|
431
|
+
}
|
|
432
|
+
function createDeferred() {
|
|
433
|
+
let resolve;
|
|
434
|
+
let reject;
|
|
435
|
+
return {
|
|
436
|
+
promise: new Promise((res, rej) => {
|
|
437
|
+
resolve = res;
|
|
438
|
+
reject = rej;
|
|
439
|
+
}),
|
|
440
|
+
resolve,
|
|
441
|
+
reject
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
/** A promise already settled with `value`, readable without awaiting. */
|
|
445
|
+
function fulfilledPromise(value) {
|
|
446
|
+
const promise = Promise.resolve(value);
|
|
447
|
+
promise.status = "fulfilled";
|
|
448
|
+
promise.value = value;
|
|
449
|
+
return promise;
|
|
450
|
+
}
|
|
451
|
+
const typeKey = (type, limit) => `${limit}:${type}`;
|
|
316
452
|
/**
|
|
317
453
|
* Page size used to refresh a scope whose request record has gone missing.
|
|
318
454
|
* A loaded scope always has one, so this is a floor rather than a default.
|
|
@@ -331,13 +467,14 @@ function highestOperationIndex(operations) {
|
|
|
331
467
|
/**
|
|
332
468
|
* Document cache implementation that uses the new ReactorClient API.
|
|
333
469
|
*
|
|
334
|
-
* This cache subscribes to document change events
|
|
470
|
+
* This cache subscribes to document change events through the client's read half
|
|
335
471
|
* and automatically updates the cache when documents are created, updated, or deleted.
|
|
472
|
+
* It is built over a browser client, whose `read` half it uses, or over a read
|
|
473
|
+
* half directly.
|
|
336
474
|
* It also holds paginated operation history per document scope (see `IOperationCache`),
|
|
337
475
|
* which is dropped for a document whenever a change event for it arrives.
|
|
338
476
|
*/
|
|
339
477
|
var DocumentCache = class {
|
|
340
|
-
client;
|
|
341
478
|
documents = /* @__PURE__ */ new Map();
|
|
342
479
|
batchPromises = /* @__PURE__ */ new Map();
|
|
343
480
|
listeners = /* @__PURE__ */ new Map();
|
|
@@ -362,14 +499,20 @@ var DocumentCache = class {
|
|
|
362
499
|
/** In-flight controllers and the next page's cursor/limit, by document id then scope. */
|
|
363
500
|
operationRequests = /* @__PURE__ */ new Map();
|
|
364
501
|
operationListeners = /* @__PURE__ */ new Map();
|
|
502
|
+
/** Documents by type and page size, keyed by typeKey. */
|
|
503
|
+
typeEntries = /* @__PURE__ */ new Map();
|
|
504
|
+
typeRequests = /* @__PURE__ */ new Map();
|
|
505
|
+
typeListeners = /* @__PURE__ */ new Map();
|
|
365
506
|
unsubscribe = null;
|
|
507
|
+
client;
|
|
366
508
|
constructor(client) {
|
|
367
|
-
this.client = client;
|
|
368
|
-
this.unsubscribe = client.subscribe({}, (event) => {
|
|
509
|
+
this.client = readerOf(client);
|
|
510
|
+
this.unsubscribe = this.client.subscribe({}, (event) => {
|
|
369
511
|
this.handleDocumentChange(event);
|
|
370
512
|
});
|
|
371
513
|
}
|
|
372
514
|
handleDocumentChange(event) {
|
|
515
|
+
this.reloadTypesChangedBy(event);
|
|
373
516
|
if (event.type === DOCUMENT_CHANGE_TYPE.Deleted) {
|
|
374
517
|
const documentId = event.context?.childId;
|
|
375
518
|
if (documentId) {
|
|
@@ -433,6 +576,7 @@ var DocumentCache = class {
|
|
|
433
576
|
const key = ids.join(",");
|
|
434
577
|
const cached = this.batchPromises.get(key);
|
|
435
578
|
const hasDeletedDocuments = ids.some((id) => !this.documents.has(id));
|
|
579
|
+
if (hasDeletedDocuments) this.loadMany(ids);
|
|
436
580
|
const currentPromises = ids.map((id) => this.get(id));
|
|
437
581
|
if (hasDeletedDocuments) {
|
|
438
582
|
const batchPromise = fulfilledOnly(currentPromises);
|
|
@@ -465,6 +609,57 @@ var DocumentCache = class {
|
|
|
465
609
|
});
|
|
466
610
|
return batchPromise;
|
|
467
611
|
}
|
|
612
|
+
/**
|
|
613
|
+
* Reserves an entry for every id not yet cached and fills them from
|
|
614
|
+
* `getMany`, in chunks of {@link GET_MANY_CHUNK_SIZE}. Each id keeps its own
|
|
615
|
+
* entry, settled as `get` would settle it.
|
|
616
|
+
*/
|
|
617
|
+
loadMany(ids) {
|
|
618
|
+
const getMany = this.client.getMany?.bind(this.client);
|
|
619
|
+
const missing = [...new Set(ids)].filter((id) => !this.documents.has(id));
|
|
620
|
+
if (!getMany || missing.length < 2) return;
|
|
621
|
+
const pending = /* @__PURE__ */ new Map();
|
|
622
|
+
for (const id of missing) {
|
|
623
|
+
const deferred = createDeferred();
|
|
624
|
+
pending.set(id, deferred);
|
|
625
|
+
this.refetcher.forget(id);
|
|
626
|
+
this.documents.set(id, addPromiseState(deferred.promise));
|
|
627
|
+
}
|
|
628
|
+
for (let i = 0; i < missing.length; i += 100) this.settleMany(missing.slice(i, i + 100), pending, getMany);
|
|
629
|
+
}
|
|
630
|
+
/**
|
|
631
|
+
* `getMany` rejects when any id is unavailable, so a failed chunk is split
|
|
632
|
+
* in half and retried; a single id is read with `get`, so it settles exactly
|
|
633
|
+
* as a single read would.
|
|
634
|
+
*/
|
|
635
|
+
async settleMany(ids, pending, getMany) {
|
|
636
|
+
if (ids.length === 1) {
|
|
637
|
+
const id = ids[0];
|
|
638
|
+
try {
|
|
639
|
+
this.settleFetched(id, await this.client.get(id), pending);
|
|
640
|
+
} catch (error) {
|
|
641
|
+
pending.get(id)?.reject(error);
|
|
642
|
+
}
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
let documents;
|
|
646
|
+
try {
|
|
647
|
+
documents = await getMany(ids);
|
|
648
|
+
} catch {
|
|
649
|
+
const half = Math.ceil(ids.length / 2);
|
|
650
|
+
await Promise.all([this.settleMany(ids.slice(0, half), pending, getMany), this.settleMany(ids.slice(half), pending, getMany)]);
|
|
651
|
+
return;
|
|
652
|
+
}
|
|
653
|
+
if (documents.length !== ids.length) {
|
|
654
|
+
await Promise.all(ids.map((id) => this.settleMany([id], pending, getMany)));
|
|
655
|
+
return;
|
|
656
|
+
}
|
|
657
|
+
ids.forEach((id, i) => this.settleFetched(id, documents[i], pending));
|
|
658
|
+
}
|
|
659
|
+
settleFetched(id, document, pending) {
|
|
660
|
+
if (document.header.id !== id) this.recordAlias(document.header.id, id);
|
|
661
|
+
pending.get(id)?.resolve(document);
|
|
662
|
+
}
|
|
468
663
|
subscribe(id, callback) {
|
|
469
664
|
const ids = Array.isArray(id) ? id : [id];
|
|
470
665
|
for (const docId of ids) {
|
|
@@ -633,6 +828,106 @@ var DocumentCache = class {
|
|
|
633
828
|
notifyOperationListeners(documentId) {
|
|
634
829
|
for (const listener of this.operationListeners.get(documentId) ?? []) listener();
|
|
635
830
|
}
|
|
831
|
+
getDocumentsOfTypeState(type, limit) {
|
|
832
|
+
return this.typeEntries.get(typeKey(type, limit)) ?? IDLE_DOCUMENTS_OF_TYPE_ENTRY;
|
|
833
|
+
}
|
|
834
|
+
loadDocumentsOfType(type, limit) {
|
|
835
|
+
const key = typeKey(type, limit);
|
|
836
|
+
const request = this.typeRequests.get(key);
|
|
837
|
+
if (request) return request.firstPage;
|
|
838
|
+
const controller = new AbortController();
|
|
839
|
+
const created = {
|
|
840
|
+
type,
|
|
841
|
+
limit,
|
|
842
|
+
firstPage: fulfilledPromise(void 0),
|
|
843
|
+
controller,
|
|
844
|
+
nextCursor: void 0
|
|
845
|
+
};
|
|
846
|
+
this.typeRequests.set(key, created);
|
|
847
|
+
created.firstPage = addPromiseState(this.loadTypePage(key, "", limit, controller, []));
|
|
848
|
+
return created.firstPage;
|
|
849
|
+
}
|
|
850
|
+
loadMoreDocumentsOfType(type, limit) {
|
|
851
|
+
const key = typeKey(type, limit);
|
|
852
|
+
const entry = this.getDocumentsOfTypeState(type, limit);
|
|
853
|
+
const request = this.typeRequests.get(key);
|
|
854
|
+
if (entry.status !== "success" || !request?.nextCursor) return;
|
|
855
|
+
this.loadTypePage(key, request.nextCursor, limit, request.controller, entry.ids);
|
|
856
|
+
}
|
|
857
|
+
subscribeDocumentsOfType(type, limit, callback) {
|
|
858
|
+
const key = typeKey(type, limit);
|
|
859
|
+
this.typeListeners.set(key, [...this.typeListeners.get(key) ?? [], callback]);
|
|
860
|
+
return () => {
|
|
861
|
+
const current = this.typeListeners.get(key) ?? [];
|
|
862
|
+
this.typeListeners.set(key, current.filter((listener) => listener !== callback));
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
/** Loads one page after `before` and appends it; never rejects, the entry holds the error. */
|
|
866
|
+
async loadTypePage(key, cursor, limit, controller, before) {
|
|
867
|
+
const type = this.typeRequests.get(key)?.type ?? "";
|
|
868
|
+
this.setTypeEntry(key, {
|
|
869
|
+
...this.getTypeEntry(key),
|
|
870
|
+
status: "pending"
|
|
871
|
+
});
|
|
872
|
+
try {
|
|
873
|
+
const page = await this.client.find({ type }, void 0, {
|
|
874
|
+
cursor,
|
|
875
|
+
limit
|
|
876
|
+
}, controller.signal);
|
|
877
|
+
if (controller.signal.aborted) return;
|
|
878
|
+
for (const document of page.results) this.primeDocument(document);
|
|
879
|
+
const request = this.typeRequests.get(key);
|
|
880
|
+
if (request) request.nextCursor = page.nextCursor;
|
|
881
|
+
this.setTypeEntry(key, {
|
|
882
|
+
status: "success",
|
|
883
|
+
ids: [...before, ...page.results.map((d) => d.header.id)],
|
|
884
|
+
error: void 0,
|
|
885
|
+
hasNextPage: !!page.nextCursor
|
|
886
|
+
});
|
|
887
|
+
} catch (error) {
|
|
888
|
+
if (controller.signal.aborted) return;
|
|
889
|
+
this.setTypeEntry(key, {
|
|
890
|
+
...this.getTypeEntry(key),
|
|
891
|
+
status: "error",
|
|
892
|
+
error
|
|
893
|
+
});
|
|
894
|
+
}
|
|
895
|
+
}
|
|
896
|
+
/** Reloads every loaded page of a type a created or deleted document belongs to. */
|
|
897
|
+
reloadTypesChangedBy(event) {
|
|
898
|
+
if (this.typeRequests.size === 0) return;
|
|
899
|
+
let changed;
|
|
900
|
+
if (event.type === DOCUMENT_CHANGE_TYPE.Created) {
|
|
901
|
+
const types = new Set(event.documents.map((d) => d.header.documentType));
|
|
902
|
+
changed = (_key, type) => types.has(type);
|
|
903
|
+
} else if (event.type === DOCUMENT_CHANGE_TYPE.Deleted) {
|
|
904
|
+
const id = event.context?.childId;
|
|
905
|
+
if (!id) return;
|
|
906
|
+
changed = (key) => this.getTypeEntry(key).ids.includes(id);
|
|
907
|
+
} else return;
|
|
908
|
+
for (const [key, request] of this.typeRequests) {
|
|
909
|
+
if (!changed(key, request.type)) continue;
|
|
910
|
+
request.controller.abort();
|
|
911
|
+
const controller = new AbortController();
|
|
912
|
+
const created = event.type === DOCUMENT_CHANGE_TYPE.Created ? event.documents.filter((d) => d.header.documentType === request.type).length : 0;
|
|
913
|
+
const loaded = this.getTypeEntry(key).ids.length;
|
|
914
|
+
const limit = Math.max(request.limit, loaded + created);
|
|
915
|
+
request.controller = controller;
|
|
916
|
+
this.loadTypePage(key, "", limit, controller, []);
|
|
917
|
+
}
|
|
918
|
+
}
|
|
919
|
+
primeDocument(document) {
|
|
920
|
+
const current = this.documents.get(document.header.id);
|
|
921
|
+
if (current && current.status !== "rejected") return;
|
|
922
|
+
this.documents.set(document.header.id, fulfilledPromise(document));
|
|
923
|
+
}
|
|
924
|
+
getTypeEntry(key) {
|
|
925
|
+
return this.typeEntries.get(key) ?? IDLE_DOCUMENTS_OF_TYPE_ENTRY;
|
|
926
|
+
}
|
|
927
|
+
setTypeEntry(key, entry) {
|
|
928
|
+
this.typeEntries.set(key, entry);
|
|
929
|
+
for (const listener of this.typeListeners.get(key) ?? []) listener();
|
|
930
|
+
}
|
|
636
931
|
/**
|
|
637
932
|
* Disposes of the cache and unsubscribes from document change events.
|
|
638
933
|
*/
|
|
@@ -644,10 +939,18 @@ var DocumentCache = class {
|
|
|
644
939
|
for (const requests of this.operationRequests.values()) for (const request of requests.values()) request.controller.abort();
|
|
645
940
|
this.operationRequests.clear();
|
|
646
941
|
this.operationEntries.clear();
|
|
942
|
+
for (const request of this.typeRequests.values()) request.controller.abort();
|
|
943
|
+
this.typeRequests.clear();
|
|
944
|
+
this.typeEntries.clear();
|
|
945
|
+
this.typeListeners.clear();
|
|
647
946
|
this.refetcher.clear();
|
|
648
947
|
this.operationListeners.clear();
|
|
649
948
|
}
|
|
650
949
|
};
|
|
950
|
+
/** The read half of a browser client, or the reader itself. */
|
|
951
|
+
function readerOf(client) {
|
|
952
|
+
return client.read ?? client;
|
|
953
|
+
}
|
|
651
954
|
//#endregion
|
|
652
955
|
//#region src/hooks/document-cache.ts
|
|
653
956
|
const documentEventFunctions = makePHEventFunctions("documentCache");
|
|
@@ -693,7 +996,8 @@ function getDocumentQueryState(promise) {
|
|
|
693
996
|
*/
|
|
694
997
|
function useDocument(id) {
|
|
695
998
|
const documentCache = useDocumentCache();
|
|
696
|
-
const
|
|
999
|
+
const getDocument = () => id ? documentCache?.get(id) : void 0;
|
|
1000
|
+
const document = useSyncExternalStore((cb) => id && documentCache ? documentCache.subscribe(id, cb) : () => {}, getDocument, getDocument);
|
|
697
1001
|
return document ? use(document) : void 0;
|
|
698
1002
|
}
|
|
699
1003
|
/**
|
|
@@ -718,8 +1022,10 @@ function useDocument(id) {
|
|
|
718
1022
|
function useDocumentSafe(id) {
|
|
719
1023
|
const documentCache = useDocumentCache();
|
|
720
1024
|
const subscribe = (cb) => id && documentCache ? documentCache.subscribe(id, cb) : () => {};
|
|
721
|
-
const
|
|
722
|
-
const
|
|
1025
|
+
const getPromise = () => id ? documentCache?.get(id) : void 0;
|
|
1026
|
+
const getRefetch = () => readRefetchState(documentCache, id);
|
|
1027
|
+
const promise = useSyncExternalStore(subscribe, getPromise, getPromise);
|
|
1028
|
+
const refetch = useSyncExternalStore(subscribe, getRefetch, getRefetch);
|
|
723
1029
|
const [, bumpOnSettle] = useReducer((count) => count + 1, 0);
|
|
724
1030
|
const pendingPromise = promise && readPromiseState(promise).status === "pending" ? promise : null;
|
|
725
1031
|
useEffect(() => {
|
|
@@ -764,7 +1070,8 @@ function readRefetchState(documentCache, id) {
|
|
|
764
1070
|
*/
|
|
765
1071
|
function useDocuments(ids) {
|
|
766
1072
|
const documentCache = useDocumentCache();
|
|
767
|
-
const
|
|
1073
|
+
const getBatch = () => ids?.length && documentCache ? documentCache.getBatch(ids) : void 0;
|
|
1074
|
+
const documents = useSyncExternalStore((cb) => ids?.length && documentCache ? documentCache.subscribe(ids, cb) : () => {}, getBatch, getBatch);
|
|
768
1075
|
return documents ? use(documents) : [];
|
|
769
1076
|
}
|
|
770
1077
|
/**
|
|
@@ -826,29 +1133,41 @@ function useGetDocumentAsync(id) {
|
|
|
826
1133
|
}
|
|
827
1134
|
//#endregion
|
|
828
1135
|
//#region src/hooks/dispatch.ts
|
|
829
|
-
|
|
1136
|
+
/** A dispatch to the document `getDocument` returns when it runs. */
|
|
1137
|
+
function createDispatch(getDocument) {
|
|
830
1138
|
/**
|
|
831
1139
|
* Dispatches actions to the document.
|
|
832
1140
|
* @param actionOrActions - The action or actions to dispatch.
|
|
833
|
-
* @param onErrors - Callback invoked with
|
|
834
|
-
* or with the failure when the dispatch itself did not complete.
|
|
1141
|
+
* @param onErrors - Callback invoked with an `OperationRejectedError` for each action the
|
|
1142
|
+
* document did not apply, or with the failure when the dispatch itself did not complete.
|
|
1143
|
+
* Runs before `onSuccess`.
|
|
1144
|
+
* @param onSuccess - Callback invoked with the document once the write lands, after
|
|
1145
|
+
* `onErrors` and also when an action was rejected; `outcome.rejected` lists the rejections.
|
|
835
1146
|
*/
|
|
836
|
-
function dispatch(actionOrActions, onErrors, onSuccess) {
|
|
1147
|
+
return function dispatch(actionOrActions, onErrors, onSuccess) {
|
|
837
1148
|
let answered = false;
|
|
838
|
-
|
|
1149
|
+
const reportErrors = onErrors && ((errors) => {
|
|
839
1150
|
answered = true;
|
|
840
1151
|
onErrors(errors);
|
|
841
|
-
})
|
|
1152
|
+
});
|
|
1153
|
+
const reportSuccess = onSuccess && ((result, outcome) => {
|
|
842
1154
|
answered = true;
|
|
843
|
-
onSuccess(result);
|
|
844
|
-
})
|
|
1155
|
+
onSuccess(result, outcome);
|
|
1156
|
+
});
|
|
1157
|
+
dispatchActions(actionOrActions, getDocument(), reportErrors, reportSuccess).catch((error) => {
|
|
845
1158
|
logger.error("Failed to dispatch actions: @error", error);
|
|
846
1159
|
if (!answered) onErrors?.([error instanceof Error ? error : new Error(String(error))]);
|
|
847
1160
|
});
|
|
848
|
-
}
|
|
849
|
-
|
|
1161
|
+
};
|
|
1162
|
+
}
|
|
1163
|
+
/** Returns the document and a dispatch to it; see `DispatchFn` for the callbacks' order. */
|
|
1164
|
+
function useDispatch(document) {
|
|
1165
|
+
const latest = useRef(document);
|
|
1166
|
+
latest.current = document;
|
|
1167
|
+
const id = document?.header.id;
|
|
1168
|
+
return [document, useMemo(() => createDispatch(() => latest.current), [id])];
|
|
850
1169
|
}
|
|
851
1170
|
//#endregion
|
|
852
|
-
export {
|
|
1171
|
+
export { DocumentModelNotFoundError as A, isDuplicateModuleError as C, queueActions as D, dispatchActions as E, NoSelectedDocumentError as M, OperationRejectedError as N, queueOperations as O, UnsupportedDocumentTypeError as P, isDuplicateManifestError as S, IDLE_REFETCH_STATE as T, isDocumentTypeCache as _, useDocument as a, DOCUMENT_CHANGE_TYPE as b, useDocuments as c, useGetDocuments as d, DocumentCache as f, addPromiseState as g, IDLE_OPERATIONS_ENTRY as h, setDocumentCache as i, DocumentTypeMismatchError as j, uploadOperations as k, useGetDocument as l, IDLE_DOCUMENTS_OF_TYPE_ENTRY as m, useDispatch as n, useDocumentCache as o, GET_MANY_CHUNK_SIZE as p, addDocumentCacheEventHandler as r, useDocumentSafe as s, createDispatch as t, useGetDocumentAsync as u, isOperationCache as v, DocumentRefetcher as w, JOB_NOT_FOUND_ERROR_NAME as x, readPromiseState as y };
|
|
853
1172
|
|
|
854
|
-
//# sourceMappingURL=dispatch-
|
|
1173
|
+
//# sourceMappingURL=dispatch-Cylwl2Dk.js.map
|