@go-avro/avro-js 1.6.6-rc11 → 1.6.6-rc13
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/client/QueryClient.d.ts +10 -0
- package/dist/client/QueryClient.js +71 -33
- package/dist/client/hooks/devices.js +17 -3
- package/dist/client/hooks/routes.js +4 -1
- package/dist/client/hooks/teams.js +4 -1
- package/dist/client/realtime.d.ts +26 -3
- package/dist/client/realtime.js +79 -10
- package/dist/types/api/Avro.d.ts +5 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
|
@@ -767,6 +767,7 @@ export declare class AvroQueryClient {
|
|
|
767
767
|
private _persister;
|
|
768
768
|
private _presencePending;
|
|
769
769
|
private _presenceTimer;
|
|
770
|
+
private _resyncTimer;
|
|
770
771
|
/** Chat rooms to (re)join on every connect; socket rooms don't survive reconnects. */
|
|
771
772
|
private _joinedChats;
|
|
772
773
|
private _restored;
|
|
@@ -820,6 +821,15 @@ export declare class AvroQueryClient {
|
|
|
820
821
|
setupSocketInvalidation(queryClient: QueryClient): void;
|
|
821
822
|
/** Join the company room; resync if the ack version moved while away. */
|
|
822
823
|
private joinCompanyRoom;
|
|
824
|
+
/**
|
|
825
|
+
* Mark matching queries stale now; refetch the ones on screen after a
|
|
826
|
+
* random delay. A deploy or restart reconnects every client at once
|
|
827
|
+
* (and resets the version counter), so an immediate refetch of
|
|
828
|
+
* everything sent the whole fleet's load at a just-restarted backend
|
|
829
|
+
* in the same second. Anything mounted meanwhile still refetches,
|
|
830
|
+
* since it is already stale.
|
|
831
|
+
*/
|
|
832
|
+
private resync;
|
|
823
833
|
/** Fallback for mutations when the socket is down (else the echo syncs). */
|
|
824
834
|
syncAfterMutation(...types: string[]): void;
|
|
825
835
|
/**
|
|
@@ -50,6 +50,8 @@ const MAX_MESSAGES_PER_PAGE = 100;
|
|
|
50
50
|
const AUTH_RECHECK_MIN_MS = 2000;
|
|
51
51
|
const AUTH_RECHECK_MAX_MS = 60000;
|
|
52
52
|
const SOCKET_AUTH_RETRY_MS = 10000;
|
|
53
|
+
/** Spread of the refetch after a reconnect finds the company changed. */
|
|
54
|
+
const RESYNC_JITTER_MS = 10000;
|
|
53
55
|
export class AvroQueryClient {
|
|
54
56
|
constructor(config) {
|
|
55
57
|
this._authState = AuthState.UNKNOWN;
|
|
@@ -63,6 +65,7 @@ export class AvroQueryClient {
|
|
|
63
65
|
this._persister = null;
|
|
64
66
|
this._presencePending = new Set();
|
|
65
67
|
this._presenceTimer = null;
|
|
68
|
+
this._resyncTimer = null;
|
|
66
69
|
/** Chat rooms to (re)join on every connect; socket rooms don't survive reconnects. */
|
|
67
70
|
this._joinedChats = new Set();
|
|
68
71
|
this._restored = null;
|
|
@@ -305,7 +308,47 @@ export class AvroQueryClient {
|
|
|
305
308
|
if (this._socketInvalidationCleanup)
|
|
306
309
|
return;
|
|
307
310
|
this._queryClient = queryClient;
|
|
308
|
-
|
|
311
|
+
// Stale-mark only: an active refetch here turned backend
|
|
312
|
+
// trouble into a full-list refetch storm (every failing
|
|
313
|
+
// presence fetch scheduled a 400KB+ list download). The next
|
|
314
|
+
// mount/focus or realtime change refetches instead.
|
|
315
|
+
const markMembersStale = () => queryClient.invalidateQueries({
|
|
316
|
+
queryKey: ['users', 'company'],
|
|
317
|
+
refetchType: 'none',
|
|
318
|
+
});
|
|
319
|
+
// Fetch just the named members and patch them into every member
|
|
320
|
+
// list; used for presence flips and for `member` changes that
|
|
321
|
+
// name the user.
|
|
322
|
+
const refreshMembers = (userIds) => {
|
|
323
|
+
const companyId = this.companyId;
|
|
324
|
+
if (!companyId || userIds.length === 0)
|
|
325
|
+
return;
|
|
326
|
+
// `ListCompanyUsers.user_ids` holds at most 1000 ids.
|
|
327
|
+
for (let i = 0; i < userIds.length; i += 1000) {
|
|
328
|
+
const wanted = userIds.slice(i, i + 1000);
|
|
329
|
+
this.post({
|
|
330
|
+
path: `/company/${companyId}/users`,
|
|
331
|
+
data: JSON.stringify({ user_ids: wanted }),
|
|
332
|
+
headers: { 'Content-Type': 'application/json' },
|
|
333
|
+
idempotent: true,
|
|
334
|
+
})
|
|
335
|
+
.then((members) => {
|
|
336
|
+
if (!Array.isArray(members)) {
|
|
337
|
+
markMembersStale();
|
|
338
|
+
return;
|
|
339
|
+
}
|
|
340
|
+
for (const member of members)
|
|
341
|
+
patchMember(queryClient, member);
|
|
342
|
+
// A member the server no longer returns left the
|
|
343
|
+
// company; patching can't remove it.
|
|
344
|
+
const got = new Set(members.map((m) => m?.user?.id));
|
|
345
|
+
if (wanted.some((id) => !got.has(id)))
|
|
346
|
+
markMembersStale();
|
|
347
|
+
})
|
|
348
|
+
.catch(markMembersStale);
|
|
349
|
+
}
|
|
350
|
+
};
|
|
351
|
+
const applier = new ChangeApplier(this, queryClient, refreshMembers);
|
|
309
352
|
this._applier = applier;
|
|
310
353
|
const handlers = [];
|
|
311
354
|
const on = (event, handler) => {
|
|
@@ -340,14 +383,6 @@ export class AvroQueryClient {
|
|
|
340
383
|
// and patch those members instead of re-downloading every
|
|
341
384
|
// member once per flip on every connected client.
|
|
342
385
|
const PRESENCE_COALESCE_MS = 500;
|
|
343
|
-
// Stale-mark only: an active refetch here turned backend
|
|
344
|
-
// trouble into a full-list refetch storm (every failing
|
|
345
|
-
// presence fetch scheduled a 400KB+ list download). The next
|
|
346
|
-
// mount/focus or realtime change refetches instead.
|
|
347
|
-
const markMembersStale = () => queryClient.invalidateQueries({
|
|
348
|
-
queryKey: ['users', 'company'],
|
|
349
|
-
refetchType: 'none',
|
|
350
|
-
});
|
|
351
386
|
on('update_presence', (data) => {
|
|
352
387
|
const userId = data?.user_id;
|
|
353
388
|
if (!userId || !this.companyId) {
|
|
@@ -361,26 +396,7 @@ export class AvroQueryClient {
|
|
|
361
396
|
this._presenceTimer = null;
|
|
362
397
|
const userIds = [...this._presencePending];
|
|
363
398
|
this._presencePending.clear();
|
|
364
|
-
|
|
365
|
-
return;
|
|
366
|
-
// `ListCompanyUsers.user_ids` holds at most 1000 ids.
|
|
367
|
-
for (let i = 0; i < userIds.length; i += 1000) {
|
|
368
|
-
this.post({
|
|
369
|
-
path: `/company/${this.companyId}/users`,
|
|
370
|
-
data: JSON.stringify({ user_ids: userIds.slice(i, i + 1000) }),
|
|
371
|
-
headers: { 'Content-Type': 'application/json' },
|
|
372
|
-
idempotent: true,
|
|
373
|
-
})
|
|
374
|
-
.then((members) => {
|
|
375
|
-
if (!Array.isArray(members)) {
|
|
376
|
-
markMembersStale();
|
|
377
|
-
return;
|
|
378
|
-
}
|
|
379
|
-
for (const member of members)
|
|
380
|
-
patchMember(queryClient, member);
|
|
381
|
-
})
|
|
382
|
-
.catch(markMembersStale);
|
|
383
|
-
}
|
|
399
|
+
refreshMembers(userIds);
|
|
384
400
|
}, PRESENCE_COALESCE_MS);
|
|
385
401
|
});
|
|
386
402
|
on('location_update', (data) => {
|
|
@@ -430,6 +446,10 @@ export class AvroQueryClient {
|
|
|
430
446
|
this._presenceTimer = null;
|
|
431
447
|
}
|
|
432
448
|
this._presencePending.clear();
|
|
449
|
+
if (this._resyncTimer) {
|
|
450
|
+
clearTimeout(this._resyncTimer);
|
|
451
|
+
this._resyncTimer = null;
|
|
452
|
+
}
|
|
433
453
|
applier.dispose();
|
|
434
454
|
this._applier = null;
|
|
435
455
|
if (this.socket.connected && this.companyId) {
|
|
@@ -455,15 +475,33 @@ export class AvroQueryClient {
|
|
|
455
475
|
// were never delivered. Refresh what loaded before now
|
|
456
476
|
// once; later joins compare versions instead.
|
|
457
477
|
const joinedAt = Date.now();
|
|
458
|
-
this.
|
|
459
|
-
predicate: (q) => q.state.dataUpdatedAt > 0 && q.state.dataUpdatedAt < joinedAt,
|
|
460
|
-
});
|
|
478
|
+
this.resync((q) => q.state.dataUpdatedAt > 0 && q.state.dataUpdatedAt < joinedAt);
|
|
461
479
|
}
|
|
462
480
|
else if (known !== ack.version) {
|
|
463
|
-
this.
|
|
481
|
+
this.resync(() => true);
|
|
464
482
|
}
|
|
465
483
|
});
|
|
466
484
|
}
|
|
485
|
+
/**
|
|
486
|
+
* Mark matching queries stale now; refetch the ones on screen after a
|
|
487
|
+
* random delay. A deploy or restart reconnects every client at once
|
|
488
|
+
* (and resets the version counter), so an immediate refetch of
|
|
489
|
+
* everything sent the whole fleet's load at a just-restarted backend
|
|
490
|
+
* in the same second. Anything mounted meanwhile still refetches,
|
|
491
|
+
* since it is already stale.
|
|
492
|
+
*/
|
|
493
|
+
resync(predicate) {
|
|
494
|
+
const qc = this._queryClient;
|
|
495
|
+
if (!qc)
|
|
496
|
+
return;
|
|
497
|
+
void qc.invalidateQueries({ predicate, refetchType: 'none' });
|
|
498
|
+
if (this._resyncTimer)
|
|
499
|
+
clearTimeout(this._resyncTimer);
|
|
500
|
+
this._resyncTimer = setTimeout(() => {
|
|
501
|
+
this._resyncTimer = null;
|
|
502
|
+
void this._queryClient?.refetchQueries({ predicate, type: 'active', stale: true });
|
|
503
|
+
}, Math.random() * RESYNC_JITTER_MS);
|
|
504
|
+
}
|
|
467
505
|
/** Fallback for mutations when the socket is down (else the echo syncs). */
|
|
468
506
|
syncAfterMutation(...types) {
|
|
469
507
|
const qc = this._queryClient;
|
|
@@ -14,9 +14,23 @@ AvroQueryClient.prototype.useReportDevice = function () {
|
|
|
14
14
|
data: JSON.stringify({ ...this.device, ...data }),
|
|
15
15
|
headers: { 'Content-Type': 'application/json' },
|
|
16
16
|
}),
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
// Reported on every foreground/visibility flip: patch the returned
|
|
18
|
+
// device in rather than refetching the profile and device list.
|
|
19
|
+
onSuccess: (device) => {
|
|
20
|
+
if (!device?.id) {
|
|
21
|
+
queryClient.invalidateQueries({ queryKey: ['devices'] });
|
|
22
|
+
queryClient.invalidateQueries({ queryKey: ['user'] });
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const upsert = (devices) => {
|
|
26
|
+
if (!Array.isArray(devices))
|
|
27
|
+
return devices;
|
|
28
|
+
return devices.some((d) => d.id === device.id)
|
|
29
|
+
? devices.map((d) => (d.id === device.id ? device : d))
|
|
30
|
+
: [...devices, device];
|
|
31
|
+
};
|
|
32
|
+
queryClient.setQueryData(['devices'], (old) => upsert(old) ?? old);
|
|
33
|
+
queryClient.setQueryData(['user'], (old) => old && Array.isArray(old.devices) ? { ...old, devices: upsert(old.devices) } : old);
|
|
20
34
|
},
|
|
21
35
|
});
|
|
22
36
|
};
|
|
@@ -67,7 +67,10 @@ AvroQueryClient.prototype.useGetRoutes = function (body, total, onProgress) {
|
|
|
67
67
|
queryKey: keys.list('routes', this.companyId, {
|
|
68
68
|
amt: body.amt ?? 50,
|
|
69
69
|
query: body.query ?? '',
|
|
70
|
-
|
|
70
|
+
// Paged vs single fetch only: a count change (a route added or
|
|
71
|
+
// removed) arrives as a realtime change and is patched in, so it
|
|
72
|
+
// mustn't re-page the whole list on every client.
|
|
73
|
+
total: typeof total === 'number' ? 'paged' : 'all',
|
|
71
74
|
}),
|
|
72
75
|
queryFn: async () => {
|
|
73
76
|
if (typeof total !== 'number') {
|
|
@@ -10,7 +10,10 @@ AvroQueryClient.prototype.useGetTeams = function (body, total, onProgress) {
|
|
|
10
10
|
queryKey: keys.list('teams', this.companyId, {
|
|
11
11
|
amt: body.amt ?? 50,
|
|
12
12
|
query: body.query ?? '',
|
|
13
|
-
|
|
13
|
+
// Paged vs single fetch only: a count change (a team added or
|
|
14
|
+
// removed) arrives as a realtime change and is patched in, so it
|
|
15
|
+
// mustn't re-page the whole list on every client.
|
|
16
|
+
total: typeof total === 'number' ? 'paged' : 'all',
|
|
14
17
|
}),
|
|
15
18
|
queryFn: async () => {
|
|
16
19
|
if (typeof total !== 'number') {
|
|
@@ -42,8 +42,14 @@ export interface EntitySpec {
|
|
|
42
42
|
construct?: (raw: any) => any;
|
|
43
43
|
fetchMany?: (client: AvroQueryClient, ids: string[]) => Promise<any[]>;
|
|
44
44
|
fetchOne?: (client: AvroQueryClient, id: string) => Promise<any>;
|
|
45
|
-
listMatch?: (filters: Filters, item: any, scope: string) => ListMatch;
|
|
45
|
+
listMatch?: (filters: Filters, item: any, scope: string, viewerId?: string) => ListMatch;
|
|
46
46
|
alsoInvalidate?: readonly (readonly unknown[])[];
|
|
47
|
+
/**
|
|
48
|
+
* Derived queries (aggregates) a change makes stale. Every connected
|
|
49
|
+
* client gets the change, so these refresh at most once per
|
|
50
|
+
* THROTTLED_REFRESH_MS instead of once per change.
|
|
51
|
+
*/
|
|
52
|
+
refreshThrottled?: readonly (readonly string[])[];
|
|
47
53
|
invalidateOnly?: boolean;
|
|
48
54
|
}
|
|
49
55
|
export declare function constructJob(raw: any): Job;
|
|
@@ -56,7 +62,7 @@ export declare function removeEntity(qc: QueryClient, ns: string, id: string): v
|
|
|
56
62
|
* companies keeps both companies' lists cached).
|
|
57
63
|
*/
|
|
58
64
|
export declare function upsertEntity(qc: QueryClient, spec: EntitySpec, item: any, company?: string | null): void;
|
|
59
|
-
export declare function matchesList(type: string, key: readonly unknown[], item: any): ListMatch;
|
|
65
|
+
export declare function matchesList(type: string, key: readonly unknown[], item: any, viewerId?: string): ListMatch;
|
|
60
66
|
export declare function invalidateType(qc: QueryClient, spec: EntitySpec): void;
|
|
61
67
|
/**
|
|
62
68
|
* Refetch every query the server refused with a 403. Called when a
|
|
@@ -65,9 +71,12 @@ export declare function invalidateType(qc: QueryClient, spec: EntitySpec): void;
|
|
|
65
71
|
* focus, remount, or reload.
|
|
66
72
|
*/
|
|
67
73
|
export declare function retryDenied(qc: QueryClient): void;
|
|
74
|
+
/** Aggregates (analytics) refresh at most this often per client. */
|
|
75
|
+
export declare const THROTTLED_REFRESH_MS = 60000;
|
|
68
76
|
export declare class ChangeApplier {
|
|
69
77
|
private client;
|
|
70
78
|
private qc;
|
|
79
|
+
private refreshMembers?;
|
|
71
80
|
private pending;
|
|
72
81
|
private timer;
|
|
73
82
|
/**
|
|
@@ -81,7 +90,21 @@ export declare class ChangeApplier {
|
|
|
81
90
|
/** Company each pending `type:id` change came from (see upsertEntity). */
|
|
82
91
|
private companyOf;
|
|
83
92
|
private inflight;
|
|
84
|
-
|
|
93
|
+
/** Last run time and pending trailing timer per throttled key. */
|
|
94
|
+
private lastRefresh;
|
|
95
|
+
private refreshTimers;
|
|
96
|
+
/**
|
|
97
|
+
* @param refreshMembers fetches the named members and patches them
|
|
98
|
+
* into the member lists (the same path presence uses), so a
|
|
99
|
+
* change to one member doesn't re-download every member.
|
|
100
|
+
*/
|
|
101
|
+
constructor(client: AvroQueryClient, qc: QueryClient, refreshMembers?: ((userIds: string[]) => void) | undefined);
|
|
102
|
+
/**
|
|
103
|
+
* Refresh `key` now if it hasn't refreshed within the window,
|
|
104
|
+
* otherwise once when the window ends. Leading runs are jittered so
|
|
105
|
+
* clients that all received the same change don't refetch in step.
|
|
106
|
+
*/
|
|
107
|
+
private refreshThrottled;
|
|
85
108
|
private mark;
|
|
86
109
|
/** Settle a change applied inline: only an in-flight fetch needs the mark. */
|
|
87
110
|
private markApplied;
|
package/dist/client/realtime.js
CHANGED
|
@@ -29,6 +29,11 @@ export const VIEWER_SCOPE = 'mine';
|
|
|
29
29
|
export function listScope(key) {
|
|
30
30
|
return typeof key[2] === 'string' ? key[2] : '';
|
|
31
31
|
}
|
|
32
|
+
/** The signed-in user's id, from the cached self profile (`['user']`). */
|
|
33
|
+
function viewerIdOf(qc) {
|
|
34
|
+
const id = qc.getQueryData(['user'])?.id;
|
|
35
|
+
return typeof id === 'string' ? id : undefined;
|
|
36
|
+
}
|
|
32
37
|
function searchAware(filters) {
|
|
33
38
|
const q = filters.query;
|
|
34
39
|
return typeof q === 'string' && q.length > 0 ? 'unknown' : 'match';
|
|
@@ -139,10 +144,15 @@ function matchBills(filters, item) {
|
|
|
139
144
|
return 'unknown';
|
|
140
145
|
return searchAware(filters);
|
|
141
146
|
}
|
|
142
|
-
function matchSessions(filters, item, scope) {
|
|
143
|
-
// "mine"
|
|
144
|
-
|
|
145
|
-
|
|
147
|
+
function matchSessions(filters, item, scope, viewerId) {
|
|
148
|
+
// "mine" is GET /session: the viewer's own open sessions. Without
|
|
149
|
+
// knowing the viewer, every session change in the company would
|
|
150
|
+
// refetch it on every client.
|
|
151
|
+
if (scope === VIEWER_SCOPE) {
|
|
152
|
+
if (!viewerId)
|
|
153
|
+
return 'unknown';
|
|
154
|
+
return item.user_id === viewerId && !item.time_ended ? 'match' : 'nomatch';
|
|
155
|
+
}
|
|
146
156
|
// `unknown_ids` on the sessions endpoint means "fetch exactly these".
|
|
147
157
|
const wanted = filters.unknown_ids;
|
|
148
158
|
if (Array.isArray(wanted) && wanted.length > 0) {
|
|
@@ -202,6 +212,8 @@ const ALL_LINE_ITEM_STATUSES = { unbilled: true, billed: true, paid: true };
|
|
|
202
212
|
/** Single-entity GET (404/403 is treated as gone by the applier). */
|
|
203
213
|
const getOne = (path) => (client, id) => client.get({ path: path(id) });
|
|
204
214
|
const COMPANY_EMBEDS = [['companies'], ['/company/list']];
|
|
215
|
+
/** Finance, bill, event and revenue aggregates all read line items and bills. */
|
|
216
|
+
const ANALYTICS = [['analytics']];
|
|
205
217
|
export const REGISTRY = {
|
|
206
218
|
job: {
|
|
207
219
|
ns: 'jobs',
|
|
@@ -214,6 +226,7 @@ export const REGISTRY = {
|
|
|
214
226
|
construct: constructEvent,
|
|
215
227
|
fetchMany: bulk('fetchEvents', ALL_LINE_ITEM_STATUSES),
|
|
216
228
|
listMatch: matchLineItems,
|
|
229
|
+
refreshThrottled: ANALYTICS,
|
|
217
230
|
},
|
|
218
231
|
route: {
|
|
219
232
|
ns: 'routes',
|
|
@@ -261,24 +274,27 @@ export const REGISTRY = {
|
|
|
261
274
|
ns: 'months',
|
|
262
275
|
fetchMany: bulk('fetchMonths', ALL_LINE_ITEM_STATUSES),
|
|
263
276
|
listMatch: matchLineItems,
|
|
277
|
+
refreshThrottled: ANALYTICS,
|
|
264
278
|
},
|
|
265
279
|
prepayment: {
|
|
266
280
|
ns: 'prepayments',
|
|
267
281
|
fetchMany: bulk('fetchPrepayments', ALL_LINE_ITEM_STATUSES),
|
|
268
282
|
listMatch: matchLineItems,
|
|
283
|
+
refreshThrottled: ANALYTICS,
|
|
269
284
|
},
|
|
270
285
|
bill: {
|
|
271
286
|
ns: 'bills',
|
|
272
287
|
construct: (d) => new Bill(d),
|
|
273
288
|
fetchMany: bulk('fetchBills'),
|
|
274
289
|
listMatch: matchBills,
|
|
290
|
+
refreshThrottled: ANALYTICS,
|
|
275
291
|
},
|
|
276
292
|
session: {
|
|
277
293
|
ns: 'sessions',
|
|
278
294
|
construct: (raw) => new Session(raw),
|
|
279
295
|
fetchMany: bulk('fetchSessions'),
|
|
280
296
|
listMatch: matchSessions,
|
|
281
|
-
|
|
297
|
+
refreshThrottled: [['analytics', 'sessions']],
|
|
282
298
|
},
|
|
283
299
|
checkin: { ns: 'checkins', listMatch: matchCheckins },
|
|
284
300
|
waiver: { ns: 'waivers', fetchMany: bulk('fetchWaivers'), listMatch: searchAware },
|
|
@@ -296,8 +312,10 @@ export const REGISTRY = {
|
|
|
296
312
|
alsoInvalidate: [['/company/list'], ['companies', 'current']],
|
|
297
313
|
},
|
|
298
314
|
member: { ns: 'members', invalidateOnly: true, alsoInvalidate: [['users', 'company']] },
|
|
315
|
+
// Sent to the user's own room only. Their rows in member lists
|
|
316
|
+
// refresh through the `member` changes sent alongside.
|
|
299
317
|
user: { ns: 'users', invalidateOnly: true, alsoInvalidate: [['user']] },
|
|
300
|
-
plan: { ns: 'plans', invalidateOnly: true },
|
|
318
|
+
plan: { ns: 'plans', invalidateOnly: true, refreshThrottled: ANALYTICS },
|
|
301
319
|
};
|
|
302
320
|
function mapList(data, fn) {
|
|
303
321
|
if (Array.isArray(data))
|
|
@@ -344,13 +362,14 @@ export function upsertEntity(qc, spec, item, company) {
|
|
|
344
362
|
const lists = qc
|
|
345
363
|
.getQueryCache()
|
|
346
364
|
.findAll({ predicate: (q) => q.queryKey[0] === spec.ns && isListKey(q.queryKey) });
|
|
365
|
+
const viewerId = viewerIdOf(qc);
|
|
347
366
|
for (const query of lists) {
|
|
348
367
|
const key = query.queryKey;
|
|
349
368
|
const scope = listScope(key);
|
|
350
369
|
if (company && scope && scope !== company && scope !== VIEWER_SCOPE)
|
|
351
370
|
continue;
|
|
352
371
|
const verdict = spec.listMatch
|
|
353
|
-
? spec.listMatch(listFilters(key), item,
|
|
372
|
+
? spec.listMatch(listFilters(key), item, scope, viewerId)
|
|
354
373
|
: searchAware(listFilters(key));
|
|
355
374
|
if (verdict === 'unknown') {
|
|
356
375
|
qc.invalidateQueries({ queryKey: key, exact: true });
|
|
@@ -369,12 +388,12 @@ export function upsertEntity(qc, spec, item, company) {
|
|
|
369
388
|
});
|
|
370
389
|
}
|
|
371
390
|
}
|
|
372
|
-
export function matchesList(type, key, item) {
|
|
391
|
+
export function matchesList(type, key, item, viewerId) {
|
|
373
392
|
const spec = REGISTRY[type];
|
|
374
393
|
if (!spec || !isListKey(key))
|
|
375
394
|
return 'nomatch';
|
|
376
395
|
return spec.listMatch
|
|
377
|
-
? spec.listMatch(listFilters(key), item, listScope(key))
|
|
396
|
+
? spec.listMatch(listFilters(key), item, listScope(key), viewerId)
|
|
378
397
|
: searchAware(listFilters(key));
|
|
379
398
|
}
|
|
380
399
|
export function invalidateType(qc, spec) {
|
|
@@ -396,6 +415,9 @@ export function retryDenied(qc) {
|
|
|
396
415
|
// ─── Applying batches ───────────────────────────────────────────────
|
|
397
416
|
const FETCH_COALESCE_MS = 50;
|
|
398
417
|
const FETCH_CHUNK = 100;
|
|
418
|
+
/** Aggregates (analytics) refresh at most this often per client. */
|
|
419
|
+
export const THROTTLED_REFRESH_MS = 60000;
|
|
420
|
+
const REFRESH_JITTER_MS = 5000;
|
|
399
421
|
/** A fetch failure that means "this entity is gone or no longer visible". */
|
|
400
422
|
function isGoneError(err) {
|
|
401
423
|
const status = err?.status;
|
|
@@ -407,9 +429,15 @@ function dropFromLists(qc, ns, id) {
|
|
|
407
429
|
qc.invalidateQueries({ queryKey: keys.detail(ns, id), exact: true });
|
|
408
430
|
}
|
|
409
431
|
export class ChangeApplier {
|
|
410
|
-
|
|
432
|
+
/**
|
|
433
|
+
* @param refreshMembers fetches the named members and patches them
|
|
434
|
+
* into the member lists (the same path presence uses), so a
|
|
435
|
+
* change to one member doesn't re-download every member.
|
|
436
|
+
*/
|
|
437
|
+
constructor(client, qc, refreshMembers) {
|
|
411
438
|
this.client = client;
|
|
412
439
|
this.qc = qc;
|
|
440
|
+
this.refreshMembers = refreshMembers;
|
|
413
441
|
this.pending = new Map();
|
|
414
442
|
this.timer = null;
|
|
415
443
|
/**
|
|
@@ -423,6 +451,28 @@ export class ChangeApplier {
|
|
|
423
451
|
/** Company each pending `type:id` change came from (see upsertEntity). */
|
|
424
452
|
this.companyOf = new Map();
|
|
425
453
|
this.inflight = new Map();
|
|
454
|
+
/** Last run time and pending trailing timer per throttled key. */
|
|
455
|
+
this.lastRefresh = new Map();
|
|
456
|
+
this.refreshTimers = new Map();
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Refresh `key` now if it hasn't refreshed within the window,
|
|
460
|
+
* otherwise once when the window ends. Leading runs are jittered so
|
|
461
|
+
* clients that all received the same change don't refetch in step.
|
|
462
|
+
*/
|
|
463
|
+
refreshThrottled(key) {
|
|
464
|
+
const id = JSON.stringify(key);
|
|
465
|
+
if (this.refreshTimers.has(id))
|
|
466
|
+
return;
|
|
467
|
+
const last = this.lastRefresh.get(id);
|
|
468
|
+
const wait = last === undefined || Date.now() - last >= THROTTLED_REFRESH_MS
|
|
469
|
+
? Math.random() * REFRESH_JITTER_MS
|
|
470
|
+
: last + THROTTLED_REFRESH_MS - Date.now();
|
|
471
|
+
this.refreshTimers.set(id, setTimeout(() => {
|
|
472
|
+
this.refreshTimers.delete(id);
|
|
473
|
+
this.lastRefresh.set(id, Date.now());
|
|
474
|
+
void this.qc.invalidateQueries({ queryKey: [...key] });
|
|
475
|
+
}, wait));
|
|
426
476
|
}
|
|
427
477
|
mark(type, id) {
|
|
428
478
|
this.latest.set(`${type}:${id}`, ++this.seq);
|
|
@@ -443,6 +493,22 @@ export class ChangeApplier {
|
|
|
443
493
|
const spec = REGISTRY[entry.type];
|
|
444
494
|
if (!spec)
|
|
445
495
|
continue;
|
|
496
|
+
spec.refreshThrottled?.forEach((k) => this.refreshThrottled(k));
|
|
497
|
+
// A named member: patch just that row on every client. No id
|
|
498
|
+
// (joins, leaves, group-wide permission edits) still means the
|
|
499
|
+
// whole list.
|
|
500
|
+
if (entry.type === 'member' && entry.id && this.refreshMembers) {
|
|
501
|
+
this.refreshMembers([entry.id]);
|
|
502
|
+
retryDenied(this.qc);
|
|
503
|
+
continue;
|
|
504
|
+
}
|
|
505
|
+
// The viewer's own profile; their member rows follow via
|
|
506
|
+
// `member` changes, so don't refetch the whole users namespace.
|
|
507
|
+
if (entry.type === 'user' && entry.id) {
|
|
508
|
+
this.qc.invalidateQueries({ queryKey: ['user'] });
|
|
509
|
+
this.qc.invalidateQueries({ queryKey: keys.detail('users', entry.id) });
|
|
510
|
+
continue;
|
|
511
|
+
}
|
|
446
512
|
if (entry.op === 'invalidate' || spec.invalidateOnly) {
|
|
447
513
|
invalidateType(this.qc, spec);
|
|
448
514
|
if (entry.type === 'member')
|
|
@@ -598,6 +664,9 @@ export class ChangeApplier {
|
|
|
598
664
|
if (this.timer)
|
|
599
665
|
clearTimeout(this.timer);
|
|
600
666
|
this.timer = null;
|
|
667
|
+
this.refreshTimers.forEach(clearTimeout);
|
|
668
|
+
this.refreshTimers.clear();
|
|
669
|
+
this.lastRefresh.clear();
|
|
601
670
|
this.pending.clear();
|
|
602
671
|
this.companyOf.clear();
|
|
603
672
|
this.latest.clear();
|
package/dist/types/api/Avro.d.ts
CHANGED
|
@@ -165,6 +165,11 @@ export interface ErrorLogEntry {
|
|
|
165
165
|
status?: number | null;
|
|
166
166
|
message: string | null;
|
|
167
167
|
traceback?: string | null;
|
|
168
|
+
/**
|
|
169
|
+
* Per-field problems of a rejected request (`"field: problem"`). A
|
|
170
|
+
* string when the list was too long and was stored truncated.
|
|
171
|
+
*/
|
|
172
|
+
errors?: string[] | string | null;
|
|
168
173
|
stack?: string | null;
|
|
169
174
|
fatal?: boolean | null;
|
|
170
175
|
platform?: string | null;
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const AVRO_JS_VERSION = "1.6.6-
|
|
1
|
+
export declare const AVRO_JS_VERSION = "1.6.6-rc13";
|
package/dist/version.js
CHANGED