@jsb188/graphql 1.1.10 → 1.1.12

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.
Files changed (176) hide show
  1. package/audience.d.ts +343 -0
  2. package/cache/config.ts +98 -50
  3. package/cache/index.chat-activity.test.ts +119 -0
  4. package/cache/index.chat.test.ts +92 -0
  5. package/cache/index.helpers.ts +96 -0
  6. package/cache/index.regression.test.ts +149 -0
  7. package/cache/index.test.ts +1017 -127
  8. package/cache/index.ts +1256 -1269
  9. package/cache/optimistic-ui-runtime.ts +46 -0
  10. package/cache/optimistic-ui.ts +401 -0
  11. package/cache/persist.ts +800 -0
  12. package/cache/store.ts +395 -0
  13. package/cache/value.helpers.ts +17 -0
  14. package/client/draft-persistence-indexed-db.ts +413 -0
  15. package/client/draft-persistence.ts +152 -0
  16. package/client/index.ts +3 -0
  17. package/client/request.helpers.ts +14 -0
  18. package/client/request.ts +870 -164
  19. package/deno.lock +187 -0
  20. package/deno.test.json +10 -4
  21. package/gql/eagerQueryMap.ts +12 -0
  22. package/gql/fragments/accountAuthFragments.ts +5 -0
  23. package/gql/fragments/accountFragments.ts +0 -3
  24. package/gql/fragments/accountSessionFragments.ts +16 -0
  25. package/gql/fragments/agentMemoryFragments.ts +23 -0
  26. package/gql/fragments/agentNoteFragments.ts +150 -0
  27. package/gql/fragments/aiChatFragments.ts +42 -0
  28. package/gql/fragments/audienceFragments.ts +141 -0
  29. package/gql/fragments/campaignFragments.ts +566 -0
  30. package/gql/fragments/dataTableFragments.ts +50 -192
  31. package/gql/fragments/emailFragments.ts +32 -17
  32. package/gql/fragments/fieldDocFragments.ts +264 -0
  33. package/gql/fragments/inboundContactFragments.ts +2 -0
  34. package/gql/fragments/logFragments.ts +101 -69
  35. package/gql/fragments/organizationFragments.ts +53 -5
  36. package/gql/fragments/qrCodeFragments.ts +29 -0
  37. package/gql/fragments/scheduleFragments.ts +667 -0
  38. package/gql/fragments/sheetFragments.ts +304 -63
  39. package/gql/fragments/storageFragments.ts +45 -0
  40. package/gql/fragments/workflowFragments.ts +41 -5
  41. package/gql/mutations/accountMutations.ts +0 -2
  42. package/gql/mutations/agentMemoryMutations.ts +68 -0
  43. package/gql/mutations/agentNoteMutations.ts +62 -0
  44. package/gql/mutations/aiChatMutations.ts +49 -4
  45. package/gql/mutations/audienceMutations.ts +71 -0
  46. package/gql/mutations/authMutations.ts +203 -18
  47. package/gql/mutations/billingMutations.ts +17 -27
  48. package/gql/mutations/campaignMutations.ts +258 -0
  49. package/gql/mutations/contactMutations.ts +23 -0
  50. package/gql/mutations/dataTableMutations.ts +19 -54
  51. package/gql/mutations/fieldDocMutations.ts +173 -0
  52. package/gql/mutations/index.ts +10 -2
  53. package/gql/mutations/logMutations.ts +6 -29
  54. package/gql/mutations/organizationMutations.ts +19 -4
  55. package/gql/mutations/qrCodeMutations.ts +20 -0
  56. package/gql/mutations/scheduleMutations.ts +1121 -0
  57. package/gql/mutations/sheetMutations.ts +221 -60
  58. package/gql/mutations/storageMutations.ts +19 -0
  59. package/gql/mutations/workflowMutations.ts +60 -0
  60. package/gql/queries/agentMemoryQueries.ts +28 -0
  61. package/gql/queries/agentNoteQueries.ts +89 -0
  62. package/gql/queries/aiChatQueries.ts +70 -2
  63. package/gql/queries/audienceQueries.ts +128 -0
  64. package/gql/queries/authQueries.ts +47 -0
  65. package/gql/queries/billingQueries.ts +118 -6
  66. package/gql/queries/campaignQueries.ts +236 -0
  67. package/gql/queries/dataTableQueries.ts +61 -13
  68. package/gql/queries/emailQueries.ts +26 -17
  69. package/gql/queries/emailReplyReportQueries.ts +76 -0
  70. package/gql/queries/fieldDocQueries.ts +166 -0
  71. package/gql/queries/inboundContactQueries.ts +4 -0
  72. package/gql/queries/index.ts +10 -2
  73. package/gql/queries/logQueries.ts +68 -30
  74. package/gql/queries/organizationQueries.ts +65 -1
  75. package/gql/queries/qrCodeQueries.ts +20 -0
  76. package/gql/queries/scheduleQueries.ts +691 -0
  77. package/gql/queries/sheetQueries.ts +244 -26
  78. package/gql/queries/storageQueries.ts +38 -0
  79. package/gql/queries/workflowQueries.ts +77 -2
  80. package/hooks/index.helpers.test.ts +50 -0
  81. package/hooks/index.helpers.ts +52 -0
  82. package/hooks/index.ts +2254 -982
  83. package/hooks/use-account-mtn.ts +76 -81
  84. package/hooks/use-agentMemory-mtn.ts +71 -0
  85. package/hooks/use-agentMemory-qry.ts +56 -0
  86. package/hooks/use-agentNote-mtn.ts +82 -0
  87. package/hooks/use-agentNote-qry.ts +190 -0
  88. package/hooks/use-agentNote-recovery.helpers.test.ts +68 -0
  89. package/hooks/use-agentNote-recovery.helpers.ts +58 -0
  90. package/hooks/use-agentNote-recovery.ts +72 -0
  91. package/hooks/use-aiChat-mtn.ts +220 -141
  92. package/hooks/use-aiChat-qry.helpers.test.ts +63 -0
  93. package/hooks/use-aiChat-qry.helpers.ts +124 -0
  94. package/hooks/use-aiChat-qry.ts +341 -237
  95. package/hooks/use-aiChat-stream-mtn.ts +14 -0
  96. package/hooks/use-app-qry.ts +12 -9
  97. package/hooks/use-audience-mtn.ts +194 -0
  98. package/hooks/use-audience-qry.helpers.test.ts +80 -0
  99. package/hooks/use-audience-qry.helpers.ts +63 -0
  100. package/hooks/use-audience-qry.ts +330 -0
  101. package/hooks/use-auth-mtn.ts +435 -252
  102. package/hooks/use-auth-qry.ts +51 -0
  103. package/hooks/use-billing-mtn.ts +31 -0
  104. package/hooks/use-billing-qry.ts +76 -0
  105. package/hooks/use-campaign-generation-recovery.ts +53 -0
  106. package/hooks/use-campaign-mtn.ts +436 -0
  107. package/hooks/use-campaign-qry.helpers.ts +40 -0
  108. package/hooks/use-campaign-qry.ts +834 -0
  109. package/hooks/use-dataTable-mtn.ts +37 -46
  110. package/hooks/use-dataTable-qry.ts +102 -128
  111. package/hooks/use-email-qry.helpers.ts +37 -0
  112. package/hooks/use-email-qry.ts +56 -86
  113. package/hooks/use-emailCampaignReplyReport-qry.ts +27 -0
  114. package/hooks/use-fieldDoc-mtn.ts +262 -0
  115. package/hooks/use-fieldDoc-qry.helpers.test.ts +65 -0
  116. package/hooks/use-fieldDoc-qry.helpers.ts +49 -0
  117. package/hooks/use-fieldDoc-qry.ts +212 -0
  118. package/hooks/use-inboundContact-mtn.ts +19 -16
  119. package/hooks/use-inboundContact-qry.ts +62 -56
  120. package/hooks/use-integration-mtn.ts +56 -38
  121. package/hooks/use-integration-qry.ts +24 -9
  122. package/hooks/use-log-mtn.ts +88 -59
  123. package/hooks/use-log-qry.ts +150 -73
  124. package/hooks/use-organization-mtn.ts +334 -277
  125. package/hooks/use-organization-qry.ts +155 -129
  126. package/hooks/use-publicFormEmail-mtn.ts +27 -0
  127. package/hooks/use-qrCode-mtn.ts +40 -0
  128. package/hooks/use-qrCode-qry.ts +78 -0
  129. package/hooks/use-schedule-mtn.ts +589 -0
  130. package/hooks/use-schedule-qry.ts +738 -0
  131. package/hooks/use-schedule-reactive.ts +486 -0
  132. package/hooks/use-sheet-mtn.ts +157 -79
  133. package/hooks/use-sheet-qry.ts +221 -92
  134. package/hooks/use-storage-mtn.ts +81 -41
  135. package/hooks/use-storage-qry.ts +128 -14
  136. package/hooks/use-workflow-mtn.ts +72 -0
  137. package/hooks/use-workflow-qry.ts +184 -47
  138. package/libs/ai-chat-outgoing.helpers.ts +153 -0
  139. package/libs/ai-chat-outgoing.ts +11 -0
  140. package/libs/ai-chat-outgoing.types.ts +25 -0
  141. package/libs/ai-chat-run-realtime.ts +20 -0
  142. package/libs/ai-chat-submission-realtime.ts +150 -0
  143. package/libs/fragment-realtime-sequence.ts +55 -0
  144. package/libs/fragment-recovery.test.ts +326 -0
  145. package/libs/fragment-recovery.ts +325 -0
  146. package/libs/open-sheet.ts +138 -0
  147. package/libs/orchestrator-realtime.helpers.test.ts +229 -0
  148. package/libs/orchestrator-realtime.helpers.ts +317 -0
  149. package/libs/orchestrator-realtime.ts +433 -0
  150. package/libs/request-cache-regression.test.ts +122 -0
  151. package/libs/request-managed-session.test.ts +574 -0
  152. package/libs/request-no-store.test.ts +419 -0
  153. package/libs/sheet-command-batch.ts +44 -0
  154. package/libs/sheet-view-realtime-router.test.ts +214 -0
  155. package/libs/sheet-view-realtime-router.ts +99 -0
  156. package/libs/sse-ticket.test.ts +26 -0
  157. package/loaders/campaign.test.ts +186 -0
  158. package/loaders/campaign.ts +110 -0
  159. package/loaders/emailPreference.ts +75 -0
  160. package/loaders/rest.ts +12 -0
  161. package/package.json +119 -71
  162. package/schedule.d.ts +1790 -0
  163. package/sse.helpers.ts +19 -0
  164. package/sse.ts +70 -106
  165. package/tests/agent-note-detail-cache.test.ts +120 -0
  166. package/tests/agent-note-search.test.ts +444 -0
  167. package/tsconfig.json +11 -10
  168. package/types.d.ts +77 -26
  169. package/utils.ts +78 -49
  170. package/gql/fragments/billingFragments.ts +0 -25
  171. package/gql/fragments/productFragments.ts +0 -39
  172. package/gql/fragments/reportFragments.ts +0 -90
  173. package/gql/queries/productQueries.ts +0 -31
  174. package/gql/queries/reportQueries.ts +0 -134
  175. package/hooks/use-product-qry.ts +0 -67
  176. package/hooks/use-report-qry.ts +0 -153
@@ -0,0 +1,46 @@
1
+ const OPTIMISTIC_UI_RUNTIME = {
2
+ activeFragmentKeys: new Set<string>(),
3
+ nextTransactionId: 0,
4
+ };
5
+
6
+ /*
7
+ * Return one unique client-only token for an optimistic transaction.
8
+ */
9
+ export function getOptimisticUITransactionId() {
10
+ OPTIMISTIC_UI_RUNTIME.nextTransactionId += 1;
11
+ return `optimistic-${OPTIMISTIC_UI_RUNTIME.nextTransactionId}`;
12
+ }
13
+
14
+ /*
15
+ * Acquire a complete set of fragment keys when none are already optimistic.
16
+ */
17
+ export function acquireOptimisticUIFragmentKeys(fragmentKeys: string[]) {
18
+ if (
19
+ fragmentKeys.some((fragmentKey) =>
20
+ OPTIMISTIC_UI_RUNTIME.activeFragmentKeys.has(fragmentKey),
21
+ )
22
+ ) {
23
+ return false;
24
+ }
25
+
26
+ fragmentKeys.forEach((fragmentKey) => {
27
+ OPTIMISTIC_UI_RUNTIME.activeFragmentKeys.add(fragmentKey);
28
+ });
29
+ return true;
30
+ }
31
+
32
+ /*
33
+ * Release fragment keys after their optimistic transaction settles.
34
+ */
35
+ export function releaseOptimisticUIFragmentKeys(fragmentKeys: string[]) {
36
+ fragmentKeys.forEach((fragmentKey) => {
37
+ OPTIMISTIC_UI_RUNTIME.activeFragmentKeys.delete(fragmentKey);
38
+ });
39
+ }
40
+
41
+ /*
42
+ * Return whether one fragment currently belongs to an optimistic transaction.
43
+ */
44
+ export function isOptimisticUIFragmentActive(fragmentKey: string) {
45
+ return OPTIMISTIC_UI_RUNTIME.activeFragmentKeys.has(fragmentKey);
46
+ }
@@ -0,0 +1,401 @@
1
+ import { graphQLValuesEqual } from './value.helpers.ts';
2
+ import type { OptimisticUI, OptimisticUIOperation, UpdateObserversFn } from '../types.d.ts';
3
+ import { addFragmentToCache, loadFragment, triggerObserver } from './index.ts';
4
+ import {
5
+ acquireOptimisticUIFragmentKeys,
6
+ getOptimisticUITransactionId,
7
+ releaseOptimisticUIFragmentKeys,
8
+ } from './optimistic-ui-runtime.ts';
9
+ import { cacheStore } from './store.ts';
10
+
11
+ interface OptimisticPreviousField {
12
+ exists: boolean;
13
+ value: any;
14
+ }
15
+
16
+ interface OptimisticPlaceholderState {
17
+ field: string;
18
+ token: string;
19
+ }
20
+
21
+ interface OptimisticFragmentPlan {
22
+ baseFragment: Record<string, any>;
23
+ fragmentKey: string;
24
+ nextFragment: Record<string, any>;
25
+ optimisticFields: Map<string, any>;
26
+ placeholders: OptimisticPlaceholderState[];
27
+ previousFields: Map<string, OptimisticPreviousField>;
28
+ }
29
+
30
+ export interface OptimisticUITransaction {
31
+ commit: () => void;
32
+ fragmentKeys: string[];
33
+ rollback: () => void;
34
+ }
35
+
36
+ export type OptimisticUITransactionResult =
37
+ | { status: 'applied'; transaction: OptimisticUITransaction }
38
+ | { status: 'conflict'; transaction: null }
39
+ | { status: 'noop'; transaction: null };
40
+
41
+ /*
42
+ * Normalize one optimistic operation or operation list into an array.
43
+ */
44
+ function getOptimisticUIOperations(optimisticUI: OptimisticUI) {
45
+ return Array.isArray(optimisticUI)
46
+ ? [...optimisticUI]
47
+ : [optimisticUI as OptimisticUIOperation];
48
+ }
49
+
50
+ /*
51
+ * Return the normalized fragment key targeted by one optimistic operation.
52
+ */
53
+ function getOptimisticUIOperationFragmentKey(operation: OptimisticUIOperation) {
54
+ return operation.action === 'update'
55
+ ? operation.fragmentKey
56
+ : operation.parentFragmentKey;
57
+ }
58
+
59
+ /*
60
+ * Return or initialize one fragment plan without mutating the cache.
61
+ */
62
+ function getOptimisticFragmentPlan(
63
+ plans: Map<string, OptimisticFragmentPlan>,
64
+ fragmentKey: string,
65
+ ) {
66
+ const currentPlan = plans.get(fragmentKey);
67
+ if (currentPlan) {
68
+ return currentPlan;
69
+ }
70
+
71
+ const fragment = loadFragment(fragmentKey);
72
+ if (!fragment || typeof fragment !== 'object' || Array.isArray(fragment)) {
73
+ return null;
74
+ }
75
+
76
+ const plan: OptimisticFragmentPlan = {
77
+ baseFragment: fragment,
78
+ fragmentKey,
79
+ nextFragment: { ...fragment },
80
+ optimisticFields: new Map(),
81
+ placeholders: [],
82
+ previousFields: new Map(),
83
+ };
84
+ plans.set(fragmentKey, plan);
85
+ return plan;
86
+ }
87
+
88
+ /*
89
+ * Apply one existing-fragment patch to an in-memory transaction plan.
90
+ */
91
+ function applyOptimisticUpdateToPlan(
92
+ plan: OptimisticFragmentPlan,
93
+ operation: Extract<OptimisticUIOperation, { action: 'update' }>,
94
+ ) {
95
+ const update =
96
+ typeof operation.update === 'function'
97
+ ? operation.update({ ...plan.nextFragment })
98
+ : operation.update;
99
+
100
+ if (!update || typeof update !== 'object' || Array.isArray(update)) {
101
+ return;
102
+ }
103
+
104
+ Object.keys(update).forEach((field) => {
105
+ if (!plan.previousFields.has(field)) {
106
+ plan.previousFields.set(field, {
107
+ exists: Object.hasOwn(plan.baseFragment, field),
108
+ value: plan.baseFragment[field],
109
+ });
110
+ }
111
+
112
+ plan.nextFragment[field] = update[field];
113
+ plan.optimisticFields.set(field, update[field]);
114
+ });
115
+ }
116
+
117
+ /*
118
+ * Insert one marked temporary collection object into an in-memory plan.
119
+ */
120
+ function applyOptimisticPlaceholderToPlan(
121
+ plan: OptimisticFragmentPlan,
122
+ operation: Extract<OptimisticUIOperation, { action: 'create-placeholder' }>,
123
+ token: string,
124
+ ) {
125
+ const currentCollection = plan.nextFragment[operation.field];
126
+ if (!Array.isArray(currentCollection)) {
127
+ return;
128
+ }
129
+
130
+ const placeholder = {
131
+ ...operation.data,
132
+ __optimisticPlaceholder: true,
133
+ __optimisticTransactionId: token,
134
+ };
135
+ plan.nextFragment[operation.field] =
136
+ operation.position === 'start'
137
+ ? [placeholder, ...currentCollection]
138
+ : [...currentCollection, placeholder];
139
+ plan.placeholders.push({ field: operation.field, token });
140
+ }
141
+
142
+ /*
143
+ * Build all fragment plans before acquiring locks or writing cache state.
144
+ */
145
+ function getOptimisticFragmentPlans(optimisticUI: OptimisticUI) {
146
+ const plans = new Map<string, OptimisticFragmentPlan>();
147
+ const transactionId = getOptimisticUITransactionId();
148
+
149
+ getOptimisticUIOperations(optimisticUI).forEach((operation, index) => {
150
+ const fragmentKey = getOptimisticUIOperationFragmentKey(operation);
151
+ if (!fragmentKey) {
152
+ return;
153
+ }
154
+
155
+ const plan = getOptimisticFragmentPlan(plans, fragmentKey);
156
+ if (!plan) {
157
+ return;
158
+ }
159
+
160
+ if (operation.action === 'update') {
161
+ applyOptimisticUpdateToPlan(plan, operation);
162
+ return;
163
+ }
164
+
165
+ applyOptimisticPlaceholderToPlan(
166
+ plan,
167
+ operation,
168
+ `${transactionId}-${index}`,
169
+ );
170
+ });
171
+
172
+ return [...plans.values()].filter((plan) => !graphQLValuesEqual(plan.baseFragment, plan.nextFragment));
173
+ }
174
+
175
+ /*
176
+ * Acquire every affected fragment together so overlapping optimistic mutations
177
+ * can be rejected before either request races authoritative cache writes.
178
+ */
179
+ function acquireOptimisticFragmentPlans(plans: OptimisticFragmentPlan[]) {
180
+ return acquireOptimisticUIFragmentKeys(plans.map((plan) => plan.fragmentKey));
181
+ }
182
+
183
+ /*
184
+ * Release every fragment owned by a completed optimistic transaction.
185
+ */
186
+ function releaseOptimisticFragmentPlans(plans: OptimisticFragmentPlan[]) {
187
+ releaseOptimisticUIFragmentKeys(plans.map((plan) => plan.fragmentKey));
188
+ }
189
+
190
+ /*
191
+ * Queue each settled fragment for persistence after releasing its transient lock.
192
+ */
193
+ function persistSettledOptimisticFragmentPlans(
194
+ plans: OptimisticFragmentPlan[],
195
+ ) {
196
+ plans.forEach((plan) => {
197
+ cacheStore.markFragmentDirty(plan.fragmentKey);
198
+ });
199
+ }
200
+
201
+ /*
202
+ * Remove only placeholders created by one transaction from a fragment.
203
+ */
204
+ function removeTransactionPlaceholders(
205
+ fragment: Record<string, any>,
206
+ placeholders: OptimisticPlaceholderState[],
207
+ ) {
208
+ let nextFragment = fragment;
209
+ let changed = false;
210
+
211
+ placeholders.forEach(({ field, token }) => {
212
+ const collection = nextFragment[field];
213
+ if (!Array.isArray(collection)) {
214
+ return;
215
+ }
216
+
217
+ const nextCollection = collection.filter(
218
+ (item) => item?.__optimisticTransactionId !== token,
219
+ );
220
+ if (nextCollection.length === collection.length) {
221
+ return;
222
+ }
223
+
224
+ if (!changed) {
225
+ nextFragment = { ...nextFragment };
226
+ }
227
+ nextFragment[field] = nextCollection;
228
+ changed = true;
229
+ });
230
+
231
+ return changed ? nextFragment : null;
232
+ }
233
+
234
+ /*
235
+ * Remove settled placeholders while retaining authoritative collection data.
236
+ */
237
+ function commitOptimisticFragmentPlans(plans: OptimisticFragmentPlan[]) {
238
+ const updatedFragmentKeys: string[] = [];
239
+
240
+ plans.forEach((plan) => {
241
+ if (!plan.placeholders.length) {
242
+ return;
243
+ }
244
+
245
+ const currentFragment = loadFragment(plan.fragmentKey);
246
+ if (!currentFragment) {
247
+ return;
248
+ }
249
+
250
+ const nextFragment = removeTransactionPlaceholders(
251
+ currentFragment,
252
+ plan.placeholders,
253
+ );
254
+ if (!nextFragment) {
255
+ return;
256
+ }
257
+
258
+ addFragmentToCache(plan.fragmentKey, nextFragment);
259
+ updatedFragmentKeys.push(plan.fragmentKey);
260
+ });
261
+
262
+ return updatedFragmentKeys;
263
+ }
264
+
265
+ /*
266
+ * Conditionally restore optimistic fields and remove transaction placeholders.
267
+ */
268
+ function rollbackOptimisticFragmentPlans(plans: OptimisticFragmentPlan[]) {
269
+ const updatedFragmentKeys: string[] = [];
270
+
271
+ plans.forEach((plan) => {
272
+ const currentFragment = loadFragment(plan.fragmentKey);
273
+ if (!currentFragment) {
274
+ return;
275
+ }
276
+
277
+ const fragmentWithoutPlaceholders = removeTransactionPlaceholders(
278
+ currentFragment,
279
+ plan.placeholders,
280
+ );
281
+ const plannedFragmentWithoutPlaceholders =
282
+ removeTransactionPlaceholders(plan.nextFragment, plan.placeholders) ||
283
+ plan.nextFragment;
284
+ let nextFragment = fragmentWithoutPlaceholders || currentFragment;
285
+ let changed = Boolean(fragmentWithoutPlaceholders);
286
+ plan.optimisticFields.forEach((_optimisticValue, field) => {
287
+ if (!graphQLValuesEqual(nextFragment[field], plannedFragmentWithoutPlaceholders[field])) {
288
+ return;
289
+ }
290
+
291
+ if (!changed) {
292
+ nextFragment = { ...nextFragment };
293
+ }
294
+ const previousField = plan.previousFields.get(field);
295
+ if (previousField?.exists) {
296
+ nextFragment[field] = previousField.value;
297
+ } else {
298
+ delete nextFragment[field];
299
+ }
300
+ changed = true;
301
+ });
302
+
303
+ if (!changed) {
304
+ return;
305
+ }
306
+
307
+ addFragmentToCache(plan.fragmentKey, nextFragment);
308
+ updatedFragmentKeys.push(plan.fragmentKey);
309
+ });
310
+
311
+ return updatedFragmentKeys;
312
+ }
313
+
314
+ /*
315
+ * Settle one optimistic transaction, release its locks, and publish final state.
316
+ */
317
+ function settleOptimisticUITransaction(
318
+ plans: OptimisticFragmentPlan[],
319
+ settlePlans: (plans: OptimisticFragmentPlan[]) => string[],
320
+ updateObservers?: UpdateObserversFn,
321
+ ) {
322
+ let updatedFragmentKeys: string[] = [];
323
+
324
+ try {
325
+ updatedFragmentKeys = settlePlans(plans);
326
+ } finally {
327
+ releaseOptimisticFragmentPlans(plans);
328
+ persistSettledOptimisticFragmentPlans(plans);
329
+ }
330
+
331
+ triggerObserver(updatedFragmentKeys, updateObservers);
332
+ }
333
+
334
+ /*
335
+ * Apply one optimistic mutation transaction and return its settlement handlers.
336
+ */
337
+ export function beginOptimisticUITransaction(
338
+ optimisticUI: OptimisticUI | null | undefined,
339
+ updateObservers?: UpdateObserversFn,
340
+ ): OptimisticUITransactionResult {
341
+ if (!optimisticUI) {
342
+ return { status: 'noop', transaction: null };
343
+ }
344
+
345
+ const plans = getOptimisticFragmentPlans(optimisticUI);
346
+ if (!plans.length) {
347
+ return { status: 'noop', transaction: null };
348
+ }
349
+ if (!acquireOptimisticFragmentPlans(plans)) {
350
+ return { status: 'conflict', transaction: null };
351
+ }
352
+
353
+ const fragmentKeys = plans.map((plan) => plan.fragmentKey);
354
+ try {
355
+ plans.forEach((plan) => {
356
+ addFragmentToCache(plan.fragmentKey, plan.nextFragment);
357
+ });
358
+ triggerObserver(fragmentKeys, updateObservers);
359
+ } catch (error) {
360
+ try {
361
+ rollbackOptimisticFragmentPlans(plans);
362
+ } finally {
363
+ releaseOptimisticFragmentPlans(plans);
364
+ persistSettledOptimisticFragmentPlans(plans);
365
+ }
366
+ throw error;
367
+ }
368
+
369
+ let settled = false;
370
+
371
+ return {
372
+ status: 'applied',
373
+ transaction: {
374
+ fragmentKeys,
375
+ commit: () => {
376
+ if (settled) {
377
+ return;
378
+ }
379
+
380
+ settled = true;
381
+ settleOptimisticUITransaction(
382
+ plans,
383
+ commitOptimisticFragmentPlans,
384
+ updateObservers,
385
+ );
386
+ },
387
+ rollback: () => {
388
+ if (settled) {
389
+ return;
390
+ }
391
+
392
+ settled = true;
393
+ settleOptimisticUITransaction(
394
+ plans,
395
+ rollbackOptimisticFragmentPlans,
396
+ updateObservers,
397
+ );
398
+ },
399
+ },
400
+ };
401
+ }