tsdf 11.1.4 → 11.2.0
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 +91 -74
- package/dist/main.d.ts +4 -0
- package/dist/main.js +69 -42
- 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);
|
|
@@ -260,7 +250,20 @@ function createFetchOrquestrator({
|
|
|
260
250
|
}
|
|
261
251
|
return lastFetchWasAborted;
|
|
262
252
|
}
|
|
253
|
+
function reset() {
|
|
254
|
+
fetchs.a = null;
|
|
255
|
+
fetchs.c = null;
|
|
256
|
+
fetchs.b = null;
|
|
257
|
+
lastFetchStartTime = 0;
|
|
258
|
+
lastFetchDuration = 0;
|
|
259
|
+
lastFetchIdStarted = 0;
|
|
260
|
+
lastMutationIdStarted = 0;
|
|
261
|
+
mutationIsInProgress = false;
|
|
262
|
+
lastFetchWasAborted = false;
|
|
263
|
+
abortFetchsBeforeOrEqual = 0;
|
|
264
|
+
}
|
|
263
265
|
return {
|
|
266
|
+
reset,
|
|
264
267
|
scheduleFetch,
|
|
265
268
|
awaitFetch,
|
|
266
269
|
startMutation,
|
|
@@ -290,8 +293,9 @@ var fetchTypePriority = {
|
|
|
290
293
|
};
|
|
291
294
|
|
|
292
295
|
// src/useEnsureIsLoaded.ts
|
|
293
|
-
var
|
|
294
|
-
var import_react2 = require("react");
|
|
296
|
+
var import_evtmitter = require("evtmitter");
|
|
297
|
+
var import_react2 = require("evtmitter/react");
|
|
298
|
+
var import_react3 = require("react");
|
|
295
299
|
|
|
296
300
|
// src/utils/hooks.ts
|
|
297
301
|
var import_react = require("react");
|
|
@@ -308,26 +312,6 @@ function useDeepCompareMemoize(value, equalityFn = import_t_state.deepEqual) {
|
|
|
308
312
|
function useDeepMemo(callback, deeps) {
|
|
309
313
|
return (0, import_react.useMemo)(callback, useDeepCompareMemoize(deeps));
|
|
310
314
|
}
|
|
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
315
|
function usePrevious(value, initial) {
|
|
332
316
|
const ref = (0, import_react.useRef)(initial);
|
|
333
317
|
(0, import_react.useEffect)(() => {
|
|
@@ -358,8 +342,8 @@ function useConst(getValue) {
|
|
|
358
342
|
|
|
359
343
|
// src/useEnsureIsLoaded.ts
|
|
360
344
|
function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
361
|
-
const isLoadedEvtEmitter = useConst(() => (0,
|
|
362
|
-
const [isForceLoading, setIsForceLoading] = (0,
|
|
345
|
+
const isLoadedEvtEmitter = useConst(() => (0, import_evtmitter.evtmitter)());
|
|
346
|
+
const [isForceLoading, setIsForceLoading] = (0, import_react3.useState)(true);
|
|
363
347
|
useOnChange(
|
|
364
348
|
ensureIsLoaded && isForceLoading && enabled,
|
|
365
349
|
({ current }) => {
|
|
@@ -369,13 +353,13 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
369
353
|
},
|
|
370
354
|
{ callOnMount: true }
|
|
371
355
|
);
|
|
372
|
-
|
|
356
|
+
(0, import_react2.useOnEvtmitterEvent)(isLoadedEvtEmitter, "isLoaded", (isLoaded) => {
|
|
373
357
|
if (ensureIsLoaded && enabled && isLoaded) {
|
|
374
358
|
setIsForceLoading(false);
|
|
375
359
|
}
|
|
376
360
|
});
|
|
377
361
|
function useModifyResult(result) {
|
|
378
|
-
return (0,
|
|
362
|
+
return (0, import_react3.useMemo)(() => {
|
|
379
363
|
if (ensureIsLoaded) {
|
|
380
364
|
const newStatus = enabled && isForceLoading ? "loading" : result.status;
|
|
381
365
|
return {
|
|
@@ -390,7 +374,7 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
390
374
|
return [useModifyResult, isLoadedEvtEmitter.emit];
|
|
391
375
|
}
|
|
392
376
|
|
|
393
|
-
// src/utils/
|
|
377
|
+
// src/utils/reusePrevIfEqual.ts
|
|
394
378
|
var import_t_state2 = require("t-state");
|
|
395
379
|
function reusePrevIfEqual({
|
|
396
380
|
prev,
|
|
@@ -406,6 +390,8 @@ function reusePrevIfEqual({
|
|
|
406
390
|
}
|
|
407
391
|
|
|
408
392
|
// src/documentStore.ts
|
|
393
|
+
var import_evtmitter2 = require("evtmitter");
|
|
394
|
+
var import_react5 = require("evtmitter/react");
|
|
409
395
|
function newTSDFDocumentStore({
|
|
410
396
|
debugName,
|
|
411
397
|
fetchFn,
|
|
@@ -484,7 +470,7 @@ function newTSDFDocumentStore({
|
|
|
484
470
|
}
|
|
485
471
|
return { data: store.state.data, error: null };
|
|
486
472
|
}
|
|
487
|
-
const storeEvents = (0,
|
|
473
|
+
const storeEvents = (0, import_evtmitter2.evtmitter)();
|
|
488
474
|
let invalidationWasTriggered = false;
|
|
489
475
|
function invalidateData(priority = "highPriority") {
|
|
490
476
|
const currentInvalidationPriority = store.state.refetchOnMount ? fetchTypePriority[store.state.refetchOnMount] : -1;
|
|
@@ -503,7 +489,7 @@ function newTSDFDocumentStore({
|
|
|
503
489
|
returnIdleStatus = !!disabled,
|
|
504
490
|
ensureIsLoaded
|
|
505
491
|
} = {}) {
|
|
506
|
-
const storeStateSelector = (0,
|
|
492
|
+
const storeStateSelector = (0, import_react4.useCallback)(
|
|
507
493
|
(state) => {
|
|
508
494
|
const { error } = state;
|
|
509
495
|
const data = selector ? selector(state.data) : state.data;
|
|
@@ -526,14 +512,14 @@ function newTSDFDocumentStore({
|
|
|
526
512
|
const storeState = store.useSelector(storeStateSelector, {
|
|
527
513
|
useExternalDeps: true
|
|
528
514
|
});
|
|
529
|
-
|
|
515
|
+
(0, import_react5.useOnEvtmitterEvent)(storeEvents, "invalidateData", (priority) => {
|
|
530
516
|
if (!invalidationWasTriggered) {
|
|
531
517
|
store.setKey("refetchOnMount", false);
|
|
532
518
|
scheduleFetch(priority);
|
|
533
519
|
invalidationWasTriggered = true;
|
|
534
520
|
}
|
|
535
521
|
});
|
|
536
|
-
(0,
|
|
522
|
+
(0, import_react4.useEffect)(() => {
|
|
537
523
|
if (disabled)
|
|
538
524
|
return;
|
|
539
525
|
const fetchType = store.state.refetchOnMount || "lowPriority";
|
|
@@ -576,8 +562,18 @@ function newTSDFDocumentStore({
|
|
|
576
562
|
);
|
|
577
563
|
return true;
|
|
578
564
|
}
|
|
565
|
+
function reset() {
|
|
566
|
+
fetchOrquestrator.reset();
|
|
567
|
+
store.setState({
|
|
568
|
+
data: null,
|
|
569
|
+
error: null,
|
|
570
|
+
status: "idle",
|
|
571
|
+
refetchOnMount: "lowPriority"
|
|
572
|
+
});
|
|
573
|
+
}
|
|
579
574
|
return {
|
|
580
575
|
store,
|
|
576
|
+
reset,
|
|
581
577
|
awaitFetch,
|
|
582
578
|
scheduleFetch,
|
|
583
579
|
invalidateData,
|
|
@@ -588,8 +584,10 @@ function newTSDFDocumentStore({
|
|
|
588
584
|
}
|
|
589
585
|
|
|
590
586
|
// src/collectionStore.ts
|
|
591
|
-
var
|
|
592
|
-
var
|
|
587
|
+
var import_evtmitter3 = require("evtmitter");
|
|
588
|
+
var import_react6 = require("evtmitter/react");
|
|
589
|
+
var import_json = require("klona/json");
|
|
590
|
+
var import_react7 = require("react");
|
|
593
591
|
var import_t_state4 = require("t-state");
|
|
594
592
|
|
|
595
593
|
// src/collectionFetchOrquestrator.ts
|
|
@@ -601,8 +599,12 @@ function createCollectionFetchOrquestrator(props) {
|
|
|
601
599
|
}
|
|
602
600
|
return fetchOrquestrators.get(key);
|
|
603
601
|
}
|
|
602
|
+
function reset() {
|
|
603
|
+
fetchOrquestrators.clear();
|
|
604
|
+
}
|
|
604
605
|
return {
|
|
605
|
-
get: getFetchOrquestrator
|
|
606
|
+
get: getFetchOrquestrator,
|
|
607
|
+
reset
|
|
606
608
|
};
|
|
607
609
|
}
|
|
608
610
|
|
|
@@ -639,7 +641,6 @@ function orderedProps(obj) {
|
|
|
639
641
|
}
|
|
640
642
|
|
|
641
643
|
// src/collectionStore.ts
|
|
642
|
-
var import_json = require("klona/json");
|
|
643
644
|
function newTSDFCollectionStore({
|
|
644
645
|
debugName,
|
|
645
646
|
fetchFn,
|
|
@@ -784,7 +785,7 @@ function newTSDFCollectionStore({
|
|
|
784
785
|
return [getItemKey(params)];
|
|
785
786
|
}
|
|
786
787
|
}
|
|
787
|
-
const storeEvents = (0,
|
|
788
|
+
const storeEvents = (0, import_evtmitter3.evtmitter)();
|
|
788
789
|
const invalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
789
790
|
function invalidateItem(itemPayload, priority = "highPriority") {
|
|
790
791
|
const itemsKey = getItemsKeyArray(itemPayload);
|
|
@@ -830,12 +831,12 @@ function newTSDFCollectionStore({
|
|
|
830
831
|
return { itemKey: getItemKey(payload), payload };
|
|
831
832
|
});
|
|
832
833
|
}, [queries]);
|
|
833
|
-
const dataSelector = (0,
|
|
834
|
+
const dataSelector = (0, import_react7.useCallback)((itemState) => {
|
|
834
835
|
if (selector)
|
|
835
836
|
return selector(itemState);
|
|
836
837
|
return itemState;
|
|
837
838
|
}, []);
|
|
838
|
-
const resultSelector = (0,
|
|
839
|
+
const resultSelector = (0, import_react7.useCallback)(
|
|
839
840
|
(state) => {
|
|
840
841
|
return queriesWithId.map(
|
|
841
842
|
({
|
|
@@ -890,7 +891,7 @@ function newTSDFCollectionStore({
|
|
|
890
891
|
equalityFn: import_t_state4.deepEqual,
|
|
891
892
|
useExternalDeps: true
|
|
892
893
|
});
|
|
893
|
-
|
|
894
|
+
(0, import_react6.useOnEvtmitterEvent)(storeEvents, "invalidateData", (event) => {
|
|
894
895
|
for (const { itemKey, payload } of queriesWithId) {
|
|
895
896
|
if (itemKey !== event.itemKey)
|
|
896
897
|
continue;
|
|
@@ -906,7 +907,7 @@ function newTSDFCollectionStore({
|
|
|
906
907
|
}
|
|
907
908
|
}
|
|
908
909
|
});
|
|
909
|
-
(0,
|
|
910
|
+
(0, import_react7.useEffect)(() => {
|
|
910
911
|
for (const { itemKey: itemId, payload } of queriesWithId) {
|
|
911
912
|
if (itemId) {
|
|
912
913
|
const itemState = getItemState(payload);
|
|
@@ -927,12 +928,12 @@ function newTSDFCollectionStore({
|
|
|
927
928
|
}
|
|
928
929
|
function useItem(payload, options = {}) {
|
|
929
930
|
const { selector, ensureIsLoaded } = options;
|
|
930
|
-
const query = (0,
|
|
931
|
+
const query = (0, import_react7.useMemo)(
|
|
931
932
|
() => payload === false || payload === null || payload === void 0 ? [] : [payload],
|
|
932
933
|
[payload]
|
|
933
934
|
);
|
|
934
935
|
const item = useMultipleItems(query, options);
|
|
935
|
-
const result = (0,
|
|
936
|
+
const result = (0, import_react7.useMemo)(
|
|
936
937
|
() => item[0] ?? {
|
|
937
938
|
payload: void 0,
|
|
938
939
|
data: selector ? selector(null) : null,
|
|
@@ -1030,12 +1031,17 @@ function newTSDFCollectionStore({
|
|
|
1030
1031
|
);
|
|
1031
1032
|
return someItemWasUpdated;
|
|
1032
1033
|
}
|
|
1034
|
+
function reset() {
|
|
1035
|
+
fetchOrquestrator.reset();
|
|
1036
|
+
store.setState({});
|
|
1037
|
+
}
|
|
1033
1038
|
return {
|
|
1034
1039
|
store,
|
|
1035
1040
|
scheduleFetch,
|
|
1036
1041
|
awaitFetch,
|
|
1037
1042
|
useMultipleItems,
|
|
1038
1043
|
useItem,
|
|
1044
|
+
reset,
|
|
1039
1045
|
getItemKey,
|
|
1040
1046
|
getItemState,
|
|
1041
1047
|
startMutation,
|
|
@@ -1047,10 +1053,11 @@ function newTSDFCollectionStore({
|
|
|
1047
1053
|
}
|
|
1048
1054
|
|
|
1049
1055
|
// src/listQueryStore.ts
|
|
1050
|
-
var
|
|
1051
|
-
var
|
|
1052
|
-
var import_t_state5 = require("t-state");
|
|
1056
|
+
var import_evtmitter4 = require("evtmitter");
|
|
1057
|
+
var import_react8 = require("evtmitter/react");
|
|
1053
1058
|
var import_json2 = require("klona/json");
|
|
1059
|
+
var import_react9 = require("react");
|
|
1060
|
+
var import_t_state5 = require("t-state");
|
|
1054
1061
|
|
|
1055
1062
|
// src/utils/findAndMap.ts
|
|
1056
1063
|
function findAndMap(arr, findFn) {
|
|
@@ -1349,7 +1356,7 @@ function newTSDFListQueryStore({
|
|
|
1349
1356
|
return [{ key: getQueryKey(payloads), payload: payloads }];
|
|
1350
1357
|
}
|
|
1351
1358
|
}
|
|
1352
|
-
const storeEvents = (0,
|
|
1359
|
+
const storeEvents = (0, import_evtmitter4.evtmitter)();
|
|
1353
1360
|
const queryInvalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
1354
1361
|
function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
|
|
1355
1362
|
const queriesKey = getQueriesKeyArray(queryPayload);
|
|
@@ -1415,8 +1422,8 @@ function newTSDFListQueryStore({
|
|
|
1415
1422
|
};
|
|
1416
1423
|
});
|
|
1417
1424
|
}, [queries]);
|
|
1418
|
-
const dataSelector = (0,
|
|
1419
|
-
const resultSelector = (0,
|
|
1425
|
+
const dataSelector = (0, import_react9.useCallback)(itemSelector, []);
|
|
1426
|
+
const resultSelector = (0, import_react9.useCallback)(
|
|
1420
1427
|
(state) => {
|
|
1421
1428
|
return queriesWithId.map(
|
|
1422
1429
|
({
|
|
@@ -1465,7 +1472,7 @@ function newTSDFListQueryStore({
|
|
|
1465
1472
|
equalityFn: import_t_state5.deepEqual,
|
|
1466
1473
|
useExternalDeps: true
|
|
1467
1474
|
});
|
|
1468
|
-
|
|
1475
|
+
(0, import_react8.useOnEvtmitterEvent)(storeEvents, "invalidateQuery", (event) => {
|
|
1469
1476
|
for (const { key, payload } of queriesWithId) {
|
|
1470
1477
|
if (key !== event.queryKey)
|
|
1471
1478
|
continue;
|
|
@@ -1482,7 +1489,7 @@ function newTSDFListQueryStore({
|
|
|
1482
1489
|
}
|
|
1483
1490
|
});
|
|
1484
1491
|
const loadSizeConst = useConst(() => loadSize);
|
|
1485
|
-
(0,
|
|
1492
|
+
(0, import_react9.useEffect)(() => {
|
|
1486
1493
|
for (const { key: itemId, payload: fetchParams } of queriesWithId) {
|
|
1487
1494
|
if (itemId) {
|
|
1488
1495
|
const itemState = getQueryState(fetchParams);
|
|
@@ -1503,12 +1510,12 @@ function newTSDFListQueryStore({
|
|
|
1503
1510
|
}
|
|
1504
1511
|
function useListQuery(payload, options = {}) {
|
|
1505
1512
|
const { ensureIsLoaded } = options;
|
|
1506
|
-
const query = (0,
|
|
1513
|
+
const query = (0, import_react9.useMemo)(
|
|
1507
1514
|
() => payload === false || payload === null || payload === void 0 ? [] : [payload],
|
|
1508
1515
|
[payload]
|
|
1509
1516
|
);
|
|
1510
1517
|
const queryResult = useMultipleListQueries(query, options);
|
|
1511
|
-
const result = (0,
|
|
1518
|
+
const result = (0, import_react9.useMemo)(
|
|
1512
1519
|
() => queryResult[0] ?? {
|
|
1513
1520
|
payload: void 0,
|
|
1514
1521
|
error: null,
|
|
@@ -1722,7 +1729,7 @@ function newTSDFListQueryStore({
|
|
|
1722
1729
|
disableRefetchOnMount,
|
|
1723
1730
|
loadFromStateOnly
|
|
1724
1731
|
} = {}) {
|
|
1725
|
-
const dataSelector = (0,
|
|
1732
|
+
const dataSelector = (0, import_react9.useCallback)(selector, []);
|
|
1726
1733
|
const memoizedItemKeys = useDeepMemo(
|
|
1727
1734
|
() => itemsPayload_.map(
|
|
1728
1735
|
(itemPayload) => ({
|
|
@@ -1732,7 +1739,7 @@ function newTSDFListQueryStore({
|
|
|
1732
1739
|
),
|
|
1733
1740
|
[itemsPayload_]
|
|
1734
1741
|
);
|
|
1735
|
-
const resultSelector = (0,
|
|
1742
|
+
const resultSelector = (0, import_react9.useCallback)(
|
|
1736
1743
|
(state) => {
|
|
1737
1744
|
return memoizedItemKeys.map(
|
|
1738
1745
|
({
|
|
@@ -1784,7 +1791,7 @@ function newTSDFListQueryStore({
|
|
|
1784
1791
|
equalityFn: import_t_state5.deepEqual,
|
|
1785
1792
|
useExternalDeps: true
|
|
1786
1793
|
});
|
|
1787
|
-
|
|
1794
|
+
(0, import_react8.useOnEvtmitterEvent)(storeEvents, "invalidateItem", (event) => {
|
|
1788
1795
|
if (loadFromStateOnly)
|
|
1789
1796
|
return;
|
|
1790
1797
|
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
@@ -1802,7 +1809,7 @@ function newTSDFListQueryStore({
|
|
|
1802
1809
|
}
|
|
1803
1810
|
}
|
|
1804
1811
|
});
|
|
1805
|
-
(0,
|
|
1812
|
+
(0, import_react9.useEffect)(() => {
|
|
1806
1813
|
if (loadFromStateOnly)
|
|
1807
1814
|
return;
|
|
1808
1815
|
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
@@ -1825,16 +1832,16 @@ function newTSDFListQueryStore({
|
|
|
1825
1832
|
}
|
|
1826
1833
|
function useItem(itemPayload, options = {}) {
|
|
1827
1834
|
const { ensureIsLoaded } = options;
|
|
1828
|
-
const query = (0,
|
|
1835
|
+
const query = (0, import_react9.useMemo)(
|
|
1829
1836
|
() => itemPayload === false || itemPayload === null || itemPayload === void 0 ? [] : [itemPayload],
|
|
1830
1837
|
[itemPayload]
|
|
1831
1838
|
);
|
|
1832
1839
|
const queryResult = useMultipleItems(query, options);
|
|
1833
|
-
const memoizedSelector = (0,
|
|
1840
|
+
const memoizedSelector = (0, import_react9.useCallback)(
|
|
1834
1841
|
options.selector ?? defaultItemDataSelector,
|
|
1835
1842
|
[]
|
|
1836
1843
|
);
|
|
1837
|
-
const result = (0,
|
|
1844
|
+
const result = (0, import_react9.useMemo)(
|
|
1838
1845
|
() => queryResult[0] ?? {
|
|
1839
1846
|
error: null,
|
|
1840
1847
|
isLoading: false,
|
|
@@ -1974,8 +1981,18 @@ function newTSDFListQueryStore({
|
|
|
1974
1981
|
{ action: { type: "delete-item-state", itemId } }
|
|
1975
1982
|
);
|
|
1976
1983
|
}
|
|
1984
|
+
function reset() {
|
|
1985
|
+
fetchItemOrquestrator?.reset();
|
|
1986
|
+
fetchQueryOrquestrator.reset();
|
|
1987
|
+
store.setState({
|
|
1988
|
+
items: {},
|
|
1989
|
+
queries: {},
|
|
1990
|
+
itemQueries: {}
|
|
1991
|
+
});
|
|
1992
|
+
}
|
|
1977
1993
|
return {
|
|
1978
1994
|
store,
|
|
1995
|
+
reset,
|
|
1979
1996
|
scheduleListQueryFetch,
|
|
1980
1997
|
getQueryState,
|
|
1981
1998
|
getQueryKey,
|
package/dist/main.d.ts
CHANGED
|
@@ -32,6 +32,7 @@ declare function newTSDFDocumentStore<State extends ValidStoreState, NError>({ d
|
|
|
32
32
|
dynamicRealtimeThrottleMs?: (lastFetchDuration: number) => number;
|
|
33
33
|
}): {
|
|
34
34
|
store: Store<TSDFDocumentStoreState<State, NError>>;
|
|
35
|
+
reset: () => void;
|
|
35
36
|
awaitFetch: () => Promise<{
|
|
36
37
|
data: State;
|
|
37
38
|
error: null;
|
|
@@ -113,7 +114,9 @@ declare function newTSDFCollectionStore<ItemState extends ValidStoreState, NErro
|
|
|
113
114
|
returnRefetchingStatus?: boolean | undefined;
|
|
114
115
|
disableRefetchOnMount?: boolean | undefined;
|
|
115
116
|
ensureIsLoaded?: boolean | undefined;
|
|
117
|
+
returnIdleStatus?: boolean | undefined;
|
|
116
118
|
}) => TSFDUseCollectionItemReturn<Selected_1, ItemPayload, NError>;
|
|
119
|
+
reset: () => void;
|
|
117
120
|
getItemKey: (params: ItemPayload) => string;
|
|
118
121
|
getItemState: {
|
|
119
122
|
(fetchParam: ItemPayload): TSFDCollectionItem<ItemState, ItemPayload, NError> | null | undefined;
|
|
@@ -203,6 +206,7 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
|
|
|
203
206
|
dynamicRealtimeThrottleMs?: (lastFetchDuration: number) => number;
|
|
204
207
|
}): {
|
|
205
208
|
store: Store<TSFDListQueryState<ItemState, NError, QueryPayload, ItemPayload>>;
|
|
209
|
+
reset: () => void;
|
|
206
210
|
scheduleListQueryFetch: {
|
|
207
211
|
(fetchType: FetchType, payload: QueryPayload, size?: number): ScheduleFetchResults;
|
|
208
212
|
(fetchType: FetchType, payload: QueryPayload[], size?: number): ScheduleFetchResults[];
|
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);
|
|
@@ -222,7 +222,20 @@ function createFetchOrquestrator({
|
|
|
222
222
|
}
|
|
223
223
|
return lastFetchWasAborted;
|
|
224
224
|
}
|
|
225
|
+
function reset() {
|
|
226
|
+
fetchs.a = null;
|
|
227
|
+
fetchs.c = null;
|
|
228
|
+
fetchs.b = null;
|
|
229
|
+
lastFetchStartTime = 0;
|
|
230
|
+
lastFetchDuration = 0;
|
|
231
|
+
lastFetchIdStarted = 0;
|
|
232
|
+
lastMutationIdStarted = 0;
|
|
233
|
+
mutationIsInProgress = false;
|
|
234
|
+
lastFetchWasAborted = false;
|
|
235
|
+
abortFetchsBeforeOrEqual = 0;
|
|
236
|
+
}
|
|
225
237
|
return {
|
|
238
|
+
reset,
|
|
226
239
|
scheduleFetch,
|
|
227
240
|
awaitFetch,
|
|
228
241
|
startMutation,
|
|
@@ -252,7 +265,8 @@ var fetchTypePriority = {
|
|
|
252
265
|
};
|
|
253
266
|
|
|
254
267
|
// src/useEnsureIsLoaded.ts
|
|
255
|
-
import
|
|
268
|
+
import { evtmitter } from "evtmitter";
|
|
269
|
+
import { useOnEvtmitterEvent } from "evtmitter/react";
|
|
256
270
|
import { useMemo as useMemo2, useState } from "react";
|
|
257
271
|
|
|
258
272
|
// src/utils/hooks.ts
|
|
@@ -270,26 +284,6 @@ function useDeepCompareMemoize(value, equalityFn = deepEqual) {
|
|
|
270
284
|
function useDeepMemo(callback, deeps) {
|
|
271
285
|
return useMemo(callback, useDeepCompareMemoize(deeps));
|
|
272
286
|
}
|
|
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
287
|
function usePrevious(value, initial) {
|
|
294
288
|
const ref = useRef(initial);
|
|
295
289
|
useEffect(() => {
|
|
@@ -320,7 +314,7 @@ function useConst(getValue) {
|
|
|
320
314
|
|
|
321
315
|
// src/useEnsureIsLoaded.ts
|
|
322
316
|
function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
323
|
-
const isLoadedEvtEmitter = useConst(() =>
|
|
317
|
+
const isLoadedEvtEmitter = useConst(() => evtmitter());
|
|
324
318
|
const [isForceLoading, setIsForceLoading] = useState(true);
|
|
325
319
|
useOnChange(
|
|
326
320
|
ensureIsLoaded && isForceLoading && enabled,
|
|
@@ -331,7 +325,7 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
331
325
|
},
|
|
332
326
|
{ callOnMount: true }
|
|
333
327
|
);
|
|
334
|
-
|
|
328
|
+
useOnEvtmitterEvent(isLoadedEvtEmitter, "isLoaded", (isLoaded) => {
|
|
335
329
|
if (ensureIsLoaded && enabled && isLoaded) {
|
|
336
330
|
setIsForceLoading(false);
|
|
337
331
|
}
|
|
@@ -352,7 +346,7 @@ function useEnsureIsLoaded(ensureIsLoaded, enabled, forceFetch) {
|
|
|
352
346
|
return [useModifyResult, isLoadedEvtEmitter.emit];
|
|
353
347
|
}
|
|
354
348
|
|
|
355
|
-
// src/utils/
|
|
349
|
+
// src/utils/reusePrevIfEqual.ts
|
|
356
350
|
import { deepEqual as deepEqual2 } from "t-state";
|
|
357
351
|
function reusePrevIfEqual({
|
|
358
352
|
prev,
|
|
@@ -368,6 +362,8 @@ function reusePrevIfEqual({
|
|
|
368
362
|
}
|
|
369
363
|
|
|
370
364
|
// src/documentStore.ts
|
|
365
|
+
import { evtmitter as evtmitter2 } from "evtmitter";
|
|
366
|
+
import { useOnEvtmitterEvent as useOnEvtmitterEvent2 } from "evtmitter/react";
|
|
371
367
|
function newTSDFDocumentStore({
|
|
372
368
|
debugName,
|
|
373
369
|
fetchFn,
|
|
@@ -446,7 +442,7 @@ function newTSDFDocumentStore({
|
|
|
446
442
|
}
|
|
447
443
|
return { data: store.state.data, error: null };
|
|
448
444
|
}
|
|
449
|
-
const storeEvents =
|
|
445
|
+
const storeEvents = evtmitter2();
|
|
450
446
|
let invalidationWasTriggered = false;
|
|
451
447
|
function invalidateData(priority = "highPriority") {
|
|
452
448
|
const currentInvalidationPriority = store.state.refetchOnMount ? fetchTypePriority[store.state.refetchOnMount] : -1;
|
|
@@ -488,7 +484,7 @@ function newTSDFDocumentStore({
|
|
|
488
484
|
const storeState = store.useSelector(storeStateSelector, {
|
|
489
485
|
useExternalDeps: true
|
|
490
486
|
});
|
|
491
|
-
|
|
487
|
+
useOnEvtmitterEvent2(storeEvents, "invalidateData", (priority) => {
|
|
492
488
|
if (!invalidationWasTriggered) {
|
|
493
489
|
store.setKey("refetchOnMount", false);
|
|
494
490
|
scheduleFetch(priority);
|
|
@@ -538,8 +534,18 @@ function newTSDFDocumentStore({
|
|
|
538
534
|
);
|
|
539
535
|
return true;
|
|
540
536
|
}
|
|
537
|
+
function reset() {
|
|
538
|
+
fetchOrquestrator.reset();
|
|
539
|
+
store.setState({
|
|
540
|
+
data: null,
|
|
541
|
+
error: null,
|
|
542
|
+
status: "idle",
|
|
543
|
+
refetchOnMount: "lowPriority"
|
|
544
|
+
});
|
|
545
|
+
}
|
|
541
546
|
return {
|
|
542
547
|
store,
|
|
548
|
+
reset,
|
|
543
549
|
awaitFetch,
|
|
544
550
|
scheduleFetch,
|
|
545
551
|
invalidateData,
|
|
@@ -550,9 +556,11 @@ function newTSDFDocumentStore({
|
|
|
550
556
|
}
|
|
551
557
|
|
|
552
558
|
// src/collectionStore.ts
|
|
553
|
-
import
|
|
559
|
+
import { evtmitter as evtmitter3 } from "evtmitter";
|
|
560
|
+
import { useOnEvtmitterEvent as useOnEvtmitterEvent3 } from "evtmitter/react";
|
|
561
|
+
import { klona } from "klona/json";
|
|
554
562
|
import { useCallback as useCallback2, useEffect as useEffect3, useMemo as useMemo3 } from "react";
|
|
555
|
-
import {
|
|
563
|
+
import { Store as Store2, deepEqual as deepEqual3, useSubscribeToStore as useSubscribeToStore2 } from "t-state";
|
|
556
564
|
|
|
557
565
|
// src/collectionFetchOrquestrator.ts
|
|
558
566
|
function createCollectionFetchOrquestrator(props) {
|
|
@@ -563,8 +571,12 @@ function createCollectionFetchOrquestrator(props) {
|
|
|
563
571
|
}
|
|
564
572
|
return fetchOrquestrators.get(key);
|
|
565
573
|
}
|
|
574
|
+
function reset() {
|
|
575
|
+
fetchOrquestrators.clear();
|
|
576
|
+
}
|
|
566
577
|
return {
|
|
567
|
-
get: getFetchOrquestrator
|
|
578
|
+
get: getFetchOrquestrator,
|
|
579
|
+
reset
|
|
568
580
|
};
|
|
569
581
|
}
|
|
570
582
|
|
|
@@ -601,7 +613,6 @@ function orderedProps(obj) {
|
|
|
601
613
|
}
|
|
602
614
|
|
|
603
615
|
// src/collectionStore.ts
|
|
604
|
-
import { klona } from "klona/json";
|
|
605
616
|
function newTSDFCollectionStore({
|
|
606
617
|
debugName,
|
|
607
618
|
fetchFn,
|
|
@@ -746,7 +757,7 @@ function newTSDFCollectionStore({
|
|
|
746
757
|
return [getItemKey(params)];
|
|
747
758
|
}
|
|
748
759
|
}
|
|
749
|
-
const storeEvents =
|
|
760
|
+
const storeEvents = evtmitter3();
|
|
750
761
|
const invalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
751
762
|
function invalidateItem(itemPayload, priority = "highPriority") {
|
|
752
763
|
const itemsKey = getItemsKeyArray(itemPayload);
|
|
@@ -852,7 +863,7 @@ function newTSDFCollectionStore({
|
|
|
852
863
|
equalityFn: deepEqual3,
|
|
853
864
|
useExternalDeps: true
|
|
854
865
|
});
|
|
855
|
-
|
|
866
|
+
useOnEvtmitterEvent3(storeEvents, "invalidateData", (event) => {
|
|
856
867
|
for (const { itemKey, payload } of queriesWithId) {
|
|
857
868
|
if (itemKey !== event.itemKey)
|
|
858
869
|
continue;
|
|
@@ -992,12 +1003,17 @@ function newTSDFCollectionStore({
|
|
|
992
1003
|
);
|
|
993
1004
|
return someItemWasUpdated;
|
|
994
1005
|
}
|
|
1006
|
+
function reset() {
|
|
1007
|
+
fetchOrquestrator.reset();
|
|
1008
|
+
store.setState({});
|
|
1009
|
+
}
|
|
995
1010
|
return {
|
|
996
1011
|
store,
|
|
997
1012
|
scheduleFetch,
|
|
998
1013
|
awaitFetch,
|
|
999
1014
|
useMultipleItems,
|
|
1000
1015
|
useItem,
|
|
1016
|
+
reset,
|
|
1001
1017
|
getItemKey,
|
|
1002
1018
|
getItemState,
|
|
1003
1019
|
startMutation,
|
|
@@ -1009,10 +1025,11 @@ function newTSDFCollectionStore({
|
|
|
1009
1025
|
}
|
|
1010
1026
|
|
|
1011
1027
|
// src/listQueryStore.ts
|
|
1012
|
-
import
|
|
1013
|
-
import {
|
|
1014
|
-
import { deepEqual as deepEqual4, Store as Store3, useSubscribeToStore as useSubscribeToStore3 } from "t-state";
|
|
1028
|
+
import { evtmitter as evtmitter4 } from "evtmitter";
|
|
1029
|
+
import { useOnEvtmitterEvent as useOnEvtmitterEvent4 } from "evtmitter/react";
|
|
1015
1030
|
import { klona as klona2 } from "klona/json";
|
|
1031
|
+
import { useCallback as useCallback3, useEffect as useEffect4, useMemo as useMemo4 } from "react";
|
|
1032
|
+
import { Store as Store3, deepEqual as deepEqual4, useSubscribeToStore as useSubscribeToStore3 } from "t-state";
|
|
1016
1033
|
|
|
1017
1034
|
// src/utils/findAndMap.ts
|
|
1018
1035
|
function findAndMap(arr, findFn) {
|
|
@@ -1311,7 +1328,7 @@ function newTSDFListQueryStore({
|
|
|
1311
1328
|
return [{ key: getQueryKey(payloads), payload: payloads }];
|
|
1312
1329
|
}
|
|
1313
1330
|
}
|
|
1314
|
-
const storeEvents =
|
|
1331
|
+
const storeEvents = evtmitter4();
|
|
1315
1332
|
const queryInvalidationWasTriggered = /* @__PURE__ */ new Set();
|
|
1316
1333
|
function invalidateQuery(queryPayload, priority = "highPriority", ignoreInvalidationSync = false) {
|
|
1317
1334
|
const queriesKey = getQueriesKeyArray(queryPayload);
|
|
@@ -1427,7 +1444,7 @@ function newTSDFListQueryStore({
|
|
|
1427
1444
|
equalityFn: deepEqual4,
|
|
1428
1445
|
useExternalDeps: true
|
|
1429
1446
|
});
|
|
1430
|
-
|
|
1447
|
+
useOnEvtmitterEvent4(storeEvents, "invalidateQuery", (event) => {
|
|
1431
1448
|
for (const { key, payload } of queriesWithId) {
|
|
1432
1449
|
if (key !== event.queryKey)
|
|
1433
1450
|
continue;
|
|
@@ -1746,7 +1763,7 @@ function newTSDFListQueryStore({
|
|
|
1746
1763
|
equalityFn: deepEqual4,
|
|
1747
1764
|
useExternalDeps: true
|
|
1748
1765
|
});
|
|
1749
|
-
|
|
1766
|
+
useOnEvtmitterEvent4(storeEvents, "invalidateItem", (event) => {
|
|
1750
1767
|
if (loadFromStateOnly)
|
|
1751
1768
|
return;
|
|
1752
1769
|
for (const { payload, itemKey } of memoizedItemKeys) {
|
|
@@ -1936,8 +1953,18 @@ function newTSDFListQueryStore({
|
|
|
1936
1953
|
{ action: { type: "delete-item-state", itemId } }
|
|
1937
1954
|
);
|
|
1938
1955
|
}
|
|
1956
|
+
function reset() {
|
|
1957
|
+
fetchItemOrquestrator?.reset();
|
|
1958
|
+
fetchQueryOrquestrator.reset();
|
|
1959
|
+
store.setState({
|
|
1960
|
+
items: {},
|
|
1961
|
+
queries: {},
|
|
1962
|
+
itemQueries: {}
|
|
1963
|
+
});
|
|
1964
|
+
}
|
|
1939
1965
|
return {
|
|
1940
1966
|
store,
|
|
1967
|
+
reset,
|
|
1941
1968
|
scheduleListQueryFetch,
|
|
1942
1969
|
getQueryState,
|
|
1943
1970
|
getQueryKey,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tsdf",
|
|
3
|
-
"version": "11.
|
|
3
|
+
"version": "11.2.0",
|
|
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": {
|