@ai-matrx/records 0.76.4 → 0.76.6
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/CHANGELOG.md +21 -0
- package/dist/data/react.cjs +129 -9
- package/dist/data/react.cjs.map +1 -1
- package/dist/data/react.js +131 -10
- package/dist/data/react.js.map +1 -1
- package/dist/react/index.cjs +129 -9
- package/dist/react/index.cjs.map +1 -1
- package/dist/react/index.js +131 -10
- package/dist/react/index.js.map +1 -1
- package/package.json +1 -1
package/dist/react/index.cjs
CHANGED
|
@@ -43418,7 +43418,64 @@ function forEachRowsEntry(qc, portKey, alias, fn) {
|
|
|
43418
43418
|
if (next && next !== data) qc.setQueryData(key, next);
|
|
43419
43419
|
}
|
|
43420
43420
|
}
|
|
43421
|
+
var LANDED_MS = 3e4;
|
|
43422
|
+
var LANDED_KEY = /* @__PURE__ */ Symbol.for("@ai-matrx/records/data-landed");
|
|
43423
|
+
function landedHome(qc) {
|
|
43424
|
+
const holder = qc;
|
|
43425
|
+
let home = holder[LANDED_KEY];
|
|
43426
|
+
if (!home) {
|
|
43427
|
+
const made = { byAlias: /* @__PURE__ */ new Map(), unsubscribe: () => {
|
|
43428
|
+
} };
|
|
43429
|
+
made.unsubscribe = qc.getQueryCache().subscribe((event) => {
|
|
43430
|
+
if (event.type !== "updated" || event.action.type !== "success") return;
|
|
43431
|
+
const key = event.query.queryKey;
|
|
43432
|
+
if (key[0] !== RECORDS_QUERY_ROOT || key[1] !== "data" || key[3] !== "alias" || key[5] !== "rows") return;
|
|
43433
|
+
const portKey = String(key[2]);
|
|
43434
|
+
const alias = String(key[4]);
|
|
43435
|
+
const landed = made.byAlias.get(`${portKey}${alias}`);
|
|
43436
|
+
if (!landed || landed.size === 0) return;
|
|
43437
|
+
reapplyLanded(qc, event.query.queryKey, landed);
|
|
43438
|
+
});
|
|
43439
|
+
holder[LANDED_KEY] = made;
|
|
43440
|
+
home = made;
|
|
43441
|
+
}
|
|
43442
|
+
return home;
|
|
43443
|
+
}
|
|
43444
|
+
function remember(qc, portKey, alias, id, row) {
|
|
43445
|
+
const home = landedHome(qc);
|
|
43446
|
+
const key = `${portKey}${alias}`;
|
|
43447
|
+
let landed = home.byAlias.get(key);
|
|
43448
|
+
if (!landed) home.byAlias.set(key, landed = /* @__PURE__ */ new Map());
|
|
43449
|
+
const now = Date.now();
|
|
43450
|
+
for (const [oldId, entry] of landed) if (now - entry.at > LANDED_MS) landed.delete(oldId);
|
|
43451
|
+
landed.set(id, { row, at: now });
|
|
43452
|
+
}
|
|
43453
|
+
function reapplyLanded(qc, key, landed) {
|
|
43454
|
+
const data = qc.getQueryData(key);
|
|
43455
|
+
if (!data) return;
|
|
43456
|
+
const now = Date.now();
|
|
43457
|
+
let changed = false;
|
|
43458
|
+
const pages = data.pages.map((page2) => {
|
|
43459
|
+
let rows = null;
|
|
43460
|
+
page2.rows.forEach((r, i) => {
|
|
43461
|
+
const entry = landed.get(r._id);
|
|
43462
|
+
if (!entry || now - entry.at > LANDED_MS) return;
|
|
43463
|
+
if (entry.row === null) {
|
|
43464
|
+
rows ??= page2.rows.slice();
|
|
43465
|
+
rows[i] = null;
|
|
43466
|
+
} else if (entry.row._version > r._version) {
|
|
43467
|
+
rows ??= page2.rows.slice();
|
|
43468
|
+
rows[i] = entry.row;
|
|
43469
|
+
}
|
|
43470
|
+
});
|
|
43471
|
+
if (!rows) return page2;
|
|
43472
|
+
changed = true;
|
|
43473
|
+
return { ...page2, rows: rows.filter((r) => r !== null) };
|
|
43474
|
+
});
|
|
43475
|
+
if (changed) qc.setQueryData(key, { ...data, pages });
|
|
43476
|
+
}
|
|
43421
43477
|
function patchRow(qc, portKey, alias, row, options = {}) {
|
|
43478
|
+
if (options.fromStore) remember(qc, portKey, alias, row._id, row);
|
|
43422
43479
|
let held = false;
|
|
43423
43480
|
forEachRowsEntry(qc, portKey, alias, (_key, data) => {
|
|
43424
43481
|
let changed = false;
|
|
@@ -43439,7 +43496,8 @@ function patchRow(qc, portKey, alias, row, options = {}) {
|
|
|
43439
43496
|
if (options.force || !current || current._version <= row._version) qc.setQueryData(rowKey, row);
|
|
43440
43497
|
return held;
|
|
43441
43498
|
}
|
|
43442
|
-
function dropRow(qc, portKey, alias, id) {
|
|
43499
|
+
function dropRow(qc, portKey, alias, id, options = {}) {
|
|
43500
|
+
if (options.fromStore) remember(qc, portKey, alias, id, null);
|
|
43443
43501
|
forEachRowsEntry(qc, portKey, alias, (_key, data) => {
|
|
43444
43502
|
let changed = false;
|
|
43445
43503
|
const pages = data.pages.map((page2) => {
|
|
@@ -43489,8 +43547,52 @@ async function sourceKind(qc, port, portKey, alias) {
|
|
|
43489
43547
|
return null;
|
|
43490
43548
|
}
|
|
43491
43549
|
}
|
|
43550
|
+
var DEFERRED_KEY = /* @__PURE__ */ Symbol.for("@ai-matrx/records/data-deferred-rereads");
|
|
43551
|
+
var OWED_KEY = /* @__PURE__ */ Symbol.for("@ai-matrx/records/data-owed-pages");
|
|
43552
|
+
function owedPages(qc) {
|
|
43553
|
+
const holder = qc;
|
|
43554
|
+
return holder[OWED_KEY] ??= /* @__PURE__ */ new Map();
|
|
43555
|
+
}
|
|
43556
|
+
function owedPage(qc, hash) {
|
|
43557
|
+
const owed = owedPages(qc).get(hash);
|
|
43558
|
+
if (!owed) return null;
|
|
43559
|
+
const query = qc.getQueryCache().get(hash);
|
|
43560
|
+
const data = query?.state.data;
|
|
43561
|
+
const last = data?.pages[data.pages.length - 1];
|
|
43562
|
+
if (!query || query.state.status === "error" || !data || data.pages.length >= owed.pages || !last?.cursor) {
|
|
43563
|
+
owedPages(qc).delete(hash);
|
|
43564
|
+
return null;
|
|
43565
|
+
}
|
|
43566
|
+
return owed;
|
|
43567
|
+
}
|
|
43568
|
+
function rereadList(qc, key) {
|
|
43569
|
+
const query = qc.getQueryCache().find({ queryKey: key, exact: true });
|
|
43570
|
+
if (!query || query.state.fetchStatus !== "fetching" && !owedPage(qc, query.queryHash)) {
|
|
43571
|
+
void qc.invalidateQueries({ queryKey: key, exact: true });
|
|
43572
|
+
return;
|
|
43573
|
+
}
|
|
43574
|
+
const holder = qc;
|
|
43575
|
+
const waiting = holder[DEFERRED_KEY] ??= /* @__PURE__ */ new Set();
|
|
43576
|
+
if (waiting.has(query.queryHash)) return;
|
|
43577
|
+
waiting.add(query.queryHash);
|
|
43578
|
+
const settle = () => {
|
|
43579
|
+
if (query.state.fetchStatus === "fetching") return;
|
|
43580
|
+
const owed = owedPage(qc, query.queryHash);
|
|
43581
|
+
if (owed) {
|
|
43582
|
+
owed.fetchNext();
|
|
43583
|
+
return;
|
|
43584
|
+
}
|
|
43585
|
+
stop();
|
|
43586
|
+
waiting.delete(query.queryHash);
|
|
43587
|
+
void qc.invalidateQueries({ queryKey: key, exact: true });
|
|
43588
|
+
};
|
|
43589
|
+
const stop = qc.getQueryCache().subscribe((event) => {
|
|
43590
|
+
if (event.query === query) settle();
|
|
43591
|
+
});
|
|
43592
|
+
settle();
|
|
43593
|
+
}
|
|
43492
43594
|
function rereadOrCount(qc, key, data, count) {
|
|
43493
|
-
if (data.pages.length <= REREAD_PAGES_MAX)
|
|
43595
|
+
if (data.pages.length <= REREAD_PAGES_MAX) rereadList(qc, key);
|
|
43494
43596
|
else qc.setQueryData(key, { ...data, newer: (data.newer ?? 0) + count });
|
|
43495
43597
|
}
|
|
43496
43598
|
function placeUnseen(qc, portKey, alias, rows, kind) {
|
|
@@ -43515,8 +43617,8 @@ async function rereadIds(qc, port, portKey, alias, ids) {
|
|
|
43515
43617
|
for (const [id, answer] of answers) {
|
|
43516
43618
|
if (!answer.ok) continue;
|
|
43517
43619
|
if (answer.data === null) {
|
|
43518
|
-
dropRow(qc, portKey, alias, id);
|
|
43519
|
-
} else if (!patchRow(qc, portKey, alias, answer.data)) {
|
|
43620
|
+
dropRow(qc, portKey, alias, id, { fromStore: true });
|
|
43621
|
+
} else if (!patchRow(qc, portKey, alias, answer.data, { fromStore: true })) {
|
|
43520
43622
|
unseen.push(answer.data);
|
|
43521
43623
|
}
|
|
43522
43624
|
}
|
|
@@ -43525,10 +43627,10 @@ async function rereadIds(qc, port, portKey, alias, ids) {
|
|
|
43525
43627
|
function rereadUnknown(qc, portKey, alias) {
|
|
43526
43628
|
for (const [key, data] of qc.getQueriesData({ queryKey: dataKeys.rowsPrefix(portKey, alias) })) {
|
|
43527
43629
|
if (!data || data.pages.length === 0) {
|
|
43528
|
-
|
|
43630
|
+
rereadList(qc, key);
|
|
43529
43631
|
continue;
|
|
43530
43632
|
}
|
|
43531
|
-
if (data.pages.length <= REREAD_PAGES_MAX)
|
|
43633
|
+
if (data.pages.length <= REREAD_PAGES_MAX) rereadList(qc, key);
|
|
43532
43634
|
else qc.setQueryData(key, { ...data, newer: Math.max(data.newer ?? 0, 1) });
|
|
43533
43635
|
}
|
|
43534
43636
|
void qc.invalidateQueries({ queryKey: [...dataKeys.alias(portKey, alias), "row"] });
|
|
@@ -43608,9 +43710,10 @@ function useDataRows(alias, query = {}, scope) {
|
|
|
43608
43710
|
const { port, portKey } = useDataPort(scope);
|
|
43609
43711
|
const qc = useRecordsQueryClient();
|
|
43610
43712
|
useLive(alias, scope);
|
|
43713
|
+
const rowsKey = dataKeys.rows(portKey, alias, query);
|
|
43611
43714
|
const q = (0, import_react_query.useInfiniteQuery)(
|
|
43612
43715
|
{
|
|
43613
|
-
queryKey:
|
|
43716
|
+
queryKey: rowsKey,
|
|
43614
43717
|
queryFn: async ({ pageParam }) => unwrap(await port.list(alias, query, pageParam ?? void 0)),
|
|
43615
43718
|
initialPageParam: null,
|
|
43616
43719
|
getNextPageParam: (last) => last.cursor ?? void 0,
|
|
@@ -43630,6 +43733,20 @@ function useDataRows(alias, query = {}, scope) {
|
|
|
43630
43733
|
}
|
|
43631
43734
|
return out;
|
|
43632
43735
|
}, [q.data]);
|
|
43736
|
+
const want = (0, import_react6.useRef)({ key: "", pages: 0 });
|
|
43737
|
+
const keyHash = (0, import_react_query.hashKey)(rowsKey);
|
|
43738
|
+
const loaded = q.data?.pages.length ?? 0;
|
|
43739
|
+
const { isFetching, hasNextPage, isFetchNextPageError, fetchNextPage } = q;
|
|
43740
|
+
(0, import_react6.useEffect)(() => {
|
|
43741
|
+
const w = want.current;
|
|
43742
|
+
if (w.key !== keyHash || isFetching || loaded >= w.pages) return;
|
|
43743
|
+
if (!hasNextPage || isFetchNextPageError) {
|
|
43744
|
+
want.current = { key: keyHash, pages: 0 };
|
|
43745
|
+
owedPages(qc).delete(keyHash);
|
|
43746
|
+
return;
|
|
43747
|
+
}
|
|
43748
|
+
void fetchNextPage({ cancelRefetch: false });
|
|
43749
|
+
}, [qc, keyHash, isFetching, loaded, hasNextPage, isFetchNextPageError, fetchNextPage]);
|
|
43633
43750
|
return {
|
|
43634
43751
|
rows,
|
|
43635
43752
|
total: q.data?.pages[0]?.total ?? null,
|
|
@@ -43638,7 +43755,10 @@ function useDataRows(alias, query = {}, scope) {
|
|
|
43638
43755
|
hasMore: Boolean(q.hasNextPage),
|
|
43639
43756
|
error: asRecordsError(q.error),
|
|
43640
43757
|
loadMore: () => {
|
|
43641
|
-
if (q.hasNextPage
|
|
43758
|
+
if (!q.hasNextPage || q.isFetchingNextPage) return;
|
|
43759
|
+
want.current = { key: keyHash, pages: loaded + 1 };
|
|
43760
|
+
owedPages(qc).set(keyHash, { pages: loaded + 1, fetchNext: () => void q.fetchNextPage({ cancelRefetch: false }) });
|
|
43761
|
+
if (!q.isFetching) void q.fetchNextPage({ cancelRefetch: false });
|
|
43642
43762
|
},
|
|
43643
43763
|
newer: q.data?.newer ?? 0,
|
|
43644
43764
|
refresh: () => void q.refetch()
|
|
@@ -43696,7 +43816,7 @@ function useDataMutations(alias, scope) {
|
|
|
43696
43816
|
if (before) patchRow(qc, portKey, alias, drawPatch(before, patch), { force: true });
|
|
43697
43817
|
const answer = await port.update(alias, id, patch, baseVersion);
|
|
43698
43818
|
if (answer.ok) {
|
|
43699
|
-
patchRow(qc, portKey, alias, answer.data, { force: true });
|
|
43819
|
+
patchRow(qc, portKey, alias, answer.data, { force: true, fromStore: true });
|
|
43700
43820
|
return answer;
|
|
43701
43821
|
}
|
|
43702
43822
|
if (before) patchRow(qc, portKey, alias, before, { force: true });
|