@ai-matrx/records 0.76.1 → 0.76.2

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.
@@ -43218,21 +43218,31 @@ function isPlainNewestFirst(key) {
43218
43218
  }
43219
43219
  }
43220
43220
  var REREAD_PAGES_MAX = 2;
43221
- function placeUnseen(qc, portKey, alias, rows) {
43221
+ async function sourceKind(qc, port, portKey, alias) {
43222
+ try {
43223
+ const sources = await qc.ensureQueryData({ queryKey: dataKeys.sources(portKey), queryFn: async () => unwrap(await port.sources()) });
43224
+ return sources.find((s) => s.alias === alias)?.kind ?? null;
43225
+ } catch {
43226
+ return null;
43227
+ }
43228
+ }
43229
+ function rereadOrCount(qc, key, data, count) {
43230
+ if (data.pages.length <= REREAD_PAGES_MAX) void qc.invalidateQueries({ queryKey: key, exact: true });
43231
+ else qc.setQueryData(key, { ...data, newer: (data.newer ?? 0) + count });
43232
+ }
43233
+ function placeUnseen(qc, portKey, alias, rows, kind) {
43222
43234
  for (const [key, data] of qc.getQueriesData({ queryKey: dataKeys.rowsPrefix(portKey, alias) })) {
43223
43235
  if (!data || data.pages.length === 0) continue;
43224
43236
  const missing = rows.filter((row) => !data.pages.some((page2) => page2.rows.some((r) => r._id === row._id)));
43225
43237
  if (missing.length === 0) continue;
43226
- if (isPlainNewestFirst(key)) {
43238
+ if (kind === "table" && isPlainNewestFirst(key)) {
43227
43239
  const [first, ...rest] = data.pages;
43228
43240
  qc.setQueryData(key, {
43229
43241
  ...data,
43230
43242
  pages: [{ ...first, rows: [...missing, ...first.rows], total: first.total === null ? null : first.total + missing.length }, ...rest]
43231
43243
  });
43232
- } else if (data.pages.length <= REREAD_PAGES_MAX) {
43233
- void qc.invalidateQueries({ queryKey: key, exact: true });
43234
43244
  } else {
43235
- qc.setQueryData(key, { ...data, newer: (data.newer ?? 0) + missing.length });
43245
+ rereadOrCount(qc, key, data, missing.length);
43236
43246
  }
43237
43247
  }
43238
43248
  }
@@ -43247,25 +43257,35 @@ async function rereadIds(qc, port, portKey, alias, ids) {
43247
43257
  unseen.push(answer.data);
43248
43258
  }
43249
43259
  }
43250
- if (unseen.length > 0) placeUnseen(qc, portKey, alias, unseen);
43260
+ if (unseen.length > 0) placeUnseen(qc, portKey, alias, unseen, await sourceKind(qc, port, portKey, alias));
43261
+ }
43262
+ function rereadUnknown(qc, portKey, alias) {
43263
+ for (const [key, data] of qc.getQueriesData({ queryKey: dataKeys.rowsPrefix(portKey, alias) })) {
43264
+ if (!data || data.pages.length === 0) {
43265
+ void qc.invalidateQueries({ queryKey: key, exact: true });
43266
+ continue;
43267
+ }
43268
+ if (data.pages.length <= REREAD_PAGES_MAX) void qc.invalidateQueries({ queryKey: key, exact: true });
43269
+ else qc.setQueryData(key, { ...data, newer: Math.max(data.newer ?? 0, 1) });
43270
+ }
43271
+ void qc.invalidateQueries({ queryKey: [...dataKeys.alias(portKey, alias), "row"] });
43251
43272
  }
43252
43273
  function onBump(qc, port, portKey, alias, entry, event) {
43253
43274
  if (event.shape) {
43254
43275
  void qc.invalidateQueries({ queryKey: dataKeys.alias(portKey, alias) });
43255
43276
  return;
43256
43277
  }
43257
- if (event.ids === null) {
43258
- void qc.invalidateQueries({ queryKey: dataKeys.rowsPrefix(portKey, alias) });
43259
- for (const [key] of qc.getQueriesData({ queryKey: [...dataKeys.alias(portKey, alias), "row"] })) void qc.invalidateQueries({ queryKey: key });
43260
- return;
43261
- }
43262
- for (const id of event.ids) entry.pending.add(id);
43278
+ if (event.ids === null) entry.unknown = true;
43279
+ else for (const id of event.ids) entry.pending.add(id);
43263
43280
  if (entry.flush) return;
43264
43281
  entry.flush = setTimeout(() => {
43265
43282
  entry.flush = null;
43266
43283
  const ids = [...entry.pending];
43284
+ const unknown = entry.unknown;
43267
43285
  entry.pending.clear();
43268
- if (ids.length > 0) void rereadIds(qc, port, portKey, alias, ids);
43286
+ entry.unknown = false;
43287
+ if (unknown) rereadUnknown(qc, portKey, alias);
43288
+ else if (ids.length > 0) void rereadIds(qc, port, portKey, alias, ids);
43269
43289
  }, BUMP_BATCH_MS);
43270
43290
  }
43271
43291
  function acquireLive(qc, port, events, portKey, alias) {
@@ -43274,7 +43294,7 @@ function acquireLive(qc, port, events, portKey, alias) {
43274
43294
  let entry = home.get(key);
43275
43295
  if (!entry) {
43276
43296
  const made = { refs: 0, stop: () => {
43277
- }, timer: null, pending: /* @__PURE__ */ new Set(), flush: null };
43297
+ }, timer: null, pending: /* @__PURE__ */ new Set(), unknown: false, flush: null };
43278
43298
  if (events) {
43279
43299
  made.stop = events.on(`rows:${alias}`, (event) => onBump(qc, port, portKey, alias, made, event));
43280
43300
  } else if (!warnedNotLive) {
@@ -43362,6 +43382,15 @@ function useDataRows(alias, query = {}, scope) {
43362
43382
  };
43363
43383
  }
43364
43384
  var tempCounter = 0;
43385
+ function drawPatch(before, patch) {
43386
+ const drawn = { ...before, ...patch, _id: before._id, _version: before._version, _organization_id: before._organization_id };
43387
+ if (patch._custom && typeof patch._custom === "object") {
43388
+ drawn._custom = { ...before._custom ?? {}, ...patch._custom };
43389
+ } else if (before._custom !== void 0) {
43390
+ drawn._custom = before._custom;
43391
+ }
43392
+ return drawn;
43393
+ }
43365
43394
  function useDataMutations(alias, scope) {
43366
43395
  const qc = useRecordsQueryClient();
43367
43396
  const { port, portKey } = useDataPort(scope);
@@ -43401,7 +43430,7 @@ function useDataMutations(alias, scope) {
43401
43430
  await qc.cancelQueries({ queryKey: dataKeys.alias(portKey, alias) });
43402
43431
  const rowKey = dataKeys.row(portKey, alias, id);
43403
43432
  const rowBefore = qc.getQueryData(rowKey);
43404
- if (before) patchRow(qc, portKey, alias, { ...before, ...patch, _id: id, _version: before._version, _organization_id: before._organization_id }, { force: true });
43433
+ if (before) patchRow(qc, portKey, alias, drawPatch(before, patch), { force: true });
43405
43434
  const answer = await port.update(alias, id, patch, baseVersion);
43406
43435
  if (answer.ok) {
43407
43436
  patchRow(qc, portKey, alias, answer.data, { force: true });