@go-avro/avro-js 0.0.116 → 0.0.118

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.
@@ -155,12 +155,16 @@ const SOCKET_EVENT_CONFIG = {
155
155
  update_jobs: { invalidateKeys: [['jobs'], ['infinite', 'jobs']] },
156
156
  delete_jobs: { invalidateKeys: [['jobs'], ['infinite', 'jobs']] },
157
157
  // ── Routes ──
158
+ // Route membership lives on jobs too (task.route_ids), so every
159
+ // route mutation must refresh the jobs caches or unrouted/routed
160
+ // state goes stale.
158
161
  create_route: {
159
162
  entityKey: 'routes',
160
163
  action: 'create',
161
164
  fetchPath: (id) => `/route/${id}`,
162
165
  construct: (d) => new Route(d),
163
166
  matchesQuery: matchesRoutesListQuery,
167
+ alsoInvalidate: [['jobs'], ['infinite', 'jobs']],
164
168
  },
165
169
  update_route: {
166
170
  entityKey: 'routes',
@@ -168,8 +172,14 @@ const SOCKET_EVENT_CONFIG = {
168
172
  fetchPath: (id) => `/route/${id}`,
169
173
  construct: (d) => new Route(d),
170
174
  matchesQuery: matchesRoutesListQuery,
175
+ alsoInvalidate: [['jobs'], ['infinite', 'jobs']],
176
+ },
177
+ delete_route: {
178
+ entityKey: 'routes',
179
+ action: 'delete',
180
+ fetchPath: null,
181
+ alsoInvalidate: [['jobs'], ['infinite', 'jobs']],
171
182
  },
172
- delete_route: { entityKey: 'routes', action: 'delete', fetchPath: null },
173
183
  // ── Events (also refetch parent job — overdueness, last_event, etc.) ──
174
184
  create_event: {
175
185
  entityKey: 'events',
@@ -350,6 +360,11 @@ const SOCKET_EVENT_CONFIG = {
350
360
  construct: (d) => new Job(d),
351
361
  },
352
362
  // ── Scheduling ──
363
+ // Handled surgically in setupSocketInvalidation: when the payload
364
+ // carries `job_ids`, the affected jobs are bulk-fetched and patched
365
+ // in place instead of refetching every jobs query. These keys are
366
+ // the fallback for payloads without ids (legacy backends, runs
367
+ // where the affected set is unknown).
353
368
  schedule_complete: {
354
369
  invalidateKeys: [
355
370
  ['routes'],
@@ -579,6 +594,96 @@ export class AvroQueryClient {
579
594
  handlers.push({ event, handler });
580
595
  continue;
581
596
  }
597
+ if (event === 'schedule_complete' && isBulkEvent(config)) {
598
+ // Scheduling touches a known set of jobs; refetching every
599
+ // jobs query on each run is untenable. When the payload
600
+ // names the affected ids, bulk-fetch just those jobs and
601
+ // patch them into the cached lists in place.
602
+ const handler = async (data) => {
603
+ // Always cheap and always stale after a run: the new
604
+ // routes and the company's schedule_locked banner.
605
+ queryClient.invalidateQueries({ queryKey: ['routes'] });
606
+ queryClient.invalidateQueries({ queryKey: ['companies'] });
607
+ queryClient.invalidateQueries({ queryKey: ['/company/list'] });
608
+ const invalidateJobs = () => {
609
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
610
+ queryClient.invalidateQueries({ queryKey: ['infinite', 'jobs'] });
611
+ };
612
+ const rawIds = data?.job_ids;
613
+ if (!Array.isArray(rawIds)) {
614
+ // Legacy backend / unknown affected set
615
+ invalidateJobs();
616
+ return;
617
+ }
618
+ const jobIds = rawIds.filter((x) => typeof x === 'string');
619
+ if (jobIds.length === 0)
620
+ return;
621
+ try {
622
+ // One request for exactly the affected jobs
623
+ // (`unknown_ids` = "only these ids"), serialized
624
+ // server-side with this user's own permissions.
625
+ const raw = await client.fetchJobs({
626
+ unknown_ids: jobIds,
627
+ amt: jobIds.length,
628
+ });
629
+ const fresh = new Map(raw.filter((d) => d?.id).map((d) => [d.id, new Job(d)]));
630
+ const matching = queryClient
631
+ .getQueryCache()
632
+ .findAll({ predicate: (q) => matchesEntityKey(q, 'jobs') });
633
+ for (const query of matching) {
634
+ const queryKey = query.queryKey;
635
+ // Detail cache: ['jobs', jobId]
636
+ if (queryKey.length === 2 && queryKey[0] === 'jobs') {
637
+ const detailId = queryKey[1];
638
+ if (typeof detailId === 'string' && fresh.has(detailId)) {
639
+ queryClient.setQueryData(queryKey, fresh.get(detailId));
640
+ }
641
+ continue;
642
+ }
643
+ // Route-filtered lists: scheduling changes
644
+ // membership, and an in-place patch can't
645
+ // insert newly-assigned jobs — refetch those
646
+ // (only mounted route views hold them).
647
+ if (queryKey[0] === 'infinite' &&
648
+ typeof queryKey[5] === 'string' &&
649
+ queryKey[5].length > 0) {
650
+ queryClient.invalidateQueries({ queryKey, exact: true });
651
+ continue;
652
+ }
653
+ queryClient.setQueryData(queryKey, (old) => {
654
+ if (!old)
655
+ return old;
656
+ const patchList = (list) => {
657
+ let changed = false;
658
+ const next = list.map((x) => {
659
+ const replacement = x?.id ? fresh.get(x.id) : undefined;
660
+ if (!replacement)
661
+ return x;
662
+ changed = true;
663
+ return replacement;
664
+ });
665
+ return changed ? next : list;
666
+ };
667
+ if (old.pages && Array.isArray(old.pages)) {
668
+ return {
669
+ ...old,
670
+ pages: old.pages.map((p) => Array.isArray(p) ? patchList(p) : p),
671
+ };
672
+ }
673
+ if (Array.isArray(old))
674
+ return patchList(old);
675
+ return old;
676
+ });
677
+ }
678
+ }
679
+ catch {
680
+ invalidateJobs();
681
+ }
682
+ };
683
+ this.socket.on(event, handler);
684
+ handlers.push({ event, handler });
685
+ continue;
686
+ }
582
687
  if (isBulkEvent(config)) {
583
688
  // ── Bulk: invalidate all specified keys ──────────────
584
689
  const handler = () => {
@@ -95,6 +95,9 @@ AvroQueryClient.prototype.useCreateRoute = function () {
95
95
  fetchPath: `/route/${result.id}`,
96
96
  construct: (d) => new Route(d),
97
97
  });
98
+ // task.route_ids lives in the jobs caches
99
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
100
+ queryClient.invalidateQueries({ queryKey: ['infinite', 'jobs'] });
98
101
  },
99
102
  onError: (_err, _variables, context) => {
100
103
  if (context?.previousRoutes) {
@@ -179,6 +182,9 @@ AvroQueryClient.prototype.useUpdateRoute = function () {
179
182
  fetchPath: `/route/${routeId}`,
180
183
  construct: (d) => new Route(d),
181
184
  });
185
+ // task.route_ids lives in the jobs caches
186
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
187
+ queryClient.invalidateQueries({ queryKey: ['infinite', 'jobs'] });
182
188
  },
183
189
  onError: (_err, variables, context) => {
184
190
  const { routeId } = variables;
@@ -223,6 +229,11 @@ AvroQueryClient.prototype.useDeleteRoute = function () {
223
229
  entityKey: 'routes',
224
230
  id: routeId,
225
231
  });
232
+ // task.route_ids lives in the jobs caches — without this,
233
+ // freed tasks still look routed and get excluded from the
234
+ // next scheduling attempt.
235
+ queryClient.invalidateQueries({ queryKey: ['jobs'] });
236
+ queryClient.invalidateQueries({ queryKey: ['infinite', 'jobs'] });
226
237
  },
227
238
  onError: (_err, variables, context) => {
228
239
  const { routeId } = variables;
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const AVRO_JS_VERSION = "0.0.116";
1
+ export declare const AVRO_JS_VERSION = "0.0.118";
package/dist/version.js CHANGED
@@ -1,3 +1,3 @@
1
1
  // AUTO-GENERATED by scripts/gen-version.js — do not edit by hand.
2
2
  // Regenerated from package.json by the `prebuild` npm hook.
3
- export const AVRO_JS_VERSION = '0.0.116';
3
+ export const AVRO_JS_VERSION = '0.0.118';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@go-avro/avro-js",
3
- "version": "0.0.116",
3
+ "version": "0.0.118",
4
4
  "description": "JS client for Avro backend integration.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",