@jsb188/graphql 1.1.10 → 1.1.11

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 (116) hide show
  1. package/cache/config.ts +64 -50
  2. package/cache/index.test.ts +671 -109
  3. package/cache/index.ts +1700 -1240
  4. package/cache/persist.ts +721 -0
  5. package/cache/store.ts +437 -0
  6. package/client/data-table-realtime-subscriptions.test.ts +740 -0
  7. package/client/data-table-realtime-subscriptions.ts +955 -0
  8. package/client/data-table-realtime.test.ts +792 -0
  9. package/client/data-table-realtime.ts +1218 -0
  10. package/client/fragment-realtime-sequence.ts +52 -0
  11. package/client/index.ts +2 -0
  12. package/client/open-sheet.ts +137 -0
  13. package/client/request-managed-session.test.ts +574 -0
  14. package/client/request-no-store.test.ts +157 -0
  15. package/client/request.ts +954 -163
  16. package/client/sse-ticket.test.ts +26 -0
  17. package/deno.lock +170 -0
  18. package/deno.test.json +8 -4
  19. package/drafts/index.ts +149 -0
  20. package/drafts/store.ts +397 -0
  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/agentDocFragments.ts +46 -0
  26. package/gql/fragments/agentMemoryFragments.ts +19 -0
  27. package/gql/fragments/aiChatFragments.ts +16 -0
  28. package/gql/fragments/billingFragments.ts +0 -11
  29. package/gql/fragments/campaignFragments.ts +277 -0
  30. package/gql/fragments/dataTableFragments.ts +18 -128
  31. package/gql/fragments/emailFragments.ts +19 -9
  32. package/gql/fragments/fieldDocFragments.ts +82 -0
  33. package/gql/fragments/logFragments.ts +20 -79
  34. package/gql/fragments/organizationFragments.ts +43 -5
  35. package/gql/fragments/scheduleFragments.ts +155 -0
  36. package/gql/fragments/sheetFragments.ts +552 -22
  37. package/gql/fragments/storageFragments.ts +21 -0
  38. package/gql/fragments/workflowFragments.ts +28 -4
  39. package/gql/mutations/accountMutations.ts +0 -2
  40. package/gql/mutations/agentDocMutations.ts +177 -0
  41. package/gql/mutations/agentMemoryMutations.ts +36 -0
  42. package/gql/mutations/aiChatMutations.ts +17 -4
  43. package/gql/mutations/authMutations.ts +158 -17
  44. package/gql/mutations/campaignMutations.ts +215 -0
  45. package/gql/mutations/dataTableMutations.ts +48 -26
  46. package/gql/mutations/fieldDocMutations.ts +24 -0
  47. package/gql/mutations/index.ts +6 -1
  48. package/gql/mutations/logMutations.ts +6 -29
  49. package/gql/mutations/organizationMutations.ts +13 -4
  50. package/gql/mutations/scheduleMutations.ts +423 -0
  51. package/gql/mutations/sheetMutations.ts +273 -56
  52. package/gql/mutations/storageMutations.ts +20 -0
  53. package/gql/mutations/workflowMutations.ts +42 -0
  54. package/gql/queries/agentDocQueries.ts +120 -0
  55. package/gql/queries/agentMemoryQueries.ts +22 -0
  56. package/gql/queries/aiChatQueries.ts +18 -2
  57. package/gql/queries/authQueries.ts +41 -0
  58. package/gql/queries/billingQueries.ts +23 -0
  59. package/gql/queries/campaignQueries.ts +127 -0
  60. package/gql/queries/dataTableQueries.ts +100 -11
  61. package/gql/queries/emailQueries.ts +55 -3
  62. package/gql/queries/fieldDocQueries.ts +158 -0
  63. package/gql/queries/index.ts +8 -2
  64. package/gql/queries/logQueries.ts +11 -28
  65. package/gql/queries/organizationQueries.ts +52 -1
  66. package/gql/queries/scheduleQueries.ts +315 -0
  67. package/gql/queries/sheetQueries.ts +336 -25
  68. package/gql/queries/storageQueries.ts +38 -0
  69. package/gql/queries/workflowQueries.ts +72 -3
  70. package/hooks/index.ts +2065 -971
  71. package/hooks/use-account-mtn.ts +76 -81
  72. package/hooks/use-agentDoc-mtn.ts +285 -0
  73. package/hooks/use-agentDoc-qry.ts +208 -0
  74. package/hooks/use-agentMemory-mtn.ts +73 -0
  75. package/hooks/use-agentMemory-qry.ts +44 -0
  76. package/hooks/use-aiChat-mtn.ts +232 -137
  77. package/hooks/use-aiChat-qry.ts +480 -222
  78. package/hooks/use-app-qry.ts +12 -9
  79. package/hooks/use-auth-mtn.ts +406 -252
  80. package/hooks/use-auth-qry.ts +51 -0
  81. package/hooks/use-campaign-mtn.ts +377 -0
  82. package/hooks/use-campaign-qry.ts +257 -0
  83. package/hooks/use-dataTable-mtn.ts +116 -33
  84. package/hooks/use-dataTable-qry.ts +859 -35
  85. package/hooks/use-email-qry.ts +194 -79
  86. package/hooks/use-fieldDoc-mtn.ts +50 -0
  87. package/hooks/use-fieldDoc-qry.ts +180 -0
  88. package/hooks/use-inboundContact-mtn.ts +19 -16
  89. package/hooks/use-inboundContact-qry.ts +63 -55
  90. package/hooks/use-integration-mtn.ts +56 -38
  91. package/hooks/use-integration-qry.ts +24 -9
  92. package/hooks/use-log-mtn.ts +72 -56
  93. package/hooks/use-log-qry.ts +93 -69
  94. package/hooks/use-organization-mtn.ts +334 -277
  95. package/hooks/use-organization-qry.ts +199 -129
  96. package/hooks/use-schedule-mtn.ts +416 -0
  97. package/hooks/use-schedule-qry.ts +157 -0
  98. package/hooks/use-sheet-mtn.ts +233 -47
  99. package/hooks/use-sheet-qry.ts +353 -70
  100. package/hooks/use-storage-mtn.ts +81 -41
  101. package/hooks/use-storage-qry.ts +104 -13
  102. package/hooks/use-workflow-mtn.ts +55 -0
  103. package/hooks/use-workflow-qry.ts +206 -48
  104. package/loaders/campaign.ts +137 -0
  105. package/package.json +94 -71
  106. package/sse.ts +157 -104
  107. package/tsconfig.json +11 -10
  108. package/types.d.ts +60 -30
  109. package/utils.ts +78 -49
  110. package/gql/fragments/productFragments.ts +0 -39
  111. package/gql/fragments/reportFragments.ts +0 -90
  112. package/gql/mutations/billingMutations.ts +0 -34
  113. package/gql/queries/productQueries.ts +0 -31
  114. package/gql/queries/reportQueries.ts +0 -134
  115. package/hooks/use-product-qry.ts +0 -67
  116. package/hooks/use-report-qry.ts +0 -153
package/cache/store.ts ADDED
@@ -0,0 +1,437 @@
1
+ /**
2
+ * Versioned normalized store core for the GraphQL client cache.
3
+ *
4
+ * Wraps plain Maps with per-key version stamps so read assembly
5
+ * (loadFragment/loadQuery) can be memoized with structural sharing, writes
6
+ * that do not change data never invalidate anything, eviction can be LRU
7
+ * instead of FIFO, and a persistence adapter can subscribe to every write
8
+ * regardless of its source (query result, mutation result, realtime event).
9
+ *
10
+ * This module is internal to the cache; the public cache API in ./index.ts
11
+ * is unchanged.
12
+ */
13
+
14
+ /**
15
+ * Monotonic clock shared by version stamps and LRU access stamps.
16
+ * Never reused, so a deleted-and-recreated key always invalidates memo
17
+ * entries that captured the old version.
18
+ */
19
+
20
+ let TICK = 1;
21
+
22
+ function nextTick(): number {
23
+ TICK += 1;
24
+ return TICK;
25
+ }
26
+
27
+ type StoreEntry = {
28
+ data: any;
29
+ v: number; // version stamp; bumped only when data actually changes
30
+ at: number; // last access stamp for LRU eviction
31
+ };
32
+
33
+ type DirtyListener = (dirty: {
34
+ fragmentKeys: Set<string>;
35
+ queryKeys: Set<string>;
36
+ }) => void;
37
+
38
+ /**
39
+ * Dependency capture stack for memoized reads. Reads record (namespacedKey ->
40
+ * version) into the top capture only; memoize() merges each finished capture
41
+ * into the parent capture so nested memo hits still propagate their deps.
42
+ */
43
+
44
+ const CAPTURE_STACK: Array<Map<string, number>> = [];
45
+
46
+ function recordDependency(namespacedKey: string, version: number) {
47
+ const activeCapture = CAPTURE_STACK[CAPTURE_STACK.length - 1];
48
+ if (activeCapture) {
49
+ activeCapture.set(namespacedKey, version);
50
+ }
51
+ }
52
+
53
+ function mergeIntoActiveCapture(deps: Map<string, number>) {
54
+ const activeCapture = CAPTURE_STACK[CAPTURE_STACK.length - 1];
55
+ if (activeCapture) {
56
+ deps.forEach((version, key) => {
57
+ activeCapture.set(key, version);
58
+ });
59
+ }
60
+ }
61
+
62
+ /**
63
+ * Fast deep equality with reference short-circuit; used to skip version bumps
64
+ * for writes that re-assert identical data (common with realtime echoes and
65
+ * refetches), which keeps memoized reads and downstream renders stable.
66
+ */
67
+
68
+ function deepEqual(a: any, b: any): boolean {
69
+ if (a === b) {
70
+ return true;
71
+ }
72
+
73
+ if (
74
+ typeof a !== typeof b ||
75
+ a === null ||
76
+ b === null ||
77
+ typeof a !== 'object'
78
+ ) {
79
+ // NaN never equals itself, but the cache never stores NaN meaningfully
80
+ return false;
81
+ }
82
+
83
+ const aIsArray = Array.isArray(a);
84
+ if (aIsArray !== Array.isArray(b)) {
85
+ return false;
86
+ }
87
+
88
+ if (aIsArray) {
89
+ if (a.length !== b.length) {
90
+ return false;
91
+ }
92
+
93
+ for (let i = 0; i < a.length; i++) {
94
+ if (!deepEqual(a[i], b[i])) {
95
+ return false;
96
+ }
97
+ }
98
+
99
+ return true;
100
+ }
101
+
102
+ const aKeys = Object.keys(a);
103
+ const bKeys = Object.keys(b);
104
+ if (aKeys.length !== bKeys.length) {
105
+ return false;
106
+ }
107
+
108
+ for (const key of aKeys) {
109
+ if (
110
+ !Object.prototype.hasOwnProperty.call(b, key) ||
111
+ !deepEqual(a[key], b[key])
112
+ ) {
113
+ return false;
114
+ }
115
+ }
116
+
117
+ return true;
118
+ }
119
+
120
+ /**
121
+ * Map-compatible facade over a versioned entry map. get() records a memo
122
+ * dependency and touches the LRU stamp; set() bumps the version only when the
123
+ * data actually changed and reports the key as dirty for subscribers.
124
+ */
125
+
126
+ export class VersionedKeyMap {
127
+ private entries = new Map<string, StoreEntry>();
128
+ private namespace: string;
129
+ private markDirty: (key: string) => void;
130
+ private onDelete?: (key: string) => void;
131
+
132
+ constructor(
133
+ namespace: string,
134
+ markDirty: (key: string) => void,
135
+ onDelete?: (key: string) => void,
136
+ ) {
137
+ this.namespace = namespace;
138
+ this.markDirty = markDirty;
139
+ this.onDelete = onDelete;
140
+ }
141
+
142
+ get size(): number {
143
+ return this.entries.size;
144
+ }
145
+
146
+ get(key: string): any {
147
+ const entry = this.entries.get(key);
148
+ recordDependency(this.namespace + key, entry?.v || 0);
149
+
150
+ if (!entry) {
151
+ return undefined;
152
+ }
153
+
154
+ entry.at = nextTick();
155
+ return entry.data;
156
+ }
157
+
158
+ /** Read without recording a dependency or touching the LRU stamp. */
159
+ peek(key: string): any {
160
+ return this.entries.get(key)?.data;
161
+ }
162
+
163
+ version(key: string): number {
164
+ return this.entries.get(key)?.v || 0;
165
+ }
166
+
167
+ lastAccess(key: string): number {
168
+ return this.entries.get(key)?.at || 0;
169
+ }
170
+
171
+ set(key: string, data: any): void {
172
+ const entry = this.entries.get(key);
173
+
174
+ if (entry) {
175
+ if (deepEqual(entry.data, data)) {
176
+ // Keep the previously stored reference so memoized assemblies built
177
+ // from it remain valid; the write changes nothing observable.
178
+ entry.at = nextTick();
179
+ return;
180
+ }
181
+
182
+ entry.data = data;
183
+ entry.v = nextTick();
184
+ entry.at = entry.v;
185
+ } else {
186
+ const stamp = nextTick();
187
+ this.entries.set(key, { data, v: stamp, at: stamp });
188
+ }
189
+
190
+ this.markDirty(key);
191
+ }
192
+
193
+ /**
194
+ * Load one persisted entry at boot. In-memory data always wins (it is
195
+ * newer than any snapshot) and hydration never marks the key dirty, so
196
+ * hydrated values cannot echo back into the persistence write buffer.
197
+ * Returns whether the entry was actually added.
198
+ */
199
+ hydrate(key: string, data: any): boolean {
200
+ if (this.entries.has(key)) {
201
+ return false;
202
+ }
203
+
204
+ const stamp = nextTick();
205
+ this.entries.set(key, { data, v: stamp, at: stamp });
206
+ return true;
207
+ }
208
+
209
+ has(key: string): boolean {
210
+ return this.entries.has(key);
211
+ }
212
+
213
+ delete(key: string): boolean {
214
+ const existed = this.entries.delete(key);
215
+ if (existed) {
216
+ this.onDelete?.(key);
217
+ this.markDirty(key);
218
+ }
219
+ return existed;
220
+ }
221
+
222
+ keys(): IterableIterator<string> {
223
+ return this.entries.keys();
224
+ }
225
+
226
+ clear(): void {
227
+ this.entries.clear();
228
+ }
229
+
230
+ /**
231
+ * Return keys ordered least-recently-accessed first, excluding keys the
232
+ * predicate rejects.
233
+ */
234
+ keysByLeastRecentAccess(filter?: (key: string) => boolean): string[] {
235
+ const keys: Array<[string, number]> = [];
236
+
237
+ this.entries.forEach((entry, key) => {
238
+ if (!filter || filter(key)) {
239
+ keys.push([key, entry.at]);
240
+ }
241
+ });
242
+
243
+ keys.sort((a, b) => a[1] - b[1]);
244
+ return keys.map(([key]) => key);
245
+ }
246
+ }
247
+
248
+ /**
249
+ * Memoization for read assembly. An entry is valid while every dependency it
250
+ * captured still has the same version.
251
+ */
252
+
253
+ const MEMO = new Map<string, { value: any; deps: Map<string, number> }>();
254
+ const MEMO_LIMIT = 20000;
255
+
256
+ function dependenciesAreCurrent(
257
+ deps: Map<string, number>,
258
+ versionOf: (namespacedKey: string) => number,
259
+ ): boolean {
260
+ for (const [key, version] of deps) {
261
+ if (versionOf(key) !== version) {
262
+ return false;
263
+ }
264
+ }
265
+
266
+ return true;
267
+ }
268
+
269
+ /**
270
+ * GraphQL cache store singleton: fragment and query namespaces, dirty-key
271
+ * subscriptions for persistence adapters, and memoized read assembly.
272
+ */
273
+
274
+ class GraphQLCacheStore {
275
+ fragments: VersionedKeyMap;
276
+ queries: VersionedKeyMap;
277
+
278
+ private dirtyFragmentKeys = new Set<string>();
279
+ private dirtyQueryKeys = new Set<string>();
280
+ private dirtyListeners = new Set<DirtyListener>();
281
+ private flushScheduled = false;
282
+
283
+ private queryFetchTimes = new Map<string, number>();
284
+
285
+ constructor() {
286
+ this.fragments = new VersionedKeyMap('f:', (key) =>
287
+ this.queueDirty(this.dirtyFragmentKeys, key),
288
+ );
289
+ this.queries = new VersionedKeyMap(
290
+ 'q:',
291
+ (key) => this.queueDirty(this.dirtyQueryKeys, key),
292
+ (key) => this.queryFetchTimes.delete(key),
293
+ );
294
+ }
295
+
296
+ /**
297
+ * Record when one query's data last came from the network. Marks the query
298
+ * dirty so the persisted record carries the stamp even when the response
299
+ * data itself was deep-equal (no version bump, no observer noise).
300
+ */
301
+ setQueryFetchTime(queryKey: string, fetchedAt: number): void {
302
+ this.queryFetchTimes.set(queryKey, fetchedAt);
303
+ this.queueDirty(this.dirtyQueryKeys, queryKey);
304
+ }
305
+
306
+ getQueryFetchTime(queryKey: string): number | undefined {
307
+ return this.queryFetchTimes.get(queryKey);
308
+ }
309
+
310
+ /** Restore one persisted fetch stamp at boot (never marks dirty). */
311
+ hydrateQueryFetchTime(queryKey: string, fetchedAt: number): void {
312
+ if (!this.queryFetchTimes.has(queryKey)) {
313
+ this.queryFetchTimes.set(queryKey, fetchedAt);
314
+ }
315
+ }
316
+
317
+ /**
318
+ * Subscribe to batched dirty-key notifications (microtask-coalesced).
319
+ * Returns the unsubscribe function. This is the single hook point for the
320
+ * IndexedDB persistence adapter: every fragment/query write from any source
321
+ * lands here.
322
+ */
323
+ subscribe(listener: DirtyListener): () => void {
324
+ this.dirtyListeners.add(listener);
325
+ return () => {
326
+ this.dirtyListeners.delete(listener);
327
+ };
328
+ }
329
+
330
+ private queueDirty(dirtySet: Set<string>, key: string) {
331
+ if (!this.dirtyListeners.size) {
332
+ return;
333
+ }
334
+
335
+ dirtySet.add(key);
336
+
337
+ if (!this.flushScheduled) {
338
+ this.flushScheduled = true;
339
+ queueMicrotask(() => this.flushDirty());
340
+ }
341
+ }
342
+
343
+ private flushDirty() {
344
+ this.flushScheduled = false;
345
+
346
+ if (!this.dirtyFragmentKeys.size && !this.dirtyQueryKeys.size) {
347
+ return;
348
+ }
349
+
350
+ const dirty = {
351
+ fragmentKeys: this.dirtyFragmentKeys,
352
+ queryKeys: this.dirtyQueryKeys,
353
+ };
354
+ this.dirtyFragmentKeys = new Set();
355
+ this.dirtyQueryKeys = new Set();
356
+
357
+ this.dirtyListeners.forEach((listener) => {
358
+ listener(dirty);
359
+ });
360
+ }
361
+
362
+ private versionOfNamespacedKey = (namespacedKey: string): number => {
363
+ if (namespacedKey.startsWith('f:')) {
364
+ return this.fragments.version(namespacedKey.substring(2));
365
+ }
366
+
367
+ return this.queries.version(namespacedKey.substring(2));
368
+ };
369
+
370
+ /**
371
+ * Memoize one read assembly with automatic dependency tracking. While the
372
+ * captured dependencies are unchanged, the exact same value reference is
373
+ * returned, which is what makes render-level reference-equality checks work.
374
+ */
375
+ memoizeRead<T>(memoKey: string, compute: () => T): T {
376
+ const hit = MEMO.get(memoKey);
377
+ if (hit && dependenciesAreCurrent(hit.deps, this.versionOfNamespacedKey)) {
378
+ mergeIntoActiveCapture(hit.deps);
379
+ return hit.value;
380
+ }
381
+
382
+ const capture = new Map<string, number>();
383
+ CAPTURE_STACK.push(capture);
384
+
385
+ let value: T;
386
+ try {
387
+ value = compute();
388
+ } finally {
389
+ CAPTURE_STACK.pop();
390
+ }
391
+
392
+ mergeIntoActiveCapture(capture);
393
+
394
+ if (MEMO.size >= MEMO_LIMIT) {
395
+ // Rare full reset is cheaper and simpler than per-entry LRU here;
396
+ // entries rebuild lazily on the next read.
397
+ MEMO.clear();
398
+ }
399
+ MEMO.set(memoKey, { value, deps: capture });
400
+
401
+ return value;
402
+ }
403
+
404
+ /**
405
+ * Build a combined version stamp for a set of fragment keys. Callers can
406
+ * compare stamps to know whether any of the fragments (or their spreads)
407
+ * changed without loading them.
408
+ */
409
+ getFragmentVersionStamp(fragmentKeys: string[]): string {
410
+ let stamp = '';
411
+
412
+ for (const key of fragmentKeys) {
413
+ stamp +=
414
+ this.fragments.version(key) +
415
+ '.' +
416
+ this.fragments.version(`${key}.spreads`) +
417
+ '|';
418
+ }
419
+
420
+ return stamp;
421
+ }
422
+
423
+ clearMemo(): void {
424
+ MEMO.clear();
425
+ }
426
+
427
+ clearAll(): void {
428
+ this.fragments.clear();
429
+ this.queries.clear();
430
+ this.queryFetchTimes.clear();
431
+ MEMO.clear();
432
+ this.dirtyFragmentKeys.clear();
433
+ this.dirtyQueryKeys.clear();
434
+ }
435
+ }
436
+
437
+ export const cacheStore = new GraphQLCacheStore();