tsdf 11.1.3 → 11.1.5
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 +69 -78
- package/dist/main.d.ts +1 -0
- package/dist/main.js +47 -46
- package/package.json +2 -2
package/dist/main.cjs
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __create = Object.create;
|
|
3
2
|
var __defProp = Object.defineProperty;
|
|
4
3
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
7
5
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
6
|
var __export = (target, all) => {
|
|
9
7
|
for (var name in all)
|
|
@@ -17,14 +15,6 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
17
15
|
}
|
|
18
16
|
return to;
|
|
19
17
|
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
18
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
19
|
|
|
30
20
|
// src/main.ts
|
|
@@ -38,8 +28,7 @@ module.exports = __toCommonJS(main_exports);
|
|
|
38
28
|
|
|
39
29
|
// src/documentStore.ts
|
|
40
30
|
var import_immer = require("immer");
|
|
41
|
-
var
|
|
42
|
-
var import_react3 = require("react");
|
|
31
|
+
var import_react4 = require("react");
|
|
43
32
|
var import_t_state3 = require("t-state");
|
|
44
33
|
|
|
45
34
|
// src/fetchOrquestrator.ts
|
|
@@ -153,15 +142,16 @@ function createFetchOrquestrator({
|
|
|
153
142
|
}
|
|
154
143
|
function scheduleFetch(fetchType, params) {
|
|
155
144
|
const startTime = Date.now();
|
|
156
|
-
|
|
145
|
+
const fetchTypeToUse = !lastFetchStartTime ? "highPriority" : fetchType;
|
|
146
|
+
if (dynamicRealtimeThrottleMs && fetchTypeToUse === "realtimeUpdate") {
|
|
157
147
|
if (scheduleRTU(startTime, params)) {
|
|
158
148
|
return "rt-scheduled";
|
|
159
149
|
}
|
|
160
150
|
}
|
|
161
|
-
if (shouldSkipFetch(
|
|
151
|
+
if (shouldSkipFetch(fetchTypeToUse, startTime)) {
|
|
162
152
|
return "skipped";
|
|
163
153
|
}
|
|
164
|
-
if (shouldScheduleFetch(
|
|
154
|
+
if (shouldScheduleFetch(fetchTypeToUse, params)) {
|
|
165
155
|
return "scheduled";
|
|
166
156
|
}
|
|
167
157
|
startFetch(params, startTime);
|
|
@@ -290,8 +280,9 @@ var fetchTypePriority = {
|
|
|
290
280
|
};
|
|
291
281
|
|
|
292
282
|
// src/useEnsureIsLoaded.ts
|
|
293
|
-
var
|
|
294
|
-
var import_react2 = require("react");
|
|
283
|
+
var import_evtmitter = require("evtmitter");
|
|
284
|
+
var import_react2 = require("evtmitter/react");
|
|
285
|
+
var import_react3 = require("react");
|
|
295
286
|
|
|
296
287
|
// src/utils/hooks.ts
|
|
297
288
|
var import_react = require("react");
|
|
@@ -308,26 +299,6 @@ function useDeepCompareMemoize(value, equalityFn = import_t_state.deepEqual) {
|
|
|
308
299
|
function useDeepMemo(callback, deeps) {
|
|
309
300
|
return (0, import_react.useMemo)(callback, useDeepCompareMemoize(deeps));
|
|
310
301
|
}
|
|
311
|
-
function useLatestValue(value) {
|
|
312
|
-
const ref = (0, import_react.useRef)({ insideEffect: value, insideMemo: value });
|
|
313
|
-
ref.current.insideMemo = value;
|
|
314
|
-
(0, import_react.useLayoutEffect)(() => {
|
|
315
|
-
ref.current.insideEffect = value;
|
|
316
|
-
});
|
|
317
|
-
return ref.current;
|
|
318
|
-
}
|
|
319
|
-
function useOnMittEvent(mitt5, event, callback) {
|
|
320
|
-
const latestCallback = useLatestValue(callback);
|
|
321
|
-
(0, import_react.useLayoutEffect)(() => {
|
|
322
|
-
const cb = (payload) => {
|
|
323
|
-
latestCallback.insideEffect(payload);
|
|
324
|
-
};
|
|
325
|
-
mitt5.on(event, cb);
|
|
326
|
-
return () => {
|
|
327
|
-
mitt5.off(event, cb);
|
|
328
|
-
};
|
|
329
|
-
}, [mitt5, event, latestCallback]);
|
|
330
|
-
}
|
|
331
302
|
function usePrevious(value, initial) {
|
|
332
303
|
const ref = (0, import_react.useRef)(initial);
|
|
333
304
|
(0, import_react.useEffect)(() => {
|
|
@@ -358,8 +329,8 @@ function useConst(getValue) {
|
|
|
358
329
|
|
|
359
330
|
// src/useEnsureIsLoaded.ts
|
|
360
331
|
function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
361
|
-
const isLoadedEvtEmitter = useConst(() => (0,
|
|
362
|
-
const [isForceLoading, setIsForceLoading] = (0,
|
|
332
|
+
const isLoadedEvtEmitter = useConst(() => (0, import_evtmitter.evtmitter)());
|
|
333
|
+
const [isForceLoading, setIsForceLoading] = (0, import_react3.useState)(true);
|
|
363
334
|
useOnChange(
|
|
364
335
|
ensureIsLoaded && isForceLoading && enabled,
|
|
365
336
|
({ current }) => {
|
|
@@ -369,13 +340,13 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
369
340
|
},
|
|
370
341
|
{ callOnMount: true }
|
|
371
342
|
);
|
|
372
|
-
|
|
343
|
+
(0, import_react2.useOnEvtmitterEvent)(isLoadedEvtEmitter, "isLoaded", (isLoaded) => {
|
|
373
344
|
if (ensureIsLoaded && enabled && isLoaded) {
|
|
374
345
|
setIsForceLoading(false);
|
|
375
346
|
}
|
|
376
347
|
});
|
|
377
348
|
function useModifyResult(result) {
|
|
378
|
-
return (0,
|
|
349
|
+
return (0, import_react3.useMemo)(() => {
|
|
379
350
|
if (ensureIsLoaded) {
|
|
380
351
|
const newStatus = enabled && isForceLoading ? "loading" : result.status;
|
|
381
352
|
return {
|
|
@@ -390,7 +361,7 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
390
361
|
return [useModifyResult, isLoadedEvtEmitter.emit];
|
|
391
362
|
}
|
|
392
363
|
|
|
393
|
-
// src/utils/
|
|
364
|
+
// src/utils/reusePrevIfEqual.ts
|
|
394
365
|
var import_t_state2 = require("t-state");
|
|
395
366
|
function reusePrevIfEqual({
|
|
396
367
|
prev,
|
|
@@ -406,6 +377,8 @@ function reusePrevIfEqual({
|
|
|
406
377
|
}
|
|
407
378
|
|
|
408
379
|
// src/documentStore.ts
|
|
380
|
+
var import_evtmitter2 = require("evtmitter");
|
|
381
|
+
var import_react5 = require("evtmitter/react");
|
|
409
382
|
function newTSDFDocumentStore({
|
|
410
383
|
debugName,
|
|
411
384
|
fetchFn,
|
|
@@ -476,12 +449,15 @@ function newTSDFDocumentStore({
|
|
|
476
449
|
if (wasAborted) {
|
|
477
450
|
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
478
451
|
}
|
|
452
|
+
if (store.state.error) {
|
|
453
|
+
return { data: null, error: store.state.error };
|
|
454
|
+
}
|
|
479
455
|
if (!store.state.data) {
|
|
480
|
-
return { data: null, error: errorNormalizer(new Error("
|
|
456
|
+
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
481
457
|
}
|
|
482
|
-
return
|
|
458
|
+
return { data: store.state.data, error: null };
|
|
483
459
|
}
|
|
484
|
-
const storeEvents = (0,
|
|
460
|
+
const storeEvents = (0, import_evtmitter2.evtmitter)();
|
|
485
461
|
let invalidationWasTriggered = false;
|
|
486
462
|
function invalidateData(priority = "highPriority") {
|
|
487
463
|
const currentInvalidationPriority = store.state.refetchOnMount ? fetchTypePriority[store.state.refetchOnMount] : -1;
|
|
@@ -500,7 +476,7 @@ function newTSDFDocumentStore({
|
|
|
500
476
|
returnIdleStatus = !!disabled,
|
|
501
477
|
ensureIsLoaded
|
|
502
478
|
} = {}) {
|
|
503
|
-
const storeStateSelector = (0,
|
|
479
|
+
const storeStateSelector = (0, import_react4.useCallback)(
|
|
504
480
|
(state) => {
|
|
505
481
|
const { error } = state;
|
|
506
482
|
const data = selector ? selector(state.data) : state.data;
|
|
@@ -523,14 +499,14 @@ function newTSDFDocumentStore({
|
|
|
523
499
|
const storeState = store.useSelector(storeStateSelector, {
|
|
524
500
|
useExternalDeps: true
|
|
525
501
|
});
|
|
526
|
-
|
|
502
|
+
(0, import_react5.useOnEvtmitterEvent)(storeEvents, "invalidateData", (priority) => {
|
|
527
503
|
if (!invalidationWasTriggered) {
|
|
528
504
|
store.setKey("refetchOnMount", false);
|
|
529
505
|
scheduleFetch(priority);
|
|
530
506
|
invalidationWasTriggered = true;
|
|
531
507
|
}
|
|
532
508
|
});
|
|
533
|
-
(0,
|
|
509
|
+
(0, import_react4.useEffect)(() => {
|
|
534
510
|
if (disabled)
|
|
535
511
|
return;
|
|
536
512
|
const fetchType = store.state.refetchOnMount || "lowPriority";
|
|
@@ -585,8 +561,10 @@ function newTSDFDocumentStore({
|
|
|
585
561
|
}
|
|
586
562
|
|
|
587
563
|
// src/collectionStore.ts
|
|
588
|
-
var
|
|
589
|
-
var
|
|
564
|
+
var import_evtmitter3 = require("evtmitter");
|
|
565
|
+
var import_react6 = require("evtmitter/react");
|
|
566
|
+
var import_json = require("klona/json");
|
|
567
|
+
var import_react7 = require("react");
|
|
590
568
|
var import_t_state4 = require("t-state");
|
|
591
569
|
|
|
592
570
|
// src/collectionFetchOrquestrator.ts
|
|
@@ -636,7 +614,6 @@ function orderedProps(obj) {
|
|
|
636
614
|
}
|
|
637
615
|
|
|
638
616
|
// src/collectionStore.ts
|
|
639
|
-
var import_json = require("klona/json");
|
|
640
617
|
function newTSDFCollectionStore({
|
|
641
618
|
debugName,
|
|
642
619
|
fetchFn,
|
|
@@ -761,10 +738,13 @@ function newTSDFCollectionStore({
|
|
|
761
738
|
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
762
739
|
}
|
|
763
740
|
const item = store.state[itemId];
|
|
741
|
+
if (item?.error) {
|
|
742
|
+
return { data: null, error: item.error };
|
|
743
|
+
}
|
|
764
744
|
if (!item?.data) {
|
|
765
745
|
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
766
746
|
}
|
|
767
|
-
return
|
|
747
|
+
return { data: item.data, error: null };
|
|
768
748
|
}
|
|
769
749
|
function getItemsKeyArray(params) {
|
|
770
750
|
const items = store.state;
|
|
@@ -778,7 +758,7 @@ function newTSDFCollectionStore({
|
|
|
778
758
|
return [getItemKey(params)];
|
|
779
759
|
}
|
|
780
760
|
}
|
|
781
|
-
const storeEvents = (0,
|
|
761
|
+
const storeEvents = (0, import_evtmitter3.evtmitter)();
|
|
782
762
|
const invalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
783
763
|
function invalidateItem(itemPayload, priority = "highPriority") {
|
|
784
764
|
const itemsKey = getItemsKeyArray(itemPayload);
|
|
@@ -824,12 +804,12 @@ function newTSDFCollectionStore({
|
|
|
824
804
|
return { itemKey: getItemKey(payload), payload };
|
|
825
805
|
});
|
|
826
806
|
}, [queries]);
|
|
827
|
-
const dataSelector = (0,
|
|
807
|
+
const dataSelector = (0, import_react7.useCallback)((itemState) => {
|
|
828
808
|
if (selector)
|
|
829
809
|
return selector(itemState);
|
|
830
810
|
return itemState;
|
|
831
811
|
}, []);
|
|
832
|
-
const resultSelector = (0,
|
|
812
|
+
const resultSelector = (0, import_react7.useCallback)(
|
|
833
813
|
(state) => {
|
|
834
814
|
return queriesWithId.map(
|
|
835
815
|
({
|
|
@@ -884,7 +864,7 @@ function newTSDFCollectionStore({
|
|
|
884
864
|
equalityFn: import_t_state4.deepEqual,
|
|
885
865
|
useExternalDeps: true
|
|
886
866
|
});
|
|
887
|
-
|
|
867
|
+
(0, import_react6.useOnEvtmitterEvent)(storeEvents, "invalidateData", (event) => {
|
|
888
868
|
for (const { itemKey, payload } of queriesWithId) {
|
|
889
869
|
if (itemKey !== event.itemKey)
|
|
890
870
|
continue;
|
|
@@ -900,7 +880,7 @@ function newTSDFCollectionStore({
|
|
|
900
880
|
}
|
|
901
881
|
}
|
|
902
882
|
});
|
|
903
|
-
(0,
|
|
883
|
+
(0, import_react7.useEffect)(() => {
|
|
904
884
|
for (const { itemKey: itemId, payload } of queriesWithId) {
|
|
905
885
|
if (itemId) {
|
|
906
886
|
const itemState = getItemState(payload);
|
|
@@ -921,12 +901,12 @@ function newTSDFCollectionStore({
|
|
|
921
901
|
}
|
|
922
902
|
function useItem(payload, options = {}) {
|
|
923
903
|
const { selector, ensureIsLoaded } = options;
|
|
924
|
-
const query = (0,
|
|
904
|
+
const query = (0, import_react7.useMemo)(
|
|
925
905
|
() => payload === false || payload === null || payload === void 0 ? [] : [payload],
|
|
926
906
|
[payload]
|
|
927
907
|
);
|
|
928
908
|
const item = useMultipleItems(query, options);
|
|
929
|
-
const result = (0,
|
|
909
|
+
const result = (0, import_react7.useMemo)(
|
|
930
910
|
() => item[0] ?? {
|
|
931
911
|
payload: void 0,
|
|
932
912
|
data: selector ? selector(null) : null,
|
|
@@ -1041,10 +1021,11 @@ function newTSDFCollectionStore({
|
|
|
1041
1021
|
}
|
|
1042
1022
|
|
|
1043
1023
|
// src/listQueryStore.ts
|
|
1044
|
-
var
|
|
1045
|
-
var
|
|
1046
|
-
var import_t_state5 = require("t-state");
|
|
1024
|
+
var import_evtmitter4 = require("evtmitter");
|
|
1025
|
+
var import_react8 = require("evtmitter/react");
|
|
1047
1026
|
var import_json2 = require("klona/json");
|
|
1027
|
+
var import_react9 = require("react");
|
|
1028
|
+
var import_t_state5 = require("t-state");
|
|
1048
1029
|
|
|
1049
1030
|
// src/utils/findAndMap.ts
|
|
1050
1031
|
function findAndMap(arr, findFn) {
|
|
@@ -1282,6 +1263,13 @@ function newTSDFListQueryStore({
|
|
|
1282
1263
|
};
|
|
1283
1264
|
}
|
|
1284
1265
|
const query = store.state.queries[queryKey];
|
|
1266
|
+
if (query?.error) {
|
|
1267
|
+
return {
|
|
1268
|
+
items: [],
|
|
1269
|
+
error: query.error,
|
|
1270
|
+
hasMore: query.hasMore
|
|
1271
|
+
};
|
|
1272
|
+
}
|
|
1285
1273
|
if (!query) {
|
|
1286
1274
|
return {
|
|
1287
1275
|
items: [],
|
|
@@ -1289,7 +1277,7 @@ function newTSDFListQueryStore({
|
|
|
1289
1277
|
hasMore: false
|
|
1290
1278
|
};
|
|
1291
1279
|
}
|
|
1292
|
-
return
|
|
1280
|
+
return {
|
|
1293
1281
|
items: getQueryItems(query, (data, itemPayload) => ({
|
|
1294
1282
|
data,
|
|
1295
1283
|
itemPayload
|
|
@@ -1308,10 +1296,13 @@ function newTSDFListQueryStore({
|
|
|
1308
1296
|
}
|
|
1309
1297
|
const item = store.state.items[itemKey];
|
|
1310
1298
|
const itemQuery = store.state.itemQueries[itemKey];
|
|
1299
|
+
if (itemQuery?.error) {
|
|
1300
|
+
return { data: null, error: itemQuery.error };
|
|
1301
|
+
}
|
|
1311
1302
|
if (!itemQuery || !item) {
|
|
1312
1303
|
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
1313
1304
|
}
|
|
1314
|
-
return
|
|
1305
|
+
return { data: item, error: null };
|
|
1315
1306
|
}
|
|
1316
1307
|
function getQueriesKeyArray(payloads) {
|
|
1317
1308
|
if (Array.isArray(payloads)) {
|
|
@@ -1333,7 +1324,7 @@ function newTSDFListQueryStore({
|
|
|
1333
1324
|
return [{ key: getQueryKey(payloads), payload: payloads }];
|
|
1334
1325
|
}
|
|
1335
1326
|
}
|
|
1336
|
-
const storeEvents = (0,
|
|
1327
|
+
const storeEvents = (0, import_evtmitter4.evtmitter)();
|
|
1337
1328
|
const queryInvalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
1338
1329
|
function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
|
|
1339
1330
|
const queriesKey = getQueriesKeyArray(queryPayload);
|
|
@@ -1399,8 +1390,8 @@ function newTSDFListQueryStore({
|
|
|
1399
1390
|
};
|
|
1400
1391
|
});
|
|
1401
1392
|
}, [queries]);
|
|
1402
|
-
const dataSelector = (0,
|
|
1403
|
-
const resultSelector = (0,
|
|
1393
|
+
const dataSelector = (0, import_react9.useCallback)(itemSelector, []);
|
|
1394
|
+
const resultSelector = (0, import_react9.useCallback)(
|
|
1404
1395
|
(state) => {
|
|
1405
1396
|
return queriesWithId.map(
|
|
1406
1397
|
({
|
|
@@ -1449,7 +1440,7 @@ function newTSDFListQueryStore({
|
|
|
1449
1440
|
equalityFn: import_t_state5.deepEqual,
|
|
1450
1441
|
useExternalDeps: true
|
|
1451
1442
|
});
|
|
1452
|
-
|
|
1443
|
+
(0, import_react8.useOnEvtmitterEvent)(storeEvents, "invalidateQuery", (event) => {
|
|
1453
1444
|
for (const { key, payload } of queriesWithId) {
|
|
1454
1445
|
if (key !== event.queryKey)
|
|
1455
1446
|
continue;
|
|
@@ -1466,7 +1457,7 @@ function newTSDFListQueryStore({
|
|
|
1466
1457
|
}
|
|
1467
1458
|
});
|
|
1468
1459
|
const loadSizeConst = useConst(() => loadSize);
|
|
1469
|
-
(0,
|
|
1460
|
+
(0, import_react9.useEffect)(() => {
|
|
1470
1461
|
for (const { key: itemId, payload: fetchParams } of queriesWithId) {
|
|
1471
1462
|
if (itemId) {
|
|
1472
1463
|
const itemState = getQueryState(fetchParams);
|
|
@@ -1487,12 +1478,12 @@ function newTSDFListQueryStore({
|
|
|
1487
1478
|
}
|
|
1488
1479
|
function useListQuery(payload, options = {}) {
|
|
1489
1480
|
const { ensureIsLoaded } = options;
|
|
1490
|
-
const query = (0,
|
|
1481
|
+
const query = (0, import_react9.useMemo)(
|
|
1491
1482
|
() => payload === false || payload === null || payload === void 0 ? [] : [payload],
|
|
1492
1483
|
[payload]
|
|
1493
1484
|
);
|
|
1494
1485
|
const queryResult = useMultipleListQueries(query, options);
|
|
1495
|
-
const result = (0,
|
|
1486
|
+
const result = (0, import_react9.useMemo)(
|
|
1496
1487
|
() => queryResult[0] ?? {
|
|
1497
1488
|
payload: void 0,
|
|
1498
1489
|
error: null,
|
|
@@ -1706,7 +1697,7 @@ function newTSDFListQueryStore({
|
|
|
1706
1697
|
disableRefetchOnMount,
|
|
1707
1698
|
loadFromStateOnly
|
|
1708
1699
|
} = {}) {
|
|
1709
|
-
const dataSelector = (0,
|
|
1700
|
+
const dataSelector = (0, import_react9.useCallback)(selector, []);
|
|
1710
1701
|
const memoizedItemKeys = useDeepMemo(
|
|
1711
1702
|
() => itemsPayload_.map(
|
|
1712
1703
|
(itemPayload) => ({
|
|
@@ -1716,7 +1707,7 @@ function newTSDFListQueryStore({
|
|
|
1716
1707
|
),
|
|
1717
1708
|
[itemsPayload_]
|
|
1718
1709
|
);
|
|
1719
|
-
const resultSelector = (0,
|
|
1710
|
+
const resultSelector = (0, import_react9.useCallback)(
|
|
1720
1711
|
(state) => {
|
|
1721
1712
|
return memoizedItemKeys.map(
|
|
1722
1713
|
({
|
|
@@ -1768,7 +1759,7 @@ function newTSDFListQueryStore({
|
|
|
1768
1759
|
equalityFn: import_t_state5.deepEqual,
|
|
1769
1760
|
useExternalDeps: true
|
|
1770
1761
|
});
|
|
1771
|
-
|
|
1762
|
+
(0, import_react8.useOnEvtmitterEvent)(storeEvents, "invalidateItem", (event) => {
|
|
1772
1763
|
if (loadFromStateOnly)
|
|
1773
1764
|
return;
|
|
1774
1765
|
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
@@ -1786,7 +1777,7 @@ function newTSDFListQueryStore({
|
|
|
1786
1777
|
}
|
|
1787
1778
|
}
|
|
1788
1779
|
});
|
|
1789
|
-
(0,
|
|
1780
|
+
(0, import_react9.useEffect)(() => {
|
|
1790
1781
|
if (loadFromStateOnly)
|
|
1791
1782
|
return;
|
|
1792
1783
|
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
@@ -1809,16 +1800,16 @@ function newTSDFListQueryStore({
|
|
|
1809
1800
|
}
|
|
1810
1801
|
function useItem(itemPayload, options = {}) {
|
|
1811
1802
|
const { ensureIsLoaded } = options;
|
|
1812
|
-
const query = (0,
|
|
1803
|
+
const query = (0, import_react9.useMemo)(
|
|
1813
1804
|
() => itemPayload === false || itemPayload === null || itemPayload === void 0 ? [] : [itemPayload],
|
|
1814
1805
|
[itemPayload]
|
|
1815
1806
|
);
|
|
1816
1807
|
const queryResult = useMultipleItems(query, options);
|
|
1817
|
-
const memoizedSelector = (0,
|
|
1808
|
+
const memoizedSelector = (0, import_react9.useCallback)(
|
|
1818
1809
|
options.selector ?? defaultItemDataSelector,
|
|
1819
1810
|
[]
|
|
1820
1811
|
);
|
|
1821
|
-
const result = (0,
|
|
1812
|
+
const result = (0, import_react9.useMemo)(
|
|
1822
1813
|
() => queryResult[0] ?? {
|
|
1823
1814
|
error: null,
|
|
1824
1815
|
isLoading: false,
|
package/dist/main.d.ts
CHANGED
|
@@ -113,6 +113,7 @@ declare function newTSDFCollectionStore<ItemState extends ValidStoreState, NErro
|
|
|
113
113
|
returnRefetchingStatus?: boolean | undefined;
|
|
114
114
|
disableRefetchOnMount?: boolean | undefined;
|
|
115
115
|
ensureIsLoaded?: boolean | undefined;
|
|
116
|
+
returnIdleStatus?: boolean | undefined;
|
|
116
117
|
}) => TSFDUseCollectionItemReturn<Selected_1, ItemPayload, NError>;
|
|
117
118
|
getItemKey: (params: ItemPayload) => string;
|
|
118
119
|
getItemState: {
|
package/dist/main.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
// src/documentStore.ts
|
|
2
2
|
import { produce } from "immer";
|
|
3
|
-
import mitt2 from "mitt";
|
|
4
3
|
import { useCallback, useEffect as useEffect2 } from "react";
|
|
5
4
|
import { Store, useSubscribeToStore } from "t-state";
|
|
6
5
|
|
|
@@ -115,15 +114,16 @@ function createFetchOrquestrator({
|
|
|
115
114
|
}
|
|
116
115
|
function scheduleFetch(fetchType, params) {
|
|
117
116
|
const startTime = Date.now();
|
|
118
|
-
|
|
117
|
+
const fetchTypeToUse = !lastFetchStartTime ? "highPriority" : fetchType;
|
|
118
|
+
if (dynamicRealtimeThrottleMs && fetchTypeToUse === "realtimeUpdate") {
|
|
119
119
|
if (scheduleRTU(startTime, params)) {
|
|
120
120
|
return "rt-scheduled";
|
|
121
121
|
}
|
|
122
122
|
}
|
|
123
|
-
if (shouldSkipFetch(
|
|
123
|
+
if (shouldSkipFetch(fetchTypeToUse, startTime)) {
|
|
124
124
|
return "skipped";
|
|
125
125
|
}
|
|
126
|
-
if (shouldScheduleFetch(
|
|
126
|
+
if (shouldScheduleFetch(fetchTypeToUse, params)) {
|
|
127
127
|
return "scheduled";
|
|
128
128
|
}
|
|
129
129
|
startFetch(params, startTime);
|
|
@@ -252,7 +252,8 @@ var fetchTypePriority = {
|
|
|
252
252
|
};
|
|
253
253
|
|
|
254
254
|
// src/useEnsureIsLoaded.ts
|
|
255
|
-
import
|
|
255
|
+
import { evtmitter } from "evtmitter";
|
|
256
|
+
import { useOnEvtmitterEvent } from "evtmitter/react";
|
|
256
257
|
import { useMemo as useMemo2, useState } from "react";
|
|
257
258
|
|
|
258
259
|
// src/utils/hooks.ts
|
|
@@ -270,26 +271,6 @@ function useDeepCompareMemoize(value, equalityFn = deepEqual) {
|
|
|
270
271
|
function useDeepMemo(callback, deeps) {
|
|
271
272
|
return useMemo(callback, useDeepCompareMemoize(deeps));
|
|
272
273
|
}
|
|
273
|
-
function useLatestValue(value) {
|
|
274
|
-
const ref = useRef({ insideEffect: value, insideMemo: value });
|
|
275
|
-
ref.current.insideMemo = value;
|
|
276
|
-
useLayoutEffect(() => {
|
|
277
|
-
ref.current.insideEffect = value;
|
|
278
|
-
});
|
|
279
|
-
return ref.current;
|
|
280
|
-
}
|
|
281
|
-
function useOnMittEvent(mitt5, event, callback) {
|
|
282
|
-
const latestCallback = useLatestValue(callback);
|
|
283
|
-
useLayoutEffect(() => {
|
|
284
|
-
const cb = (payload) => {
|
|
285
|
-
latestCallback.insideEffect(payload);
|
|
286
|
-
};
|
|
287
|
-
mitt5.on(event, cb);
|
|
288
|
-
return () => {
|
|
289
|
-
mitt5.off(event, cb);
|
|
290
|
-
};
|
|
291
|
-
}, [mitt5, event, latestCallback]);
|
|
292
|
-
}
|
|
293
274
|
function usePrevious(value, initial) {
|
|
294
275
|
const ref = useRef(initial);
|
|
295
276
|
useEffect(() => {
|
|
@@ -320,7 +301,7 @@ function useConst(getValue) {
|
|
|
320
301
|
|
|
321
302
|
// src/useEnsureIsLoaded.ts
|
|
322
303
|
function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
323
|
-
const isLoadedEvtEmitter = useConst(() =>
|
|
304
|
+
const isLoadedEvtEmitter = useConst(() => evtmitter());
|
|
324
305
|
const [isForceLoading, setIsForceLoading] = useState(true);
|
|
325
306
|
useOnChange(
|
|
326
307
|
ensureIsLoaded && isForceLoading && enabled,
|
|
@@ -331,7 +312,7 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
331
312
|
},
|
|
332
313
|
{ callOnMount: true }
|
|
333
314
|
);
|
|
334
|
-
|
|
315
|
+
useOnEvtmitterEvent(isLoadedEvtEmitter, "isLoaded", (isLoaded) => {
|
|
335
316
|
if (ensureIsLoaded && enabled && isLoaded) {
|
|
336
317
|
setIsForceLoading(false);
|
|
337
318
|
}
|
|
@@ -352,7 +333,7 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
352
333
|
return [useModifyResult, isLoadedEvtEmitter.emit];
|
|
353
334
|
}
|
|
354
335
|
|
|
355
|
-
// src/utils/
|
|
336
|
+
// src/utils/reusePrevIfEqual.ts
|
|
356
337
|
import { deepEqual as deepEqual2 } from "t-state";
|
|
357
338
|
function reusePrevIfEqual({
|
|
358
339
|
prev,
|
|
@@ -368,6 +349,8 @@ function reusePrevIfEqual({
|
|
|
368
349
|
}
|
|
369
350
|
|
|
370
351
|
// src/documentStore.ts
|
|
352
|
+
import { evtmitter as evtmitter2 } from "evtmitter";
|
|
353
|
+
import { useOnEvtmitterEvent as useOnEvtmitterEvent2 } from "evtmitter/react";
|
|
371
354
|
function newTSDFDocumentStore({
|
|
372
355
|
debugName,
|
|
373
356
|
fetchFn,
|
|
@@ -438,12 +421,15 @@ function newTSDFDocumentStore({
|
|
|
438
421
|
if (wasAborted) {
|
|
439
422
|
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
440
423
|
}
|
|
424
|
+
if (store.state.error) {
|
|
425
|
+
return { data: null, error: store.state.error };
|
|
426
|
+
}
|
|
441
427
|
if (!store.state.data) {
|
|
442
|
-
return { data: null, error: errorNormalizer(new Error("
|
|
428
|
+
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
443
429
|
}
|
|
444
|
-
return
|
|
430
|
+
return { data: store.state.data, error: null };
|
|
445
431
|
}
|
|
446
|
-
const storeEvents =
|
|
432
|
+
const storeEvents = evtmitter2();
|
|
447
433
|
let invalidationWasTriggered = false;
|
|
448
434
|
function invalidateData(priority = "highPriority") {
|
|
449
435
|
const currentInvalidationPriority = store.state.refetchOnMount ? fetchTypePriority[store.state.refetchOnMount] : -1;
|
|
@@ -485,7 +471,7 @@ function newTSDFDocumentStore({
|
|
|
485
471
|
const storeState = store.useSelector(storeStateSelector, {
|
|
486
472
|
useExternalDeps: true
|
|
487
473
|
});
|
|
488
|
-
|
|
474
|
+
useOnEvtmitterEvent2(storeEvents, "invalidateData", (priority) => {
|
|
489
475
|
if (!invalidationWasTriggered) {
|
|
490
476
|
store.setKey("refetchOnMount", false);
|
|
491
477
|
scheduleFetch(priority);
|
|
@@ -547,9 +533,11 @@ function newTSDFDocumentStore({
|
|
|
547
533
|
}
|
|
548
534
|
|
|
549
535
|
// src/collectionStore.ts
|
|
550
|
-
import
|
|
536
|
+
import { evtmitter as evtmitter3 } from "evtmitter";
|
|
537
|
+
import { useOnEvtmitterEvent as useOnEvtmitterEvent3 } from "evtmitter/react";
|
|
538
|
+
import { klona } from "klona/json";
|
|
551
539
|
import { useCallback as useCallback2, useEffect as useEffect3, useMemo as useMemo3 } from "react";
|
|
552
|
-
import {
|
|
540
|
+
import { Store as Store2, deepEqual as deepEqual3, useSubscribeToStore as useSubscribeToStore2 } from "t-state";
|
|
553
541
|
|
|
554
542
|
// src/collectionFetchOrquestrator.ts
|
|
555
543
|
function createCollectionFetchOrquestrator(props) {
|
|
@@ -598,7 +586,6 @@ function orderedProps(obj) {
|
|
|
598
586
|
}
|
|
599
587
|
|
|
600
588
|
// src/collectionStore.ts
|
|
601
|
-
import { klona } from "klona/json";
|
|
602
589
|
function newTSDFCollectionStore({
|
|
603
590
|
debugName,
|
|
604
591
|
fetchFn,
|
|
@@ -723,10 +710,13 @@ function newTSDFCollectionStore({
|
|
|
723
710
|
return { data: null, error: errorNormalizer(new Error("Aborted")) };
|
|
724
711
|
}
|
|
725
712
|
const item = store.state[itemId];
|
|
713
|
+
if (item?.error) {
|
|
714
|
+
return { data: null, error: item.error };
|
|
715
|
+
}
|
|
726
716
|
if (!item?.data) {
|
|
727
717
|
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
728
718
|
}
|
|
729
|
-
return
|
|
719
|
+
return { data: item.data, error: null };
|
|
730
720
|
}
|
|
731
721
|
function getItemsKeyArray(params) {
|
|
732
722
|
const items = store.state;
|
|
@@ -740,7 +730,7 @@ function newTSDFCollectionStore({
|
|
|
740
730
|
return [getItemKey(params)];
|
|
741
731
|
}
|
|
742
732
|
}
|
|
743
|
-
const storeEvents =
|
|
733
|
+
const storeEvents = evtmitter3();
|
|
744
734
|
const invalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
745
735
|
function invalidateItem(itemPayload, priority = "highPriority") {
|
|
746
736
|
const itemsKey = getItemsKeyArray(itemPayload);
|
|
@@ -846,7 +836,7 @@ function newTSDFCollectionStore({
|
|
|
846
836
|
equalityFn: deepEqual3,
|
|
847
837
|
useExternalDeps: true
|
|
848
838
|
});
|
|
849
|
-
|
|
839
|
+
useOnEvtmitterEvent3(storeEvents, "invalidateData", (event) => {
|
|
850
840
|
for (const { itemKey, payload } of queriesWithId) {
|
|
851
841
|
if (itemKey !== event.itemKey)
|
|
852
842
|
continue;
|
|
@@ -1003,10 +993,11 @@ function newTSDFCollectionStore({
|
|
|
1003
993
|
}
|
|
1004
994
|
|
|
1005
995
|
// src/listQueryStore.ts
|
|
1006
|
-
import
|
|
1007
|
-
import {
|
|
1008
|
-
import { deepEqual as deepEqual4, Store as Store3, useSubscribeToStore as useSubscribeToStore3 } from "t-state";
|
|
996
|
+
import { evtmitter as evtmitter4 } from "evtmitter";
|
|
997
|
+
import { useOnEvtmitterEvent as useOnEvtmitterEvent4 } from "evtmitter/react";
|
|
1009
998
|
import { klona as klona2 } from "klona/json";
|
|
999
|
+
import { useCallback as useCallback3, useEffect as useEffect4, useMemo as useMemo4 } from "react";
|
|
1000
|
+
import { Store as Store3, deepEqual as deepEqual4, useSubscribeToStore as useSubscribeToStore3 } from "t-state";
|
|
1010
1001
|
|
|
1011
1002
|
// src/utils/findAndMap.ts
|
|
1012
1003
|
function findAndMap(arr, findFn) {
|
|
@@ -1244,6 +1235,13 @@ function newTSDFListQueryStore({
|
|
|
1244
1235
|
};
|
|
1245
1236
|
}
|
|
1246
1237
|
const query = store.state.queries[queryKey];
|
|
1238
|
+
if (query?.error) {
|
|
1239
|
+
return {
|
|
1240
|
+
items: [],
|
|
1241
|
+
error: query.error,
|
|
1242
|
+
hasMore: query.hasMore
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1247
1245
|
if (!query) {
|
|
1248
1246
|
return {
|
|
1249
1247
|
items: [],
|
|
@@ -1251,7 +1249,7 @@ function newTSDFListQueryStore({
|
|
|
1251
1249
|
hasMore: false
|
|
1252
1250
|
};
|
|
1253
1251
|
}
|
|
1254
|
-
return
|
|
1252
|
+
return {
|
|
1255
1253
|
items: getQueryItems(query, (data, itemPayload) => ({
|
|
1256
1254
|
data,
|
|
1257
1255
|
itemPayload
|
|
@@ -1270,10 +1268,13 @@ function newTSDFListQueryStore({
|
|
|
1270
1268
|
}
|
|
1271
1269
|
const item = store.state.items[itemKey];
|
|
1272
1270
|
const itemQuery = store.state.itemQueries[itemKey];
|
|
1271
|
+
if (itemQuery?.error) {
|
|
1272
|
+
return { data: null, error: itemQuery.error };
|
|
1273
|
+
}
|
|
1273
1274
|
if (!itemQuery || !item) {
|
|
1274
1275
|
return { data: null, error: errorNormalizer(new Error("Not found")) };
|
|
1275
1276
|
}
|
|
1276
|
-
return
|
|
1277
|
+
return { data: item, error: null };
|
|
1277
1278
|
}
|
|
1278
1279
|
function getQueriesKeyArray(payloads) {
|
|
1279
1280
|
if (Array.isArray(payloads)) {
|
|
@@ -1295,7 +1296,7 @@ function newTSDFListQueryStore({
|
|
|
1295
1296
|
return [{ key: getQueryKey(payloads), payload: payloads }];
|
|
1296
1297
|
}
|
|
1297
1298
|
}
|
|
1298
|
-
const storeEvents =
|
|
1299
|
+
const storeEvents = evtmitter4();
|
|
1299
1300
|
const queryInvalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
1300
1301
|
function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
|
|
1301
1302
|
const queriesKey = getQueriesKeyArray(queryPayload);
|
|
@@ -1411,7 +1412,7 @@ function newTSDFListQueryStore({
|
|
|
1411
1412
|
equalityFn: deepEqual4,
|
|
1412
1413
|
useExternalDeps: true
|
|
1413
1414
|
});
|
|
1414
|
-
|
|
1415
|
+
useOnEvtmitterEvent4(storeEvents, "invalidateQuery", (event) => {
|
|
1415
1416
|
for (const { key, payload } of queriesWithId) {
|
|
1416
1417
|
if (key !== event.queryKey)
|
|
1417
1418
|
continue;
|
|
@@ -1730,7 +1731,7 @@ function newTSDFListQueryStore({
|
|
|
1730
1731
|
equalityFn: deepEqual4,
|
|
1731
1732
|
useExternalDeps: true
|
|
1732
1733
|
});
|
|
1733
|
-
|
|
1734
|
+
useOnEvtmitterEvent4(storeEvents, "invalidateItem", (event) => {
|
|
1734
1735
|
if (loadFromStateOnly)
|
|
1735
1736
|
return;
|
|
1736
1737
|
for (const { payload, itemKey } of memoizedItemKeys) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tsdf",
|
|
3
|
-
"version": "11.1.
|
|
3
|
+
"version": "11.1.5",
|
|
4
4
|
"description": "A data fetching solution based on t-state",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"author": "Lucas Santos",
|
|
@@ -64,9 +64,9 @@
|
|
|
64
64
|
"vitest": "^0.30.1"
|
|
65
65
|
},
|
|
66
66
|
"dependencies": {
|
|
67
|
+
"evtmitter": "^0.3.1",
|
|
67
68
|
"immer": "^9.0.17",
|
|
68
69
|
"klona": "^2.0.6",
|
|
69
|
-
"mitt": "^3.0.0",
|
|
70
70
|
"t-state": "^8.3.0"
|
|
71
71
|
},
|
|
72
72
|
"peerDependencies": {
|