tsdf 8.0.0-beta.4 → 8.0.0-beta.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/dist/main.cjs +153 -120
- package/dist/main.d.ts +32 -30
- package/dist/main.js +153 -120
- package/package.json +2 -1
package/dist/main.cjs
CHANGED
|
@@ -589,7 +589,7 @@ function isObject(value) {
|
|
|
589
589
|
|
|
590
590
|
// src/utils/getCacheId.ts
|
|
591
591
|
function getCacheId(input) {
|
|
592
|
-
return typeof input === "string" ? input : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
|
|
592
|
+
return typeof input === "string" || typeof input === "number" ? String(input) : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
|
|
593
593
|
}
|
|
594
594
|
function orderedProps(obj) {
|
|
595
595
|
return filterAndMap(Object.keys(obj).sort(), (k, ignore2) => {
|
|
@@ -600,12 +600,8 @@ function orderedProps(obj) {
|
|
|
600
600
|
});
|
|
601
601
|
}
|
|
602
602
|
|
|
603
|
-
// src/utils/serializableClone.ts
|
|
604
|
-
function serializableClone(input) {
|
|
605
|
-
return JSON.parse(JSON.stringify(input));
|
|
606
|
-
}
|
|
607
|
-
|
|
608
603
|
// src/collectionStore.ts
|
|
604
|
+
var import_json = require("klona/json");
|
|
609
605
|
function newTSDFCollectionStore({
|
|
610
606
|
debugName,
|
|
611
607
|
fetchFn,
|
|
@@ -628,7 +624,7 @@ function newTSDFCollectionStore({
|
|
|
628
624
|
}
|
|
629
625
|
async function fetch(fetchCtx, itemPayload) {
|
|
630
626
|
const itemId = getItemKey(itemPayload);
|
|
631
|
-
const payload =
|
|
627
|
+
const payload = (0, import_json.klona)(itemPayload);
|
|
632
628
|
const itemState = store.state[itemId];
|
|
633
629
|
store.produceState(
|
|
634
630
|
(draft) => {
|
|
@@ -922,7 +918,7 @@ function newTSDFCollectionStore({
|
|
|
922
918
|
wasLoaded: true,
|
|
923
919
|
refetchOnMount: false,
|
|
924
920
|
error: null,
|
|
925
|
-
payload:
|
|
921
|
+
payload: (0, import_json.klona)(fetchParams)
|
|
926
922
|
};
|
|
927
923
|
},
|
|
928
924
|
{ action: { type: "create-item-state", payload: fetchParams } }
|
|
@@ -994,6 +990,7 @@ function newTSDFCollectionStore({
|
|
|
994
990
|
var import_mitt4 = __toESM(require("mitt"), 1);
|
|
995
991
|
var import_react5 = require("react");
|
|
996
992
|
var import_t_state4 = require("t-state");
|
|
993
|
+
var import_json2 = require("klona/json");
|
|
997
994
|
var noFetchFnError = "No fetchItemFn was provided";
|
|
998
995
|
function newTSDFListQueryStore({
|
|
999
996
|
debugName,
|
|
@@ -1015,8 +1012,11 @@ function newTSDFListQueryStore({
|
|
|
1015
1012
|
function getQueryKey(params) {
|
|
1016
1013
|
return getCacheId(params);
|
|
1017
1014
|
}
|
|
1015
|
+
function getItemKey(params) {
|
|
1016
|
+
return getCacheId(params);
|
|
1017
|
+
}
|
|
1018
1018
|
async function fetchQuery(fetchCtx, [fetchType, queryPayload, size = defaultQuerySize]) {
|
|
1019
|
-
const payload =
|
|
1019
|
+
const payload = (0, import_json2.klona)(queryPayload);
|
|
1020
1020
|
const queryKey = getQueryKey(payload);
|
|
1021
1021
|
const queryState = store.state.queries[queryKey];
|
|
1022
1022
|
const isLoading = !queryState?.wasLoaded;
|
|
@@ -1069,16 +1069,18 @@ function newTSDFListQueryStore({
|
|
|
1069
1069
|
query.wasLoaded = true;
|
|
1070
1070
|
query.hasMore = hasMore;
|
|
1071
1071
|
query.items = [];
|
|
1072
|
-
for (const { data,
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1072
|
+
for (const { data, itemPayload } of items) {
|
|
1073
|
+
const itemKey = getItemKey(itemPayload);
|
|
1074
|
+
draft.items[itemKey] = data;
|
|
1075
|
+
query.items.push(String(itemKey));
|
|
1076
|
+
const itemQuery = draft.itemQueries[itemKey];
|
|
1076
1077
|
if (!itemQuery || itemQuery.status !== "loading" && itemQuery.status !== "refetching") {
|
|
1077
|
-
draft.itemQueries[
|
|
1078
|
+
draft.itemQueries[itemKey] = {
|
|
1078
1079
|
error: null,
|
|
1079
1080
|
refetchOnMount: false,
|
|
1080
1081
|
status: "success",
|
|
1081
|
-
wasLoaded: true
|
|
1082
|
+
wasLoaded: true,
|
|
1083
|
+
payload: itemPayload
|
|
1082
1084
|
};
|
|
1083
1085
|
}
|
|
1084
1086
|
}
|
|
@@ -1087,7 +1089,7 @@ function newTSDFListQueryStore({
|
|
|
1087
1089
|
action: { type: "fetch-query-success", payload }
|
|
1088
1090
|
}
|
|
1089
1091
|
);
|
|
1090
|
-
for (const { id } of items) {
|
|
1092
|
+
for (const { itemPayload: id } of items) {
|
|
1091
1093
|
const itemFetchOrquestrator = fetchItemOrquestrator?.get(String(id));
|
|
1092
1094
|
if (itemFetchOrquestrator) {
|
|
1093
1095
|
itemFetchOrquestrator.setLastFetchStartTime(fetchCtx.getStartTime());
|
|
@@ -1116,15 +1118,16 @@ function newTSDFListQueryStore({
|
|
|
1116
1118
|
function getQueryState(params) {
|
|
1117
1119
|
return store.state.queries[getQueryKey(params)];
|
|
1118
1120
|
}
|
|
1119
|
-
function getItemState(
|
|
1120
|
-
if (typeof
|
|
1121
|
-
|
|
1121
|
+
function getItemState(itemPayload) {
|
|
1122
|
+
if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
|
|
1123
|
+
const itemsId = getItemsKeyArray(itemPayload);
|
|
1124
|
+
return filterAndMap(itemsId, ({ itemKey }, ignore2) => {
|
|
1125
|
+
const item = store.state.items[itemKey];
|
|
1126
|
+
const payload = store.state.itemQueries[itemKey]?.payload;
|
|
1127
|
+
return !item || !payload ? ignore2 : { payload, data: item };
|
|
1128
|
+
});
|
|
1122
1129
|
}
|
|
1123
|
-
|
|
1124
|
-
return filterAndMap(itemsId, (itemId, ignore2) => {
|
|
1125
|
-
const item = store.state.items[itemId];
|
|
1126
|
-
return !item ? ignore2 : { id: itemId, data: item };
|
|
1127
|
-
});
|
|
1130
|
+
return store.state.items[getItemKey(itemPayload)];
|
|
1128
1131
|
}
|
|
1129
1132
|
const fetchQueryOrquestrator = createCollectionFetchOrquestrator({
|
|
1130
1133
|
fetchFn: fetchQuery,
|
|
@@ -1147,9 +1150,9 @@ function newTSDFListQueryStore({
|
|
|
1147
1150
|
return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
|
|
1148
1151
|
}
|
|
1149
1152
|
function getQueryItems(query, itemDataSelector) {
|
|
1150
|
-
return filterAndMap(query.items, (
|
|
1151
|
-
const
|
|
1152
|
-
return
|
|
1153
|
+
return filterAndMap(query.items, (itemKey, ignore2) => {
|
|
1154
|
+
const item = store.state.items[itemKey];
|
|
1155
|
+
return item ? itemDataSelector(item, itemKey) : ignore2;
|
|
1153
1156
|
});
|
|
1154
1157
|
}
|
|
1155
1158
|
async function awaitListQueryFetch(params, size = defaultQuerySize) {
|
|
@@ -1171,24 +1174,25 @@ function newTSDFListQueryStore({
|
|
|
1171
1174
|
};
|
|
1172
1175
|
}
|
|
1173
1176
|
return query.error ? { items: [], error: query.error, hasMore: query.hasMore } : {
|
|
1174
|
-
items: getQueryItems(query, (data,
|
|
1177
|
+
items: getQueryItems(query, (data, itemKey) => ({ data, itemKey })),
|
|
1175
1178
|
error: null,
|
|
1176
1179
|
hasMore: query.hasMore
|
|
1177
1180
|
};
|
|
1178
1181
|
}
|
|
1179
|
-
async function awaitItemFetch(
|
|
1182
|
+
async function awaitItemFetch(itemPayload) {
|
|
1180
1183
|
if (!fetchItemOrquestrator)
|
|
1181
1184
|
throw new Error(noFetchFnError);
|
|
1182
|
-
const
|
|
1185
|
+
const itemKey = getItemKey(itemPayload);
|
|
1186
|
+
const wasAborted = await fetchItemOrquestrator.get(itemKey).awaitFetch(itemPayload);
|
|
1183
1187
|
if (wasAborted) {
|
|
1184
1188
|
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
1185
1189
|
}
|
|
1186
|
-
const
|
|
1187
|
-
const itemQuery = store.state.itemQueries[
|
|
1188
|
-
if (!itemQuery || !
|
|
1190
|
+
const item = store.state.items[itemKey];
|
|
1191
|
+
const itemQuery = store.state.itemQueries[itemKey];
|
|
1192
|
+
if (!itemQuery || !item) {
|
|
1189
1193
|
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
1190
1194
|
}
|
|
1191
|
-
return itemQuery.error ? { data: null, error: itemQuery.error } : { data:
|
|
1195
|
+
return itemQuery.error ? { data: null, error: itemQuery.error } : { data: item, error: null };
|
|
1192
1196
|
}
|
|
1193
1197
|
function getQueriesKeyArray(payloads) {
|
|
1194
1198
|
if (Array.isArray(payloads)) {
|
|
@@ -1229,9 +1233,9 @@ function newTSDFListQueryStore({
|
|
|
1229
1233
|
}
|
|
1230
1234
|
if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
|
|
1231
1235
|
invalidateItem(
|
|
1232
|
-
(
|
|
1236
|
+
(itemPayload) => queriesKey.some(({ payload }) => {
|
|
1233
1237
|
return syncMutationsAndInvalidations.syncItemAndQuery(
|
|
1234
|
-
|
|
1238
|
+
itemPayload,
|
|
1235
1239
|
payload
|
|
1236
1240
|
);
|
|
1237
1241
|
}),
|
|
@@ -1389,20 +1393,22 @@ function newTSDFListQueryStore({
|
|
|
1389
1393
|
});
|
|
1390
1394
|
return useModifyResult(result);
|
|
1391
1395
|
}
|
|
1392
|
-
async function fetchItem(fetchCtx,
|
|
1396
|
+
async function fetchItem(fetchCtx, itemPayload) {
|
|
1393
1397
|
if (!fetchItemOrquestrator) {
|
|
1394
1398
|
throw new Error(noFetchFnError);
|
|
1395
1399
|
}
|
|
1396
|
-
const
|
|
1400
|
+
const itemKey = getCacheId(itemPayload);
|
|
1401
|
+
const isLoaded = !store.state.itemQueries[itemKey]?.wasLoaded;
|
|
1397
1402
|
store.produceState(
|
|
1398
1403
|
(draft) => {
|
|
1399
|
-
const itemQuery = draft.itemQueries[
|
|
1404
|
+
const itemQuery = draft.itemQueries[itemKey];
|
|
1400
1405
|
if (!itemQuery) {
|
|
1401
|
-
draft.itemQueries[
|
|
1406
|
+
draft.itemQueries[itemKey] = {
|
|
1402
1407
|
status: "loading",
|
|
1403
1408
|
error: null,
|
|
1404
1409
|
wasLoaded: false,
|
|
1405
|
-
refetchOnMount: false
|
|
1410
|
+
refetchOnMount: false,
|
|
1411
|
+
payload: (0, import_json2.klona)(itemPayload)
|
|
1406
1412
|
};
|
|
1407
1413
|
return;
|
|
1408
1414
|
}
|
|
@@ -1413,25 +1419,25 @@ function newTSDFListQueryStore({
|
|
|
1413
1419
|
{
|
|
1414
1420
|
action: {
|
|
1415
1421
|
type: isLoaded ? "fetch-item-start" : "fetch-item-refetch-start",
|
|
1416
|
-
|
|
1422
|
+
itemPayload
|
|
1417
1423
|
}
|
|
1418
1424
|
}
|
|
1419
1425
|
);
|
|
1420
1426
|
try {
|
|
1421
|
-
const item = await fetchItemFn(
|
|
1427
|
+
const item = await fetchItemFn(itemPayload);
|
|
1422
1428
|
if (fetchCtx.shouldAbort())
|
|
1423
1429
|
return false;
|
|
1424
1430
|
store.produceState(
|
|
1425
1431
|
(draft) => {
|
|
1426
|
-
const itemQuery = draft.itemQueries[
|
|
1432
|
+
const itemQuery = draft.itemQueries[itemKey];
|
|
1427
1433
|
if (!itemQuery)
|
|
1428
1434
|
return;
|
|
1429
1435
|
itemQuery.status = "success";
|
|
1430
1436
|
itemQuery.wasLoaded = true;
|
|
1431
|
-
draft.items[
|
|
1437
|
+
draft.items[itemKey] = item;
|
|
1432
1438
|
},
|
|
1433
1439
|
{
|
|
1434
|
-
action: { type: "fetch-item-success",
|
|
1440
|
+
action: { type: "fetch-item-success", itemPayload }
|
|
1435
1441
|
}
|
|
1436
1442
|
);
|
|
1437
1443
|
return true;
|
|
@@ -1441,14 +1447,14 @@ function newTSDFListQueryStore({
|
|
|
1441
1447
|
const error = errorNormalizer(exception);
|
|
1442
1448
|
store.produceState(
|
|
1443
1449
|
(draft) => {
|
|
1444
|
-
const itemQuery = draft.itemQueries[
|
|
1450
|
+
const itemQuery = draft.itemQueries[itemKey];
|
|
1445
1451
|
if (!itemQuery)
|
|
1446
1452
|
return;
|
|
1447
1453
|
itemQuery.status = "error";
|
|
1448
1454
|
itemQuery.error = error;
|
|
1449
1455
|
},
|
|
1450
1456
|
{
|
|
1451
|
-
action: { type: "fetch-item-error",
|
|
1457
|
+
action: { type: "fetch-item-error", itemPayload }
|
|
1452
1458
|
}
|
|
1453
1459
|
);
|
|
1454
1460
|
return false;
|
|
@@ -1460,56 +1466,61 @@ function newTSDFListQueryStore({
|
|
|
1460
1466
|
dynamicRealtimeThrottleMs,
|
|
1461
1467
|
mediumPriorityThrottleMs
|
|
1462
1468
|
});
|
|
1463
|
-
function scheduleItemFetch(fetchType,
|
|
1469
|
+
function scheduleItemFetch(fetchType, itemPayload) {
|
|
1464
1470
|
if (!fetchItemOrquestrator) {
|
|
1465
1471
|
throw new Error(noFetchFnError);
|
|
1466
1472
|
}
|
|
1467
|
-
const fetchMultiple = Array.isArray(
|
|
1468
|
-
const itemsId = fetchMultiple ?
|
|
1469
|
-
const results = itemsId.map((
|
|
1470
|
-
|
|
1473
|
+
const fetchMultiple = Array.isArray(itemPayload);
|
|
1474
|
+
const itemsId = fetchMultiple ? itemPayload : [itemPayload];
|
|
1475
|
+
const results = itemsId.map((payload) => {
|
|
1476
|
+
const itemKey = getCacheId(payload);
|
|
1477
|
+
return fetchItemOrquestrator.get(itemKey).scheduleFetch(fetchType, payload);
|
|
1471
1478
|
});
|
|
1472
1479
|
return fetchMultiple ? results : results[0];
|
|
1473
1480
|
}
|
|
1474
|
-
function
|
|
1475
|
-
if (Array.isArray(
|
|
1476
|
-
return
|
|
1481
|
+
function getItemsKeyArray(itemsPayload) {
|
|
1482
|
+
if (Array.isArray(itemsPayload)) {
|
|
1483
|
+
return itemsPayload.map((payload) => ({
|
|
1484
|
+
itemKey: getItemKey(payload),
|
|
1485
|
+
payload
|
|
1486
|
+
}));
|
|
1477
1487
|
}
|
|
1478
|
-
if (typeof
|
|
1488
|
+
if (typeof itemsPayload === "function") {
|
|
1479
1489
|
return filterAndMap(
|
|
1480
1490
|
Object.entries(store.state.items),
|
|
1481
|
-
([
|
|
1482
|
-
|
|
1491
|
+
([itemKey, item], ignore2) => {
|
|
1492
|
+
const payload = store.state.itemQueries[itemKey]?.payload;
|
|
1493
|
+
if (item === null || !payload)
|
|
1483
1494
|
return ignore2;
|
|
1484
|
-
return
|
|
1495
|
+
return itemsPayload(payload, item) ? { itemKey, payload } : ignore2;
|
|
1485
1496
|
}
|
|
1486
1497
|
);
|
|
1487
1498
|
}
|
|
1488
|
-
return [
|
|
1499
|
+
return [{ payload: itemsPayload, itemKey: getItemKey(itemsPayload) }];
|
|
1489
1500
|
}
|
|
1490
1501
|
const itemInvalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
1491
1502
|
function invalidateItem(itemId, priority = "highPriority", ignoreInvalidationSync = false) {
|
|
1492
1503
|
if (!fetchItemOrquestrator) {
|
|
1493
1504
|
throw new Error(noFetchFnError);
|
|
1494
1505
|
}
|
|
1495
|
-
const
|
|
1496
|
-
for (const
|
|
1506
|
+
const itemsKey = getItemsKeyArray(itemId);
|
|
1507
|
+
for (const { itemKey } of itemsKey) {
|
|
1497
1508
|
store.produceState(
|
|
1498
1509
|
(draft) => {
|
|
1499
|
-
const query = draft.itemQueries[
|
|
1510
|
+
const query = draft.itemQueries[itemKey];
|
|
1500
1511
|
if (!query)
|
|
1501
1512
|
return;
|
|
1502
1513
|
query.refetchOnMount = priority;
|
|
1503
1514
|
},
|
|
1504
|
-
{ action: { type: "invalidate-item", queryKey:
|
|
1515
|
+
{ action: { type: "invalidate-item", queryKey: itemKey } }
|
|
1505
1516
|
);
|
|
1506
|
-
itemInvalidationWasTriggered.delete(
|
|
1507
|
-
storeEvents.emit("invalidateItem", { priority,
|
|
1517
|
+
itemInvalidationWasTriggered.delete(itemKey);
|
|
1518
|
+
storeEvents.emit("invalidateItem", { priority, itemKey });
|
|
1508
1519
|
}
|
|
1509
1520
|
if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
|
|
1510
1521
|
invalidateQuery(
|
|
1511
|
-
(query) =>
|
|
1512
|
-
(
|
|
1522
|
+
(query) => itemsKey.some(
|
|
1523
|
+
({ payload }) => syncMutationsAndInvalidations.syncItemAndQuery(payload, query)
|
|
1513
1524
|
),
|
|
1514
1525
|
priority,
|
|
1515
1526
|
true
|
|
@@ -1519,13 +1530,16 @@ function newTSDFListQueryStore({
|
|
|
1519
1530
|
function startItemMutation(itemId) {
|
|
1520
1531
|
if (!fetchItemOrquestrator)
|
|
1521
1532
|
throw new Error(noFetchFnError);
|
|
1522
|
-
const
|
|
1533
|
+
const itemsKey = getItemsKeyArray(itemId);
|
|
1523
1534
|
const endMutations = [];
|
|
1524
|
-
for (const
|
|
1525
|
-
endMutations.push(fetchItemOrquestrator.get(
|
|
1535
|
+
for (const { itemKey, payload } of itemsKey) {
|
|
1536
|
+
endMutations.push(fetchItemOrquestrator.get(itemKey).startMutation());
|
|
1526
1537
|
if (syncMutationsAndInvalidations) {
|
|
1527
1538
|
for (const [queryKey, query] of Object.entries(store.state.queries)) {
|
|
1528
|
-
if (syncMutationsAndInvalidations.syncItemAndQuery(
|
|
1539
|
+
if (syncMutationsAndInvalidations.syncItemAndQuery(
|
|
1540
|
+
payload,
|
|
1541
|
+
query.payload
|
|
1542
|
+
)) {
|
|
1529
1543
|
endMutations.push(
|
|
1530
1544
|
fetchQueryOrquestrator.get(queryKey).startMutation()
|
|
1531
1545
|
);
|
|
@@ -1542,27 +1556,38 @@ function newTSDFListQueryStore({
|
|
|
1542
1556
|
function defaultItemDataSelector(data) {
|
|
1543
1557
|
return data;
|
|
1544
1558
|
}
|
|
1545
|
-
function useMultipleItems(
|
|
1559
|
+
function useMultipleItems(itemsPayload_, {
|
|
1546
1560
|
selector = defaultItemDataSelector,
|
|
1547
1561
|
returnIdleStatus,
|
|
1548
1562
|
returnRefetchingStatus,
|
|
1549
1563
|
disableRefetchOnMount
|
|
1550
1564
|
} = {}) {
|
|
1551
1565
|
const dataSelector = (0, import_react5.useCallback)(selector, []);
|
|
1552
|
-
const
|
|
1566
|
+
const memoizedItemKeys = useDeepMemo(
|
|
1567
|
+
() => itemsPayload_.map(
|
|
1568
|
+
(itemPayload) => ({
|
|
1569
|
+
payload: itemPayload,
|
|
1570
|
+
itemKey: getItemKey(itemPayload)
|
|
1571
|
+
})
|
|
1572
|
+
),
|
|
1573
|
+
[itemsPayload_]
|
|
1574
|
+
);
|
|
1553
1575
|
const resultSelector = (0, import_react5.useCallback)(
|
|
1554
1576
|
(state) => {
|
|
1555
|
-
return
|
|
1556
|
-
(
|
|
1557
|
-
|
|
1558
|
-
|
|
1577
|
+
return memoizedItemKeys.map(
|
|
1578
|
+
({
|
|
1579
|
+
itemKey,
|
|
1580
|
+
payload
|
|
1581
|
+
}) => {
|
|
1582
|
+
const itemQuery = state.itemQueries[itemKey];
|
|
1583
|
+
const itemState = state.items[itemKey];
|
|
1559
1584
|
if (itemQuery === null) {
|
|
1560
1585
|
return {
|
|
1561
1586
|
status: "deleted",
|
|
1562
1587
|
error: null,
|
|
1563
1588
|
isLoading: false,
|
|
1564
|
-
|
|
1565
|
-
data: dataSelector(null,
|
|
1589
|
+
payload,
|
|
1590
|
+
data: dataSelector(null, itemKey)
|
|
1566
1591
|
};
|
|
1567
1592
|
}
|
|
1568
1593
|
if (!itemQuery) {
|
|
@@ -1570,8 +1595,8 @@ function newTSDFListQueryStore({
|
|
|
1570
1595
|
status: returnIdleStatus ? "idle" : "loading",
|
|
1571
1596
|
error: null,
|
|
1572
1597
|
isLoading: returnIdleStatus ? false : true,
|
|
1573
|
-
|
|
1574
|
-
data: dataSelector(null,
|
|
1598
|
+
payload,
|
|
1599
|
+
data: dataSelector(null, itemKey)
|
|
1575
1600
|
};
|
|
1576
1601
|
}
|
|
1577
1602
|
let status = itemQuery.status;
|
|
@@ -1582,54 +1607,59 @@ function newTSDFListQueryStore({
|
|
|
1582
1607
|
status,
|
|
1583
1608
|
error: itemQuery.error,
|
|
1584
1609
|
isLoading: status === "loading",
|
|
1585
|
-
data: dataSelector(itemState ?? null,
|
|
1586
|
-
|
|
1610
|
+
data: dataSelector(itemState ?? null, itemKey),
|
|
1611
|
+
payload
|
|
1587
1612
|
};
|
|
1588
1613
|
}
|
|
1589
1614
|
);
|
|
1590
1615
|
},
|
|
1591
|
-
[
|
|
1616
|
+
[
|
|
1617
|
+
dataSelector,
|
|
1618
|
+
memoizedItemKeys,
|
|
1619
|
+
returnIdleStatus,
|
|
1620
|
+
returnRefetchingStatus
|
|
1621
|
+
]
|
|
1592
1622
|
);
|
|
1593
1623
|
const storeState = store.useSelector(resultSelector, {
|
|
1594
1624
|
equalityFn: import_t_state4.deepEqual,
|
|
1595
1625
|
useExternalDeps: true
|
|
1596
1626
|
});
|
|
1597
1627
|
useOnMittEvent(storeEvents, "invalidateItem", (event) => {
|
|
1598
|
-
for (const
|
|
1599
|
-
if (
|
|
1628
|
+
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
1629
|
+
if (itemKey !== event.itemKey)
|
|
1600
1630
|
continue;
|
|
1601
|
-
if (!itemInvalidationWasTriggered.has(
|
|
1602
|
-
scheduleItemFetch(event.priority,
|
|
1603
|
-
itemInvalidationWasTriggered.add(
|
|
1631
|
+
if (!itemInvalidationWasTriggered.has(itemKey)) {
|
|
1632
|
+
scheduleItemFetch(event.priority, payload);
|
|
1633
|
+
itemInvalidationWasTriggered.add(itemKey);
|
|
1604
1634
|
}
|
|
1605
1635
|
}
|
|
1606
1636
|
});
|
|
1607
1637
|
(0, import_react5.useEffect)(() => {
|
|
1608
|
-
for (const
|
|
1609
|
-
if (
|
|
1638
|
+
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
1639
|
+
if (itemKey) {
|
|
1610
1640
|
if (disableRefetchOnMount) {
|
|
1611
|
-
const itemState = store.state.itemQueries[
|
|
1641
|
+
const itemState = store.state.itemQueries[itemKey];
|
|
1612
1642
|
const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
|
|
1613
1643
|
if (shouldFetch) {
|
|
1614
1644
|
scheduleItemFetch(
|
|
1615
1645
|
itemState?.refetchOnMount || "lowPriority",
|
|
1616
|
-
|
|
1646
|
+
payload
|
|
1617
1647
|
);
|
|
1618
1648
|
return;
|
|
1619
1649
|
}
|
|
1620
1650
|
} else {
|
|
1621
|
-
scheduleItemFetch("lowPriority",
|
|
1651
|
+
scheduleItemFetch("lowPriority", payload);
|
|
1622
1652
|
}
|
|
1623
1653
|
}
|
|
1624
1654
|
}
|
|
1625
|
-
}, [disableRefetchOnMount,
|
|
1655
|
+
}, [disableRefetchOnMount, memoizedItemKeys]);
|
|
1626
1656
|
return storeState;
|
|
1627
1657
|
}
|
|
1628
|
-
function useItem(
|
|
1658
|
+
function useItem(itemPayload, options = {}) {
|
|
1629
1659
|
const { ensureIsLoaded } = options;
|
|
1630
1660
|
const query = (0, import_react5.useMemo)(
|
|
1631
|
-
() =>
|
|
1632
|
-
[
|
|
1661
|
+
() => itemPayload === false || itemPayload === null || itemPayload === void 0 ? [] : [itemPayload],
|
|
1662
|
+
[itemPayload]
|
|
1633
1663
|
);
|
|
1634
1664
|
const queryResult = useMultipleItems(query, options);
|
|
1635
1665
|
const memoizedSelector = (0, import_react5.useCallback)(
|
|
@@ -1641,24 +1671,24 @@ function newTSDFListQueryStore({
|
|
|
1641
1671
|
error: null,
|
|
1642
1672
|
isLoading: false,
|
|
1643
1673
|
status: "idle",
|
|
1644
|
-
data: memoizedSelector(null,
|
|
1645
|
-
|
|
1674
|
+
data: memoizedSelector(null, ""),
|
|
1675
|
+
payload: itemPayload || null
|
|
1646
1676
|
},
|
|
1647
|
-
[
|
|
1677
|
+
[itemPayload, memoizedSelector, queryResult]
|
|
1648
1678
|
);
|
|
1649
1679
|
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
1650
1680
|
ensureIsLoaded,
|
|
1651
|
-
!!
|
|
1681
|
+
!!itemPayload,
|
|
1652
1682
|
() => {
|
|
1653
|
-
if (
|
|
1654
|
-
scheduleItemFetch("highPriority",
|
|
1683
|
+
if (itemPayload) {
|
|
1684
|
+
scheduleItemFetch("highPriority", itemPayload);
|
|
1655
1685
|
}
|
|
1656
1686
|
}
|
|
1657
1687
|
);
|
|
1658
1688
|
(0, import_t_state4.useSubscribeToStore)(store, ({ observe }) => {
|
|
1659
|
-
if (!ensureIsLoaded || !
|
|
1689
|
+
if (!ensureIsLoaded || !itemPayload)
|
|
1660
1690
|
return;
|
|
1661
|
-
observe.ifSelector((state) => state.itemQueries[
|
|
1691
|
+
observe.ifSelector((state) => state.itemQueries[itemPayload]?.status).change.then(({ current }) => {
|
|
1662
1692
|
if (current === "success" || current === "error") {
|
|
1663
1693
|
emitIsLoadedEvt("isLoaded", true);
|
|
1664
1694
|
}
|
|
@@ -1667,19 +1697,19 @@ function newTSDFListQueryStore({
|
|
|
1667
1697
|
return useModifyResult(result);
|
|
1668
1698
|
}
|
|
1669
1699
|
function updateItemState(itemIds, produceNewData, ifNothingWasUpdated) {
|
|
1670
|
-
const itemKeys =
|
|
1700
|
+
const itemKeys = getItemsKeyArray(itemIds);
|
|
1671
1701
|
let someItemWasUpdated = false;
|
|
1672
1702
|
store.batch(
|
|
1673
1703
|
() => {
|
|
1674
1704
|
store.produceState((draftState) => {
|
|
1675
|
-
for (const
|
|
1676
|
-
const item = draftState.items[
|
|
1705
|
+
for (const { itemKey, payload } of itemKeys) {
|
|
1706
|
+
const item = draftState.items[itemKey];
|
|
1677
1707
|
if (!item)
|
|
1678
1708
|
continue;
|
|
1679
1709
|
someItemWasUpdated = true;
|
|
1680
|
-
const newData = produceNewData(item,
|
|
1710
|
+
const newData = produceNewData(item, payload);
|
|
1681
1711
|
if (newData) {
|
|
1682
|
-
draftState.items[
|
|
1712
|
+
draftState.items[itemKey] = newData;
|
|
1683
1713
|
}
|
|
1684
1714
|
}
|
|
1685
1715
|
});
|
|
@@ -1691,25 +1721,27 @@ function newTSDFListQueryStore({
|
|
|
1691
1721
|
);
|
|
1692
1722
|
return someItemWasUpdated;
|
|
1693
1723
|
}
|
|
1694
|
-
function addItemToState(
|
|
1724
|
+
function addItemToState(itemPayload, data) {
|
|
1725
|
+
const itemKey = getItemKey(itemPayload);
|
|
1695
1726
|
store.produceState(
|
|
1696
1727
|
(draftState) => {
|
|
1697
|
-
draftState.items[
|
|
1698
|
-
draftState.itemQueries[
|
|
1728
|
+
draftState.items[itemKey] = data;
|
|
1729
|
+
draftState.itemQueries[itemKey] = {
|
|
1699
1730
|
status: "success",
|
|
1700
1731
|
wasLoaded: true,
|
|
1701
1732
|
refetchOnMount: false,
|
|
1702
|
-
error: null
|
|
1733
|
+
error: null,
|
|
1734
|
+
payload: (0, import_json2.klona)(itemPayload)
|
|
1703
1735
|
};
|
|
1704
1736
|
},
|
|
1705
|
-
{ action: { type: "create-item-state",
|
|
1737
|
+
{ action: { type: "create-item-state", itemPayload } }
|
|
1706
1738
|
);
|
|
1707
1739
|
}
|
|
1708
1740
|
function deleteItemState(itemId) {
|
|
1709
|
-
const itemsId =
|
|
1741
|
+
const itemsId = getItemsKeyArray(itemId);
|
|
1710
1742
|
store.produceState(
|
|
1711
1743
|
(draftState) => {
|
|
1712
|
-
for (const itemKey of itemsId) {
|
|
1744
|
+
for (const { itemKey } of itemsId) {
|
|
1713
1745
|
draftState.items[itemKey] = null;
|
|
1714
1746
|
draftState.itemQueries[itemKey] = null;
|
|
1715
1747
|
for (const query of Object.values(draftState.queries)) {
|
|
@@ -1746,6 +1778,7 @@ function newTSDFListQueryStore({
|
|
|
1746
1778
|
awaitListQueryFetch,
|
|
1747
1779
|
useMultipleListQueries,
|
|
1748
1780
|
useListQuery,
|
|
1781
|
+
getItemKey,
|
|
1749
1782
|
useItem,
|
|
1750
1783
|
useMultipleItems,
|
|
1751
1784
|
addItemToState,
|
package/dist/main.d.ts
CHANGED
|
@@ -132,16 +132,17 @@ type TSFDListQuery<NError, QueryPayload extends ValidPayload> = {
|
|
|
132
132
|
refetchOnMount: false | FetchType;
|
|
133
133
|
items: string[];
|
|
134
134
|
};
|
|
135
|
-
type TSDFItemQuery<NError> = {
|
|
135
|
+
type TSDFItemQuery<NError, ItemPayload> = {
|
|
136
136
|
error: NError | null;
|
|
137
137
|
status: Exclude<QueryStatus, 'loadingMore'>;
|
|
138
138
|
wasLoaded: boolean;
|
|
139
139
|
refetchOnMount: false | FetchType;
|
|
140
|
+
payload: ItemPayload;
|
|
140
141
|
};
|
|
141
|
-
type TSFDListQueryState<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload> = {
|
|
142
|
+
type TSFDListQueryState<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = {
|
|
142
143
|
items: Record<string, ItemState | null>;
|
|
143
144
|
queries: Record<string, TSFDListQuery<NError, QueryPayload>>;
|
|
144
|
-
itemQueries: Record<string, TSDFItemQuery<NError> | null>;
|
|
145
|
+
itemQueries: Record<string, TSDFItemQuery<NError, ItemPayload> | null>;
|
|
145
146
|
};
|
|
146
147
|
type TSFDUseListQueryReturn<Selected, ItemPayload, NError> = {
|
|
147
148
|
items: Selected[];
|
|
@@ -152,59 +153,59 @@ type TSFDUseListQueryReturn<Selected, ItemPayload, NError> = {
|
|
|
152
153
|
hasMore: boolean;
|
|
153
154
|
isLoading: boolean;
|
|
154
155
|
};
|
|
155
|
-
type
|
|
156
|
+
type TSFDUseListItemReturn<Selected, NError, ItemPayload> = {
|
|
156
157
|
data: Selected;
|
|
157
158
|
status: QueryStatus | 'idle' | 'deleted';
|
|
158
|
-
|
|
159
|
+
payload: ItemPayload | null;
|
|
159
160
|
error: NError | null;
|
|
160
161
|
isLoading: boolean;
|
|
161
162
|
};
|
|
162
|
-
type FetchListFnReturn<ItemState extends ValidStoreState> = {
|
|
163
|
+
type FetchListFnReturn<ItemState extends ValidStoreState, ItemPayload extends ValidPayload> = {
|
|
163
164
|
items: {
|
|
164
|
-
|
|
165
|
+
itemPayload: ItemPayload;
|
|
165
166
|
data: ItemState;
|
|
166
167
|
}[];
|
|
167
168
|
hasMore: boolean;
|
|
168
169
|
};
|
|
169
|
-
declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload>({ debugName, fetchListFn, fetchItemFn, errorNormalizer, defaultQuerySize, disableRefetchOnWindowFocus, disableRefetchOnMount: globalDisableRefetchOnMount, lowPriorityThrottleMs, mediumPriorityThrottleMs, dynamicRealtimeThrottleMs, syncMutationsAndInvalidations, }: {
|
|
170
|
+
declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload>({ debugName, fetchListFn, fetchItemFn, errorNormalizer, defaultQuerySize, disableRefetchOnWindowFocus, disableRefetchOnMount: globalDisableRefetchOnMount, lowPriorityThrottleMs, mediumPriorityThrottleMs, dynamicRealtimeThrottleMs, syncMutationsAndInvalidations, }: {
|
|
170
171
|
debugName?: string;
|
|
171
|
-
fetchListFn: (payload: QueryPayload, size: number) => Promise<FetchListFnReturn<ItemState>>;
|
|
172
|
-
fetchItemFn?: (itemId:
|
|
172
|
+
fetchListFn: (payload: QueryPayload, size: number) => Promise<FetchListFnReturn<ItemState, ItemPayload>>;
|
|
173
|
+
fetchItemFn?: (itemId: ItemPayload) => Promise<ItemState>;
|
|
173
174
|
errorNormalizer: (exception: unknown) => NError;
|
|
174
175
|
defaultQuerySize?: number;
|
|
175
176
|
disableRefetchOnWindowFocus?: boolean;
|
|
176
177
|
syncMutationsAndInvalidations?: {
|
|
177
178
|
syncQueries: (query1: QueryPayload, query2: QueryPayload) => boolean;
|
|
178
|
-
syncItemAndQuery: (
|
|
179
|
+
syncItemAndQuery: (itemPayload: ItemPayload, query: QueryPayload) => boolean;
|
|
179
180
|
};
|
|
180
181
|
disableRefetchOnMount?: boolean;
|
|
181
182
|
lowPriorityThrottleMs?: number;
|
|
182
183
|
mediumPriorityThrottleMs?: number;
|
|
183
184
|
dynamicRealtimeThrottleMs?: (lastFetchDuration: number) => number;
|
|
184
185
|
}): {
|
|
185
|
-
store: Store<TSFDListQueryState<ItemState, NError, QueryPayload>>;
|
|
186
|
+
store: Store<TSFDListQueryState<ItemState, NError, QueryPayload, ItemPayload>>;
|
|
186
187
|
scheduleListQueryFetch: {
|
|
187
188
|
(fetchType: FetchType, payload: QueryPayload, size?: number): ScheduleFetchResults;
|
|
188
189
|
(fetchType: FetchType, payload: QueryPayload[], size?: number): ScheduleFetchResults[];
|
|
189
190
|
};
|
|
190
191
|
getQueryState: (params: QueryPayload) => TSFDListQuery<NError, QueryPayload> | undefined;
|
|
191
192
|
getQueryKey: (params: QueryPayload) => string;
|
|
192
|
-
deleteItemState: (itemId:
|
|
193
|
+
deleteItemState: (itemId: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[]) => void;
|
|
193
194
|
getItemState: {
|
|
194
|
-
(
|
|
195
|
-
|
|
195
|
+
(itemPayload: ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[]): {
|
|
196
|
+
payload: ItemPayload;
|
|
196
197
|
data: ItemState;
|
|
197
198
|
}[];
|
|
198
|
-
(
|
|
199
|
+
(itemPayload: ItemPayload): ItemState | null | undefined;
|
|
199
200
|
};
|
|
200
201
|
loadMore: (params: QueryPayload, size: number) => ScheduleFetchResults;
|
|
201
202
|
invalidateQuery: (queryPayload: QueryPayload | QueryPayload[] | ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean), priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
|
|
202
|
-
invalidateItem: (itemId:
|
|
203
|
+
invalidateItem: (itemId: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[], priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
|
|
203
204
|
scheduleItemFetch: {
|
|
204
|
-
(fetchType: FetchType,
|
|
205
|
-
(fetchType: FetchType,
|
|
205
|
+
(fetchType: FetchType, itemPayload: ItemPayload): ScheduleFetchResults;
|
|
206
|
+
(fetchType: FetchType, itemPayload: ItemPayload[]): ScheduleFetchResults[];
|
|
206
207
|
};
|
|
207
|
-
awaitItemFetch: (
|
|
208
|
+
awaitItemFetch: (itemPayload: ItemPayload) => Promise<{
|
|
208
209
|
data: null;
|
|
209
210
|
error: NError;
|
|
210
211
|
} | {
|
|
@@ -219,7 +220,7 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
|
|
|
219
220
|
} | {
|
|
220
221
|
items: {
|
|
221
222
|
data: ItemState;
|
|
222
|
-
|
|
223
|
+
itemKey: string;
|
|
223
224
|
}[];
|
|
224
225
|
error: null;
|
|
225
226
|
hasMore: boolean;
|
|
@@ -247,23 +248,24 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
|
|
|
247
248
|
ensureIsLoaded?: boolean | undefined;
|
|
248
249
|
loadSize?: number | undefined;
|
|
249
250
|
}) => TSFDUseListQueryReturn<SelectedItem_1, QueryPayload, NError>;
|
|
250
|
-
|
|
251
|
+
getItemKey: (params: ItemPayload) => string;
|
|
252
|
+
useItem: <SelectedItem_2 = ItemState | null>(itemPayload: ItemPayload | false | null | undefined, options?: {
|
|
251
253
|
selector?: ((data: ItemState | null, id: string) => SelectedItem_2) | undefined;
|
|
252
254
|
disableRefetchOnMount?: boolean | undefined;
|
|
253
255
|
returnIdleStatus?: boolean | undefined;
|
|
254
256
|
returnRefetchingStatus?: boolean | undefined;
|
|
255
257
|
ensureIsLoaded?: boolean | undefined;
|
|
256
|
-
}) =>
|
|
257
|
-
useMultipleItems: <SelectedItem_3 = ItemState>(
|
|
258
|
+
}) => TSFDUseListItemReturn<SelectedItem_2, NError, ItemPayload>;
|
|
259
|
+
useMultipleItems: <SelectedItem_3 = ItemState | null>(itemsPayload_: ItemPayload[], { selector, returnIdleStatus, returnRefetchingStatus, disableRefetchOnMount, }?: {
|
|
258
260
|
selector?: ((data: ItemState | null, id: string) => SelectedItem_3) | undefined;
|
|
259
261
|
disableRefetchOnMount?: boolean | undefined;
|
|
260
262
|
returnIdleStatus?: boolean | undefined;
|
|
261
263
|
returnRefetchingStatus?: boolean | undefined;
|
|
262
|
-
}) => readonly
|
|
263
|
-
addItemToState: (
|
|
264
|
-
updateItemState: (itemIds:
|
|
265
|
-
startItemMutation: (itemId:
|
|
264
|
+
}) => readonly TSFDUseListItemReturn<SelectedItem_3, NError, ItemPayload>[];
|
|
265
|
+
addItemToState: (itemPayload: ItemPayload, data: ItemState) => void;
|
|
266
|
+
updateItemState: (itemIds: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[], produceNewData: (draftData: ItemState, itemPayload: ItemPayload) => void | ItemState, ifNothingWasUpdated?: () => void) => boolean;
|
|
267
|
+
startItemMutation: (itemId: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[]) => () => void;
|
|
266
268
|
};
|
|
267
|
-
type TSFDListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload>>;
|
|
269
|
+
type TSFDListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload, ItemPayload>>;
|
|
268
270
|
|
|
269
|
-
export { FetchListFnReturn, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn,
|
|
271
|
+
export { FetchListFnReturn, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn, TSFDUseListItemReturn, TSFDUseListQueryReturn, ValidPayload, ValidStoreState, newTSDFCollectionStore, newTSDFDocumentStore, newTSDFListQueryStore };
|
package/dist/main.js
CHANGED
|
@@ -555,7 +555,7 @@ function isObject(value) {
|
|
|
555
555
|
|
|
556
556
|
// src/utils/getCacheId.ts
|
|
557
557
|
function getCacheId(input) {
|
|
558
|
-
return typeof input === "string" ? input : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
|
|
558
|
+
return typeof input === "string" || typeof input === "number" ? String(input) : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
|
|
559
559
|
}
|
|
560
560
|
function orderedProps(obj) {
|
|
561
561
|
return filterAndMap(Object.keys(obj).sort(), (k, ignore2) => {
|
|
@@ -566,12 +566,8 @@ function orderedProps(obj) {
|
|
|
566
566
|
});
|
|
567
567
|
}
|
|
568
568
|
|
|
569
|
-
// src/utils/serializableClone.ts
|
|
570
|
-
function serializableClone(input) {
|
|
571
|
-
return JSON.parse(JSON.stringify(input));
|
|
572
|
-
}
|
|
573
|
-
|
|
574
569
|
// src/collectionStore.ts
|
|
570
|
+
import { klona } from "klona/json";
|
|
575
571
|
function newTSDFCollectionStore({
|
|
576
572
|
debugName,
|
|
577
573
|
fetchFn,
|
|
@@ -594,7 +590,7 @@ function newTSDFCollectionStore({
|
|
|
594
590
|
}
|
|
595
591
|
async function fetch(fetchCtx, itemPayload) {
|
|
596
592
|
const itemId = getItemKey(itemPayload);
|
|
597
|
-
const payload =
|
|
593
|
+
const payload = klona(itemPayload);
|
|
598
594
|
const itemState = store.state[itemId];
|
|
599
595
|
store.produceState(
|
|
600
596
|
(draft) => {
|
|
@@ -888,7 +884,7 @@ function newTSDFCollectionStore({
|
|
|
888
884
|
wasLoaded: true,
|
|
889
885
|
refetchOnMount: false,
|
|
890
886
|
error: null,
|
|
891
|
-
payload:
|
|
887
|
+
payload: klona(fetchParams)
|
|
892
888
|
};
|
|
893
889
|
},
|
|
894
890
|
{ action: { type: "create-item-state", payload: fetchParams } }
|
|
@@ -960,6 +956,7 @@ function newTSDFCollectionStore({
|
|
|
960
956
|
import mitt4 from "mitt";
|
|
961
957
|
import { useCallback as useCallback3, useEffect as useEffect4, useMemo as useMemo4 } from "react";
|
|
962
958
|
import { deepEqual as deepEqual3, Store as Store3, useSubscribeToStore as useSubscribeToStore3 } from "t-state";
|
|
959
|
+
import { klona as klona2 } from "klona/json";
|
|
963
960
|
var noFetchFnError = "No fetchItemFn was provided";
|
|
964
961
|
function newTSDFListQueryStore({
|
|
965
962
|
debugName,
|
|
@@ -981,8 +978,11 @@ function newTSDFListQueryStore({
|
|
|
981
978
|
function getQueryKey(params) {
|
|
982
979
|
return getCacheId(params);
|
|
983
980
|
}
|
|
981
|
+
function getItemKey(params) {
|
|
982
|
+
return getCacheId(params);
|
|
983
|
+
}
|
|
984
984
|
async function fetchQuery(fetchCtx, [fetchType, queryPayload, size = defaultQuerySize]) {
|
|
985
|
-
const payload =
|
|
985
|
+
const payload = klona2(queryPayload);
|
|
986
986
|
const queryKey = getQueryKey(payload);
|
|
987
987
|
const queryState = store.state.queries[queryKey];
|
|
988
988
|
const isLoading = !queryState?.wasLoaded;
|
|
@@ -1035,16 +1035,18 @@ function newTSDFListQueryStore({
|
|
|
1035
1035
|
query.wasLoaded = true;
|
|
1036
1036
|
query.hasMore = hasMore;
|
|
1037
1037
|
query.items = [];
|
|
1038
|
-
for (const { data,
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1038
|
+
for (const { data, itemPayload } of items) {
|
|
1039
|
+
const itemKey = getItemKey(itemPayload);
|
|
1040
|
+
draft.items[itemKey] = data;
|
|
1041
|
+
query.items.push(String(itemKey));
|
|
1042
|
+
const itemQuery = draft.itemQueries[itemKey];
|
|
1042
1043
|
if (!itemQuery || itemQuery.status !== "loading" && itemQuery.status !== "refetching") {
|
|
1043
|
-
draft.itemQueries[
|
|
1044
|
+
draft.itemQueries[itemKey] = {
|
|
1044
1045
|
error: null,
|
|
1045
1046
|
refetchOnMount: false,
|
|
1046
1047
|
status: "success",
|
|
1047
|
-
wasLoaded: true
|
|
1048
|
+
wasLoaded: true,
|
|
1049
|
+
payload: itemPayload
|
|
1048
1050
|
};
|
|
1049
1051
|
}
|
|
1050
1052
|
}
|
|
@@ -1053,7 +1055,7 @@ function newTSDFListQueryStore({
|
|
|
1053
1055
|
action: { type: "fetch-query-success", payload }
|
|
1054
1056
|
}
|
|
1055
1057
|
);
|
|
1056
|
-
for (const { id } of items) {
|
|
1058
|
+
for (const { itemPayload: id } of items) {
|
|
1057
1059
|
const itemFetchOrquestrator = fetchItemOrquestrator?.get(String(id));
|
|
1058
1060
|
if (itemFetchOrquestrator) {
|
|
1059
1061
|
itemFetchOrquestrator.setLastFetchStartTime(fetchCtx.getStartTime());
|
|
@@ -1082,15 +1084,16 @@ function newTSDFListQueryStore({
|
|
|
1082
1084
|
function getQueryState(params) {
|
|
1083
1085
|
return store.state.queries[getQueryKey(params)];
|
|
1084
1086
|
}
|
|
1085
|
-
function getItemState(
|
|
1086
|
-
if (typeof
|
|
1087
|
-
|
|
1087
|
+
function getItemState(itemPayload) {
|
|
1088
|
+
if (typeof itemPayload === "function" || Array.isArray(itemPayload)) {
|
|
1089
|
+
const itemsId = getItemsKeyArray(itemPayload);
|
|
1090
|
+
return filterAndMap(itemsId, ({ itemKey }, ignore2) => {
|
|
1091
|
+
const item = store.state.items[itemKey];
|
|
1092
|
+
const payload = store.state.itemQueries[itemKey]?.payload;
|
|
1093
|
+
return !item || !payload ? ignore2 : { payload, data: item };
|
|
1094
|
+
});
|
|
1088
1095
|
}
|
|
1089
|
-
|
|
1090
|
-
return filterAndMap(itemsId, (itemId, ignore2) => {
|
|
1091
|
-
const item = store.state.items[itemId];
|
|
1092
|
-
return !item ? ignore2 : { id: itemId, data: item };
|
|
1093
|
-
});
|
|
1096
|
+
return store.state.items[getItemKey(itemPayload)];
|
|
1094
1097
|
}
|
|
1095
1098
|
const fetchQueryOrquestrator = createCollectionFetchOrquestrator({
|
|
1096
1099
|
fetchFn: fetchQuery,
|
|
@@ -1113,9 +1116,9 @@ function newTSDFListQueryStore({
|
|
|
1113
1116
|
return fetchQueryOrquestrator.get(getQueryKey(params)).scheduleFetch("highPriority", ["loadMore", params, size]);
|
|
1114
1117
|
}
|
|
1115
1118
|
function getQueryItems(query, itemDataSelector) {
|
|
1116
|
-
return filterAndMap(query.items, (
|
|
1117
|
-
const
|
|
1118
|
-
return
|
|
1119
|
+
return filterAndMap(query.items, (itemKey, ignore2) => {
|
|
1120
|
+
const item = store.state.items[itemKey];
|
|
1121
|
+
return item ? itemDataSelector(item, itemKey) : ignore2;
|
|
1119
1122
|
});
|
|
1120
1123
|
}
|
|
1121
1124
|
async function awaitListQueryFetch(params, size = defaultQuerySize) {
|
|
@@ -1137,24 +1140,25 @@ function newTSDFListQueryStore({
|
|
|
1137
1140
|
};
|
|
1138
1141
|
}
|
|
1139
1142
|
return query.error ? { items: [], error: query.error, hasMore: query.hasMore } : {
|
|
1140
|
-
items: getQueryItems(query, (data,
|
|
1143
|
+
items: getQueryItems(query, (data, itemKey) => ({ data, itemKey })),
|
|
1141
1144
|
error: null,
|
|
1142
1145
|
hasMore: query.hasMore
|
|
1143
1146
|
};
|
|
1144
1147
|
}
|
|
1145
|
-
async function awaitItemFetch(
|
|
1148
|
+
async function awaitItemFetch(itemPayload) {
|
|
1146
1149
|
if (!fetchItemOrquestrator)
|
|
1147
1150
|
throw new Error(noFetchFnError);
|
|
1148
|
-
const
|
|
1151
|
+
const itemKey = getItemKey(itemPayload);
|
|
1152
|
+
const wasAborted = await fetchItemOrquestrator.get(itemKey).awaitFetch(itemPayload);
|
|
1149
1153
|
if (wasAborted) {
|
|
1150
1154
|
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
1151
1155
|
}
|
|
1152
|
-
const
|
|
1153
|
-
const itemQuery = store.state.itemQueries[
|
|
1154
|
-
if (!itemQuery || !
|
|
1156
|
+
const item = store.state.items[itemKey];
|
|
1157
|
+
const itemQuery = store.state.itemQueries[itemKey];
|
|
1158
|
+
if (!itemQuery || !item) {
|
|
1155
1159
|
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
1156
1160
|
}
|
|
1157
|
-
return itemQuery.error ? { data: null, error: itemQuery.error } : { data:
|
|
1161
|
+
return itemQuery.error ? { data: null, error: itemQuery.error } : { data: item, error: null };
|
|
1158
1162
|
}
|
|
1159
1163
|
function getQueriesKeyArray(payloads) {
|
|
1160
1164
|
if (Array.isArray(payloads)) {
|
|
@@ -1195,9 +1199,9 @@ function newTSDFListQueryStore({
|
|
|
1195
1199
|
}
|
|
1196
1200
|
if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
|
|
1197
1201
|
invalidateItem(
|
|
1198
|
-
(
|
|
1202
|
+
(itemPayload) => queriesKey.some(({ payload }) => {
|
|
1199
1203
|
return syncMutationsAndInvalidations.syncItemAndQuery(
|
|
1200
|
-
|
|
1204
|
+
itemPayload,
|
|
1201
1205
|
payload
|
|
1202
1206
|
);
|
|
1203
1207
|
}),
|
|
@@ -1355,20 +1359,22 @@ function newTSDFListQueryStore({
|
|
|
1355
1359
|
});
|
|
1356
1360
|
return useModifyResult(result);
|
|
1357
1361
|
}
|
|
1358
|
-
async function fetchItem(fetchCtx,
|
|
1362
|
+
async function fetchItem(fetchCtx, itemPayload) {
|
|
1359
1363
|
if (!fetchItemOrquestrator) {
|
|
1360
1364
|
throw new Error(noFetchFnError);
|
|
1361
1365
|
}
|
|
1362
|
-
const
|
|
1366
|
+
const itemKey = getCacheId(itemPayload);
|
|
1367
|
+
const isLoaded = !store.state.itemQueries[itemKey]?.wasLoaded;
|
|
1363
1368
|
store.produceState(
|
|
1364
1369
|
(draft) => {
|
|
1365
|
-
const itemQuery = draft.itemQueries[
|
|
1370
|
+
const itemQuery = draft.itemQueries[itemKey];
|
|
1366
1371
|
if (!itemQuery) {
|
|
1367
|
-
draft.itemQueries[
|
|
1372
|
+
draft.itemQueries[itemKey] = {
|
|
1368
1373
|
status: "loading",
|
|
1369
1374
|
error: null,
|
|
1370
1375
|
wasLoaded: false,
|
|
1371
|
-
refetchOnMount: false
|
|
1376
|
+
refetchOnMount: false,
|
|
1377
|
+
payload: klona2(itemPayload)
|
|
1372
1378
|
};
|
|
1373
1379
|
return;
|
|
1374
1380
|
}
|
|
@@ -1379,25 +1385,25 @@ function newTSDFListQueryStore({
|
|
|
1379
1385
|
{
|
|
1380
1386
|
action: {
|
|
1381
1387
|
type: isLoaded ? "fetch-item-start" : "fetch-item-refetch-start",
|
|
1382
|
-
|
|
1388
|
+
itemPayload
|
|
1383
1389
|
}
|
|
1384
1390
|
}
|
|
1385
1391
|
);
|
|
1386
1392
|
try {
|
|
1387
|
-
const item = await fetchItemFn(
|
|
1393
|
+
const item = await fetchItemFn(itemPayload);
|
|
1388
1394
|
if (fetchCtx.shouldAbort())
|
|
1389
1395
|
return false;
|
|
1390
1396
|
store.produceState(
|
|
1391
1397
|
(draft) => {
|
|
1392
|
-
const itemQuery = draft.itemQueries[
|
|
1398
|
+
const itemQuery = draft.itemQueries[itemKey];
|
|
1393
1399
|
if (!itemQuery)
|
|
1394
1400
|
return;
|
|
1395
1401
|
itemQuery.status = "success";
|
|
1396
1402
|
itemQuery.wasLoaded = true;
|
|
1397
|
-
draft.items[
|
|
1403
|
+
draft.items[itemKey] = item;
|
|
1398
1404
|
},
|
|
1399
1405
|
{
|
|
1400
|
-
action: { type: "fetch-item-success",
|
|
1406
|
+
action: { type: "fetch-item-success", itemPayload }
|
|
1401
1407
|
}
|
|
1402
1408
|
);
|
|
1403
1409
|
return true;
|
|
@@ -1407,14 +1413,14 @@ function newTSDFListQueryStore({
|
|
|
1407
1413
|
const error = errorNormalizer(exception);
|
|
1408
1414
|
store.produceState(
|
|
1409
1415
|
(draft) => {
|
|
1410
|
-
const itemQuery = draft.itemQueries[
|
|
1416
|
+
const itemQuery = draft.itemQueries[itemKey];
|
|
1411
1417
|
if (!itemQuery)
|
|
1412
1418
|
return;
|
|
1413
1419
|
itemQuery.status = "error";
|
|
1414
1420
|
itemQuery.error = error;
|
|
1415
1421
|
},
|
|
1416
1422
|
{
|
|
1417
|
-
action: { type: "fetch-item-error",
|
|
1423
|
+
action: { type: "fetch-item-error", itemPayload }
|
|
1418
1424
|
}
|
|
1419
1425
|
);
|
|
1420
1426
|
return false;
|
|
@@ -1426,56 +1432,61 @@ function newTSDFListQueryStore({
|
|
|
1426
1432
|
dynamicRealtimeThrottleMs,
|
|
1427
1433
|
mediumPriorityThrottleMs
|
|
1428
1434
|
});
|
|
1429
|
-
function scheduleItemFetch(fetchType,
|
|
1435
|
+
function scheduleItemFetch(fetchType, itemPayload) {
|
|
1430
1436
|
if (!fetchItemOrquestrator) {
|
|
1431
1437
|
throw new Error(noFetchFnError);
|
|
1432
1438
|
}
|
|
1433
|
-
const fetchMultiple = Array.isArray(
|
|
1434
|
-
const itemsId = fetchMultiple ?
|
|
1435
|
-
const results = itemsId.map((
|
|
1436
|
-
|
|
1439
|
+
const fetchMultiple = Array.isArray(itemPayload);
|
|
1440
|
+
const itemsId = fetchMultiple ? itemPayload : [itemPayload];
|
|
1441
|
+
const results = itemsId.map((payload) => {
|
|
1442
|
+
const itemKey = getCacheId(payload);
|
|
1443
|
+
return fetchItemOrquestrator.get(itemKey).scheduleFetch(fetchType, payload);
|
|
1437
1444
|
});
|
|
1438
1445
|
return fetchMultiple ? results : results[0];
|
|
1439
1446
|
}
|
|
1440
|
-
function
|
|
1441
|
-
if (Array.isArray(
|
|
1442
|
-
return
|
|
1447
|
+
function getItemsKeyArray(itemsPayload) {
|
|
1448
|
+
if (Array.isArray(itemsPayload)) {
|
|
1449
|
+
return itemsPayload.map((payload) => ({
|
|
1450
|
+
itemKey: getItemKey(payload),
|
|
1451
|
+
payload
|
|
1452
|
+
}));
|
|
1443
1453
|
}
|
|
1444
|
-
if (typeof
|
|
1454
|
+
if (typeof itemsPayload === "function") {
|
|
1445
1455
|
return filterAndMap(
|
|
1446
1456
|
Object.entries(store.state.items),
|
|
1447
|
-
([
|
|
1448
|
-
|
|
1457
|
+
([itemKey, item], ignore2) => {
|
|
1458
|
+
const payload = store.state.itemQueries[itemKey]?.payload;
|
|
1459
|
+
if (item === null || !payload)
|
|
1449
1460
|
return ignore2;
|
|
1450
|
-
return
|
|
1461
|
+
return itemsPayload(payload, item) ? { itemKey, payload } : ignore2;
|
|
1451
1462
|
}
|
|
1452
1463
|
);
|
|
1453
1464
|
}
|
|
1454
|
-
return [
|
|
1465
|
+
return [{ payload: itemsPayload, itemKey: getItemKey(itemsPayload) }];
|
|
1455
1466
|
}
|
|
1456
1467
|
const itemInvalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
1457
1468
|
function invalidateItem(itemId, priority = "highPriority", ignoreInvalidationSync = false) {
|
|
1458
1469
|
if (!fetchItemOrquestrator) {
|
|
1459
1470
|
throw new Error(noFetchFnError);
|
|
1460
1471
|
}
|
|
1461
|
-
const
|
|
1462
|
-
for (const
|
|
1472
|
+
const itemsKey = getItemsKeyArray(itemId);
|
|
1473
|
+
for (const { itemKey } of itemsKey) {
|
|
1463
1474
|
store.produceState(
|
|
1464
1475
|
(draft) => {
|
|
1465
|
-
const query = draft.itemQueries[
|
|
1476
|
+
const query = draft.itemQueries[itemKey];
|
|
1466
1477
|
if (!query)
|
|
1467
1478
|
return;
|
|
1468
1479
|
query.refetchOnMount = priority;
|
|
1469
1480
|
},
|
|
1470
|
-
{ action: { type: "invalidate-item", queryKey:
|
|
1481
|
+
{ action: { type: "invalidate-item", queryKey: itemKey } }
|
|
1471
1482
|
);
|
|
1472
|
-
itemInvalidationWasTriggered.delete(
|
|
1473
|
-
storeEvents.emit("invalidateItem", { priority,
|
|
1483
|
+
itemInvalidationWasTriggered.delete(itemKey);
|
|
1484
|
+
storeEvents.emit("invalidateItem", { priority, itemKey });
|
|
1474
1485
|
}
|
|
1475
1486
|
if (syncMutationsAndInvalidations && !ignoreInvalidationSync) {
|
|
1476
1487
|
invalidateQuery(
|
|
1477
|
-
(query) =>
|
|
1478
|
-
(
|
|
1488
|
+
(query) => itemsKey.some(
|
|
1489
|
+
({ payload }) => syncMutationsAndInvalidations.syncItemAndQuery(payload, query)
|
|
1479
1490
|
),
|
|
1480
1491
|
priority,
|
|
1481
1492
|
true
|
|
@@ -1485,13 +1496,16 @@ function newTSDFListQueryStore({
|
|
|
1485
1496
|
function startItemMutation(itemId) {
|
|
1486
1497
|
if (!fetchItemOrquestrator)
|
|
1487
1498
|
throw new Error(noFetchFnError);
|
|
1488
|
-
const
|
|
1499
|
+
const itemsKey = getItemsKeyArray(itemId);
|
|
1489
1500
|
const endMutations = [];
|
|
1490
|
-
for (const
|
|
1491
|
-
endMutations.push(fetchItemOrquestrator.get(
|
|
1501
|
+
for (const { itemKey, payload } of itemsKey) {
|
|
1502
|
+
endMutations.push(fetchItemOrquestrator.get(itemKey).startMutation());
|
|
1492
1503
|
if (syncMutationsAndInvalidations) {
|
|
1493
1504
|
for (const [queryKey, query] of Object.entries(store.state.queries)) {
|
|
1494
|
-
if (syncMutationsAndInvalidations.syncItemAndQuery(
|
|
1505
|
+
if (syncMutationsAndInvalidations.syncItemAndQuery(
|
|
1506
|
+
payload,
|
|
1507
|
+
query.payload
|
|
1508
|
+
)) {
|
|
1495
1509
|
endMutations.push(
|
|
1496
1510
|
fetchQueryOrquestrator.get(queryKey).startMutation()
|
|
1497
1511
|
);
|
|
@@ -1508,27 +1522,38 @@ function newTSDFListQueryStore({
|
|
|
1508
1522
|
function defaultItemDataSelector(data) {
|
|
1509
1523
|
return data;
|
|
1510
1524
|
}
|
|
1511
|
-
function useMultipleItems(
|
|
1525
|
+
function useMultipleItems(itemsPayload_, {
|
|
1512
1526
|
selector = defaultItemDataSelector,
|
|
1513
1527
|
returnIdleStatus,
|
|
1514
1528
|
returnRefetchingStatus,
|
|
1515
1529
|
disableRefetchOnMount
|
|
1516
1530
|
} = {}) {
|
|
1517
1531
|
const dataSelector = useCallback3(selector, []);
|
|
1518
|
-
const
|
|
1532
|
+
const memoizedItemKeys = useDeepMemo(
|
|
1533
|
+
() => itemsPayload_.map(
|
|
1534
|
+
(itemPayload) => ({
|
|
1535
|
+
payload: itemPayload,
|
|
1536
|
+
itemKey: getItemKey(itemPayload)
|
|
1537
|
+
})
|
|
1538
|
+
),
|
|
1539
|
+
[itemsPayload_]
|
|
1540
|
+
);
|
|
1519
1541
|
const resultSelector = useCallback3(
|
|
1520
1542
|
(state) => {
|
|
1521
|
-
return
|
|
1522
|
-
(
|
|
1523
|
-
|
|
1524
|
-
|
|
1543
|
+
return memoizedItemKeys.map(
|
|
1544
|
+
({
|
|
1545
|
+
itemKey,
|
|
1546
|
+
payload
|
|
1547
|
+
}) => {
|
|
1548
|
+
const itemQuery = state.itemQueries[itemKey];
|
|
1549
|
+
const itemState = state.items[itemKey];
|
|
1525
1550
|
if (itemQuery === null) {
|
|
1526
1551
|
return {
|
|
1527
1552
|
status: "deleted",
|
|
1528
1553
|
error: null,
|
|
1529
1554
|
isLoading: false,
|
|
1530
|
-
|
|
1531
|
-
data: dataSelector(null,
|
|
1555
|
+
payload,
|
|
1556
|
+
data: dataSelector(null, itemKey)
|
|
1532
1557
|
};
|
|
1533
1558
|
}
|
|
1534
1559
|
if (!itemQuery) {
|
|
@@ -1536,8 +1561,8 @@ function newTSDFListQueryStore({
|
|
|
1536
1561
|
status: returnIdleStatus ? "idle" : "loading",
|
|
1537
1562
|
error: null,
|
|
1538
1563
|
isLoading: returnIdleStatus ? false : true,
|
|
1539
|
-
|
|
1540
|
-
data: dataSelector(null,
|
|
1564
|
+
payload,
|
|
1565
|
+
data: dataSelector(null, itemKey)
|
|
1541
1566
|
};
|
|
1542
1567
|
}
|
|
1543
1568
|
let status = itemQuery.status;
|
|
@@ -1548,54 +1573,59 @@ function newTSDFListQueryStore({
|
|
|
1548
1573
|
status,
|
|
1549
1574
|
error: itemQuery.error,
|
|
1550
1575
|
isLoading: status === "loading",
|
|
1551
|
-
data: dataSelector(itemState ?? null,
|
|
1552
|
-
|
|
1576
|
+
data: dataSelector(itemState ?? null, itemKey),
|
|
1577
|
+
payload
|
|
1553
1578
|
};
|
|
1554
1579
|
}
|
|
1555
1580
|
);
|
|
1556
1581
|
},
|
|
1557
|
-
[
|
|
1582
|
+
[
|
|
1583
|
+
dataSelector,
|
|
1584
|
+
memoizedItemKeys,
|
|
1585
|
+
returnIdleStatus,
|
|
1586
|
+
returnRefetchingStatus
|
|
1587
|
+
]
|
|
1558
1588
|
);
|
|
1559
1589
|
const storeState = store.useSelector(resultSelector, {
|
|
1560
1590
|
equalityFn: deepEqual3,
|
|
1561
1591
|
useExternalDeps: true
|
|
1562
1592
|
});
|
|
1563
1593
|
useOnMittEvent(storeEvents, "invalidateItem", (event) => {
|
|
1564
|
-
for (const
|
|
1565
|
-
if (
|
|
1594
|
+
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
1595
|
+
if (itemKey !== event.itemKey)
|
|
1566
1596
|
continue;
|
|
1567
|
-
if (!itemInvalidationWasTriggered.has(
|
|
1568
|
-
scheduleItemFetch(event.priority,
|
|
1569
|
-
itemInvalidationWasTriggered.add(
|
|
1597
|
+
if (!itemInvalidationWasTriggered.has(itemKey)) {
|
|
1598
|
+
scheduleItemFetch(event.priority, payload);
|
|
1599
|
+
itemInvalidationWasTriggered.add(itemKey);
|
|
1570
1600
|
}
|
|
1571
1601
|
}
|
|
1572
1602
|
});
|
|
1573
1603
|
useEffect4(() => {
|
|
1574
|
-
for (const
|
|
1575
|
-
if (
|
|
1604
|
+
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
1605
|
+
if (itemKey) {
|
|
1576
1606
|
if (disableRefetchOnMount) {
|
|
1577
|
-
const itemState = store.state.itemQueries[
|
|
1607
|
+
const itemState = store.state.itemQueries[itemKey];
|
|
1578
1608
|
const shouldFetch = !itemState || !itemState.wasLoaded || itemState.refetchOnMount;
|
|
1579
1609
|
if (shouldFetch) {
|
|
1580
1610
|
scheduleItemFetch(
|
|
1581
1611
|
itemState?.refetchOnMount || "lowPriority",
|
|
1582
|
-
|
|
1612
|
+
payload
|
|
1583
1613
|
);
|
|
1584
1614
|
return;
|
|
1585
1615
|
}
|
|
1586
1616
|
} else {
|
|
1587
|
-
scheduleItemFetch("lowPriority",
|
|
1617
|
+
scheduleItemFetch("lowPriority", payload);
|
|
1588
1618
|
}
|
|
1589
1619
|
}
|
|
1590
1620
|
}
|
|
1591
|
-
}, [disableRefetchOnMount,
|
|
1621
|
+
}, [disableRefetchOnMount, memoizedItemKeys]);
|
|
1592
1622
|
return storeState;
|
|
1593
1623
|
}
|
|
1594
|
-
function useItem(
|
|
1624
|
+
function useItem(itemPayload, options = {}) {
|
|
1595
1625
|
const { ensureIsLoaded } = options;
|
|
1596
1626
|
const query = useMemo4(
|
|
1597
|
-
() =>
|
|
1598
|
-
[
|
|
1627
|
+
() => itemPayload === false || itemPayload === null || itemPayload === void 0 ? [] : [itemPayload],
|
|
1628
|
+
[itemPayload]
|
|
1599
1629
|
);
|
|
1600
1630
|
const queryResult = useMultipleItems(query, options);
|
|
1601
1631
|
const memoizedSelector = useCallback3(
|
|
@@ -1607,24 +1637,24 @@ function newTSDFListQueryStore({
|
|
|
1607
1637
|
error: null,
|
|
1608
1638
|
isLoading: false,
|
|
1609
1639
|
status: "idle",
|
|
1610
|
-
data: memoizedSelector(null,
|
|
1611
|
-
|
|
1640
|
+
data: memoizedSelector(null, ""),
|
|
1641
|
+
payload: itemPayload || null
|
|
1612
1642
|
},
|
|
1613
|
-
[
|
|
1643
|
+
[itemPayload, memoizedSelector, queryResult]
|
|
1614
1644
|
);
|
|
1615
1645
|
const [useModifyResult, emitIsLoadedEvt] = useEnsureIsLoaded(
|
|
1616
1646
|
ensureIsLoaded,
|
|
1617
|
-
!!
|
|
1647
|
+
!!itemPayload,
|
|
1618
1648
|
() => {
|
|
1619
|
-
if (
|
|
1620
|
-
scheduleItemFetch("highPriority",
|
|
1649
|
+
if (itemPayload) {
|
|
1650
|
+
scheduleItemFetch("highPriority", itemPayload);
|
|
1621
1651
|
}
|
|
1622
1652
|
}
|
|
1623
1653
|
);
|
|
1624
1654
|
useSubscribeToStore3(store, ({ observe }) => {
|
|
1625
|
-
if (!ensureIsLoaded || !
|
|
1655
|
+
if (!ensureIsLoaded || !itemPayload)
|
|
1626
1656
|
return;
|
|
1627
|
-
observe.ifSelector((state) => state.itemQueries[
|
|
1657
|
+
observe.ifSelector((state) => state.itemQueries[itemPayload]?.status).change.then(({ current }) => {
|
|
1628
1658
|
if (current === "success" || current === "error") {
|
|
1629
1659
|
emitIsLoadedEvt("isLoaded", true);
|
|
1630
1660
|
}
|
|
@@ -1633,19 +1663,19 @@ function newTSDFListQueryStore({
|
|
|
1633
1663
|
return useModifyResult(result);
|
|
1634
1664
|
}
|
|
1635
1665
|
function updateItemState(itemIds, produceNewData, ifNothingWasUpdated) {
|
|
1636
|
-
const itemKeys =
|
|
1666
|
+
const itemKeys = getItemsKeyArray(itemIds);
|
|
1637
1667
|
let someItemWasUpdated = false;
|
|
1638
1668
|
store.batch(
|
|
1639
1669
|
() => {
|
|
1640
1670
|
store.produceState((draftState) => {
|
|
1641
|
-
for (const
|
|
1642
|
-
const item = draftState.items[
|
|
1671
|
+
for (const { itemKey, payload } of itemKeys) {
|
|
1672
|
+
const item = draftState.items[itemKey];
|
|
1643
1673
|
if (!item)
|
|
1644
1674
|
continue;
|
|
1645
1675
|
someItemWasUpdated = true;
|
|
1646
|
-
const newData = produceNewData(item,
|
|
1676
|
+
const newData = produceNewData(item, payload);
|
|
1647
1677
|
if (newData) {
|
|
1648
|
-
draftState.items[
|
|
1678
|
+
draftState.items[itemKey] = newData;
|
|
1649
1679
|
}
|
|
1650
1680
|
}
|
|
1651
1681
|
});
|
|
@@ -1657,25 +1687,27 @@ function newTSDFListQueryStore({
|
|
|
1657
1687
|
);
|
|
1658
1688
|
return someItemWasUpdated;
|
|
1659
1689
|
}
|
|
1660
|
-
function addItemToState(
|
|
1690
|
+
function addItemToState(itemPayload, data) {
|
|
1691
|
+
const itemKey = getItemKey(itemPayload);
|
|
1661
1692
|
store.produceState(
|
|
1662
1693
|
(draftState) => {
|
|
1663
|
-
draftState.items[
|
|
1664
|
-
draftState.itemQueries[
|
|
1694
|
+
draftState.items[itemKey] = data;
|
|
1695
|
+
draftState.itemQueries[itemKey] = {
|
|
1665
1696
|
status: "success",
|
|
1666
1697
|
wasLoaded: true,
|
|
1667
1698
|
refetchOnMount: false,
|
|
1668
|
-
error: null
|
|
1699
|
+
error: null,
|
|
1700
|
+
payload: klona2(itemPayload)
|
|
1669
1701
|
};
|
|
1670
1702
|
},
|
|
1671
|
-
{ action: { type: "create-item-state",
|
|
1703
|
+
{ action: { type: "create-item-state", itemPayload } }
|
|
1672
1704
|
);
|
|
1673
1705
|
}
|
|
1674
1706
|
function deleteItemState(itemId) {
|
|
1675
|
-
const itemsId =
|
|
1707
|
+
const itemsId = getItemsKeyArray(itemId);
|
|
1676
1708
|
store.produceState(
|
|
1677
1709
|
(draftState) => {
|
|
1678
|
-
for (const itemKey of itemsId) {
|
|
1710
|
+
for (const { itemKey } of itemsId) {
|
|
1679
1711
|
draftState.items[itemKey] = null;
|
|
1680
1712
|
draftState.itemQueries[itemKey] = null;
|
|
1681
1713
|
for (const query of Object.values(draftState.queries)) {
|
|
@@ -1712,6 +1744,7 @@ function newTSDFListQueryStore({
|
|
|
1712
1744
|
awaitListQueryFetch,
|
|
1713
1745
|
useMultipleListQueries,
|
|
1714
1746
|
useListQuery,
|
|
1747
|
+
getItemKey,
|
|
1715
1748
|
useItem,
|
|
1716
1749
|
useMultipleItems,
|
|
1717
1750
|
addItemToState,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tsdf",
|
|
3
|
-
"version": "8.0.0-beta.
|
|
3
|
+
"version": "8.0.0-beta.6",
|
|
4
4
|
"description": "A data fetching solution based on t-state",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Lucas Santos",
|
|
@@ -64,6 +64,7 @@
|
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
66
|
"immer": "^9.0.17",
|
|
67
|
+
"klona": "^2.0.6",
|
|
67
68
|
"mitt": "^3.0.0",
|
|
68
69
|
"t-state": "^7.5.1"
|
|
69
70
|
},
|