tsdf 8.0.0-beta.8 → 8.0.0-beta.9

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 CHANGED
@@ -380,6 +380,7 @@ function newTSDFDocumentStore({
380
380
  lowPriorityThrottleMs,
381
381
  mediumPriorityThrottleMs,
382
382
  dynamicRealtimeThrottleMs,
383
+ disableInitialDataInvalidation,
383
384
  errorNormalizer
384
385
  }) {
385
386
  const store = new import_t_state2.Store({
@@ -388,7 +389,7 @@ function newTSDFDocumentStore({
388
389
  data: initialData ?? null,
389
390
  error: null,
390
391
  status: initialData ? "success" : "idle",
391
- refetchOnMount: false
392
+ refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
392
393
  }
393
394
  });
394
395
  async function fetch(fetchCtx, _) {
@@ -608,14 +609,30 @@ function newTSDFCollectionStore({
608
609
  disableRefetchOnWindowFocus,
609
610
  lowPriorityThrottleMs,
610
611
  errorNormalizer,
612
+ disableInitialDataInvalidation,
611
613
  mediumPriorityThrottleMs,
614
+ initialStateItems,
612
615
  disableRefetchOnMount: globalDisableRefetchOnMount,
613
616
  dynamicRealtimeThrottleMs,
614
617
  getCollectionItemKey: filterCollectionItemObjKey
615
618
  }) {
619
+ const initialState = {};
620
+ if (initialStateItems) {
621
+ for (const item of initialStateItems) {
622
+ const itemKey = getItemKey(item.payload);
623
+ initialState[itemKey] = {
624
+ data: item.data,
625
+ error: null,
626
+ status: "success",
627
+ payload: item.payload,
628
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
629
+ wasLoaded: true
630
+ };
631
+ }
632
+ }
616
633
  const store = new import_t_state3.Store({
617
634
  debugName,
618
- state: {}
635
+ state: initialState
619
636
  });
620
637
  function getItemKey(params) {
621
638
  return getCacheId(
@@ -998,16 +1015,44 @@ function newTSDFListQueryStore({
998
1015
  fetchItemFn,
999
1016
  errorNormalizer,
1000
1017
  defaultQuerySize = 50,
1018
+ initialData,
1001
1019
  disableRefetchOnWindowFocus,
1002
1020
  disableRefetchOnMount: globalDisableRefetchOnMount,
1003
1021
  lowPriorityThrottleMs,
1022
+ disableInitialDataInvalidation,
1004
1023
  mediumPriorityThrottleMs,
1005
1024
  dynamicRealtimeThrottleMs,
1006
1025
  syncMutationsAndInvalidations
1007
1026
  }) {
1027
+ const initialState = { items: {}, queries: {}, itemQueries: {} };
1028
+ if (initialData) {
1029
+ for (const { payload, data } of initialData.items) {
1030
+ const itemKey = getItemKey(payload);
1031
+ initialState.items[itemKey] = data;
1032
+ initialState.itemQueries[itemKey] = {
1033
+ error: null,
1034
+ status: "success",
1035
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1036
+ wasLoaded: true,
1037
+ payload
1038
+ };
1039
+ }
1040
+ for (const { payload, items, hasMore } of initialData.queries) {
1041
+ const queryKey = getQueryKey(payload);
1042
+ initialState.queries[queryKey] = {
1043
+ error: null,
1044
+ status: "success",
1045
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1046
+ wasLoaded: true,
1047
+ payload,
1048
+ items: items.map(getItemKey),
1049
+ hasMore
1050
+ };
1051
+ }
1052
+ }
1008
1053
  const store = new import_t_state4.Store({
1009
1054
  debugName,
1010
- state: { items: {}, queries: {}, itemQueries: {} }
1055
+ state: initialState
1011
1056
  });
1012
1057
  function getQueryKey(params) {
1013
1058
  return getCacheId(params);
package/dist/main.d.ts CHANGED
@@ -20,10 +20,11 @@ type TSDFUseDocumentReturn<Selected, NError> = {
20
20
  error: NError | null;
21
21
  isLoading: boolean;
22
22
  };
23
- declare function newTSDFDocumentStore<State extends ValidStoreState, NError>({ debugName, fetchFn, initialData, disableRefetchOnWindowFocus, disableRefetchOnMount: globalDisableRefetchOnMount, lowPriorityThrottleMs, mediumPriorityThrottleMs, dynamicRealtimeThrottleMs, errorNormalizer, }: {
23
+ declare function newTSDFDocumentStore<State extends ValidStoreState, NError>({ debugName, fetchFn, initialData, disableRefetchOnWindowFocus, disableRefetchOnMount: globalDisableRefetchOnMount, lowPriorityThrottleMs, mediumPriorityThrottleMs, dynamicRealtimeThrottleMs, disableInitialDataInvalidation, errorNormalizer, }: {
24
24
  debugName?: string;
25
25
  fetchFn: () => Promise<State>;
26
26
  initialData?: State;
27
+ disableInitialDataInvalidation?: boolean;
27
28
  errorNormalizer: (exception: unknown) => NError;
28
29
  disableRefetchOnWindowFocus?: boolean;
29
30
  disableRefetchOnMount?: boolean;
@@ -72,13 +73,19 @@ type TSFDUseCollectionItemReturn<Selected, ItemPayload, NError> = {
72
73
  itemStateKey: string;
73
74
  isLoading: boolean;
74
75
  };
75
- declare function newTSDFCollectionStore<ItemState extends ValidStoreState, NError, ItemPayload extends ValidPayload>({ debugName, fetchFn, disableRefetchOnWindowFocus, lowPriorityThrottleMs, errorNormalizer, mediumPriorityThrottleMs, disableRefetchOnMount: globalDisableRefetchOnMount, dynamicRealtimeThrottleMs, getCollectionItemKey: filterCollectionItemObjKey, }: {
76
+ type CollectionInitialStateItem<ItemPayload extends ValidPayload, ItemState extends ValidStoreState> = {
77
+ payload: ItemPayload;
78
+ data: ItemState;
79
+ };
80
+ declare function newTSDFCollectionStore<ItemState extends ValidStoreState, NError, ItemPayload extends ValidPayload>({ debugName, fetchFn, disableRefetchOnWindowFocus, lowPriorityThrottleMs, errorNormalizer, disableInitialDataInvalidation, mediumPriorityThrottleMs, initialStateItems, disableRefetchOnMount: globalDisableRefetchOnMount, dynamicRealtimeThrottleMs, getCollectionItemKey: filterCollectionItemObjKey, }: {
76
81
  debugName?: string;
77
82
  fetchFn: (params: ItemPayload) => Promise<ItemState>;
78
83
  getCollectionItemKey?: (params: ItemPayload) => ValidPayload | any[];
79
84
  errorNormalizer: (exception: unknown) => NError;
80
85
  disableRefetchOnWindowFocus?: boolean;
81
86
  disableRefetchOnMount?: boolean;
87
+ initialStateItems?: CollectionInitialStateItem<ItemPayload, ItemState>[];
88
+ disableInitialDataInvalidation?: boolean;
82
89
  lowPriorityThrottleMs?: number;
83
90
  mediumPriorityThrottleMs?: number;
84
91
  dynamicRealtimeThrottleMs?: (lastFetchDuration: number) => number;
@@ -167,12 +174,25 @@ type FetchListFnReturn<ItemState extends ValidStoreState, ItemPayload extends Va
167
174
  }[];
168
175
  hasMore: boolean;
169
176
  };
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, }: {
177
+ type ListQueryStoreInitialData<ItemState extends ValidStoreState, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = {
178
+ items: {
179
+ payload: ItemPayload;
180
+ data: ItemState;
181
+ }[];
182
+ queries: {
183
+ payload: QueryPayload;
184
+ items: ItemPayload[];
185
+ hasMore: boolean;
186
+ }[];
187
+ };
188
+ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload>({ debugName, fetchListFn, fetchItemFn, errorNormalizer, defaultQuerySize, initialData, disableRefetchOnWindowFocus, disableRefetchOnMount: globalDisableRefetchOnMount, lowPriorityThrottleMs, disableInitialDataInvalidation, mediumPriorityThrottleMs, dynamicRealtimeThrottleMs, syncMutationsAndInvalidations, }: {
171
189
  debugName?: string;
172
190
  fetchListFn: (payload: QueryPayload, size: number) => Promise<FetchListFnReturn<ItemState, ItemPayload>>;
173
191
  fetchItemFn?: (itemId: ItemPayload) => Promise<ItemState>;
174
192
  errorNormalizer: (exception: unknown) => NError;
175
193
  defaultQuerySize?: number;
194
+ initialData?: ListQueryStoreInitialData<ItemState, QueryPayload, ItemPayload>;
195
+ disableInitialDataInvalidation?: boolean;
176
196
  disableRefetchOnWindowFocus?: boolean;
177
197
  syncMutationsAndInvalidations?: {
178
198
  syncQueries: (query1: QueryPayload, query2: QueryPayload) => boolean;
@@ -190,9 +210,9 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
190
210
  };
191
211
  getQueryState: (params: QueryPayload) => TSFDListQuery<NError, QueryPayload> | undefined;
192
212
  getQueryKey: (params: QueryPayload) => string;
193
- deleteItemState: (itemId: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[]) => void;
213
+ deleteItemState: (itemId: ItemPayload | ItemPayload[] | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean)) => void;
194
214
  getItemState: {
195
- (itemPayload: ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[]): {
215
+ (itemPayload: ItemPayload[] | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean)): {
196
216
  payload: ItemPayload;
197
217
  data: ItemState;
198
218
  }[];
@@ -200,7 +220,7 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
200
220
  };
201
221
  loadMore: (params: QueryPayload, size: number) => ScheduleFetchResults;
202
222
  invalidateQuery: (queryPayload: QueryPayload | QueryPayload[] | ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean), priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
203
- invalidateItem: (itemId: ItemPayload | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean) | ItemPayload[], priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
223
+ invalidateItem: (itemId: ItemPayload | ItemPayload[] | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean), priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
204
224
  scheduleItemFetch: {
205
225
  (fetchType: FetchType, itemPayload: ItemPayload): ScheduleFetchResults;
206
226
  (fetchType: FetchType, itemPayload: ItemPayload[]): ScheduleFetchResults[];
@@ -263,9 +283,9 @@ declare function newTSDFListQueryStore<ItemState extends ValidStoreState, NError
263
283
  returnRefetchingStatus?: boolean | undefined;
264
284
  }) => readonly TSFDUseListItemReturn<SelectedItem_3, NError, ItemPayload>[];
265
285
  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;
286
+ updateItemState: (itemIds: ItemPayload | ItemPayload[] | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean), produceNewData: (draftData: ItemState, itemPayload: ItemPayload) => void | ItemState, ifNothingWasUpdated?: () => void) => boolean;
287
+ startItemMutation: (itemId: ItemPayload | ItemPayload[] | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean)) => () => void;
268
288
  };
269
289
  type TSFDListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload, ItemPayload>>;
270
290
 
271
- export { FetchListFnReturn, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn, TSFDUseListItemReturn, TSFDUseListQueryReturn, ValidPayload, ValidStoreState, newTSDFCollectionStore, newTSDFDocumentStore, newTSDFListQueryStore };
291
+ export { CollectionInitialStateItem, FetchListFnReturn, ListQueryStoreInitialData, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn, TSFDUseListItemReturn, TSFDUseListQueryReturn, ValidPayload, ValidStoreState, newTSDFCollectionStore, newTSDFDocumentStore, newTSDFListQueryStore };
package/dist/main.js CHANGED
@@ -346,6 +346,7 @@ function newTSDFDocumentStore({
346
346
  lowPriorityThrottleMs,
347
347
  mediumPriorityThrottleMs,
348
348
  dynamicRealtimeThrottleMs,
349
+ disableInitialDataInvalidation,
349
350
  errorNormalizer
350
351
  }) {
351
352
  const store = new Store({
@@ -354,7 +355,7 @@ function newTSDFDocumentStore({
354
355
  data: initialData ?? null,
355
356
  error: null,
356
357
  status: initialData ? "success" : "idle",
357
- refetchOnMount: false
358
+ refetchOnMount: !!initialData && !disableInitialDataInvalidation ? "lowPriority" : false
358
359
  }
359
360
  });
360
361
  async function fetch(fetchCtx, _) {
@@ -574,14 +575,30 @@ function newTSDFCollectionStore({
574
575
  disableRefetchOnWindowFocus,
575
576
  lowPriorityThrottleMs,
576
577
  errorNormalizer,
578
+ disableInitialDataInvalidation,
577
579
  mediumPriorityThrottleMs,
580
+ initialStateItems,
578
581
  disableRefetchOnMount: globalDisableRefetchOnMount,
579
582
  dynamicRealtimeThrottleMs,
580
583
  getCollectionItemKey: filterCollectionItemObjKey
581
584
  }) {
585
+ const initialState = {};
586
+ if (initialStateItems) {
587
+ for (const item of initialStateItems) {
588
+ const itemKey = getItemKey(item.payload);
589
+ initialState[itemKey] = {
590
+ data: item.data,
591
+ error: null,
592
+ status: "success",
593
+ payload: item.payload,
594
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
595
+ wasLoaded: true
596
+ };
597
+ }
598
+ }
582
599
  const store = new Store2({
583
600
  debugName,
584
- state: {}
601
+ state: initialState
585
602
  });
586
603
  function getItemKey(params) {
587
604
  return getCacheId(
@@ -964,16 +981,44 @@ function newTSDFListQueryStore({
964
981
  fetchItemFn,
965
982
  errorNormalizer,
966
983
  defaultQuerySize = 50,
984
+ initialData,
967
985
  disableRefetchOnWindowFocus,
968
986
  disableRefetchOnMount: globalDisableRefetchOnMount,
969
987
  lowPriorityThrottleMs,
988
+ disableInitialDataInvalidation,
970
989
  mediumPriorityThrottleMs,
971
990
  dynamicRealtimeThrottleMs,
972
991
  syncMutationsAndInvalidations
973
992
  }) {
993
+ const initialState = { items: {}, queries: {}, itemQueries: {} };
994
+ if (initialData) {
995
+ for (const { payload, data } of initialData.items) {
996
+ const itemKey = getItemKey(payload);
997
+ initialState.items[itemKey] = data;
998
+ initialState.itemQueries[itemKey] = {
999
+ error: null,
1000
+ status: "success",
1001
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1002
+ wasLoaded: true,
1003
+ payload
1004
+ };
1005
+ }
1006
+ for (const { payload, items, hasMore } of initialData.queries) {
1007
+ const queryKey = getQueryKey(payload);
1008
+ initialState.queries[queryKey] = {
1009
+ error: null,
1010
+ status: "success",
1011
+ refetchOnMount: disableInitialDataInvalidation ? false : "lowPriority",
1012
+ wasLoaded: true,
1013
+ payload,
1014
+ items: items.map(getItemKey),
1015
+ hasMore
1016
+ };
1017
+ }
1018
+ }
974
1019
  const store = new Store3({
975
1020
  debugName,
976
- state: { items: {}, queries: {}, itemQueries: {} }
1021
+ state: initialState
977
1022
  });
978
1023
  function getQueryKey(params) {
979
1024
  return getCacheId(params);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "tsdf",
3
- "version": "8.0.0-beta.8",
3
+ "version": "8.0.0-beta.9",
4
4
  "description": "A data fetching solution based on t-state",
5
5
  "license": "MIT",
6
6
  "author": "Lucas Santos",