tsdf 8.0.0-beta.1 → 8.0.0-beta.11

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.
Files changed (57) hide show
  1. package/dist/main.cjs +1860 -0
  2. package/dist/main.d.ts +291 -0
  3. package/dist/main.js +1825 -0
  4. package/package.json +6 -2
  5. package/.eslintignore +0 -3
  6. package/.eslintrc.cjs +0 -119
  7. package/.gitattributes +0 -22
  8. package/.prettierrc +0 -6
  9. package/.vscode/settings.json +0 -3
  10. package/.vscode/tasks.json +0 -35
  11. package/fetch-orquestrator-scenarios.excalidraw +0 -10115
  12. package/scripts/check-if-is-sync.sh +0 -6
  13. package/src/collectionFetchOrquestrator.ts +0 -23
  14. package/src/collectionStore.ts +0 -608
  15. package/src/documentStore.ts +0 -272
  16. package/src/fetchOrquestrator.ts +0 -335
  17. package/src/listQueryStore.ts +0 -1190
  18. package/src/main.ts +0 -22
  19. package/src/storeShared.ts +0 -5
  20. package/src/useEnsureIsLoaded.ts +0 -50
  21. package/src/utils/filterAndMap.ts +0 -20
  22. package/src/utils/getCacheId.ts +0 -19
  23. package/src/utils/hooks.ts +0 -110
  24. package/src/utils/isObject.ts +0 -3
  25. package/src/utils/math.ts +0 -7
  26. package/src/utils/serializableClone.ts +0 -3
  27. package/src/utils/useLatestValue.ts +0 -16
  28. package/test/__snapshots__/listQueryStore.test.ts.snap +0 -146
  29. package/test/__snapshots__/listQueryStoreHooks.test.ts.snap +0 -9
  30. package/test/collectionStore.test.ts +0 -523
  31. package/test/collectionStoreHooks.test.tsx +0 -651
  32. package/test/documentStore.test.ts +0 -183
  33. package/test/documentStoreHooks.test.tsx +0 -857
  34. package/test/fetchOrquestrator.test.ts +0 -913
  35. package/test/listQueryStore.test.ts +0 -1158
  36. package/test/listQueryStoreHooks.test.tsx +0 -662
  37. package/test/listQueryStoreHooks2.test.tsx +0 -483
  38. package/test/listQueryStoreHooksMutations.test.tsx +0 -764
  39. package/test/mocks/fetchMock.ts +0 -296
  40. package/test/mocks/fetchOrquestratorEnv.ts +0 -370
  41. package/test/setup/setup.ts +0 -109
  42. package/test/testHelpers.test.ts +0 -88
  43. package/test/utils/RenderObjValues.tsx +0 -23
  44. package/test/utils/arrayUtils.ts +0 -13
  45. package/test/utils/createDefaultListQueryStore.ts +0 -275
  46. package/test/utils/createOrVariations.ts +0 -48
  47. package/test/utils/dedent.ts +0 -50
  48. package/test/utils/delayCall.ts +0 -25
  49. package/test/utils/math.ts +0 -3
  50. package/test/utils/objectUtils.ts +0 -18
  51. package/test/utils/range.ts +0 -19
  52. package/test/utils/sleep.ts +0 -3
  53. package/test/utils/storeUtils.ts +0 -460
  54. package/tsconfig.json +0 -24
  55. package/tsconfig.prod.json +0 -9
  56. package/tsup.config.ts +0 -12
  57. package/vitest.config.ts +0 -10
package/dist/main.d.ts ADDED
@@ -0,0 +1,291 @@
1
+ import { Store } from 't-state';
2
+
3
+ type FetchType = 'lowPriority' | 'highPriority' | 'realtimeUpdate';
4
+ type ScheduleFetchResults = 'skipped' | 'started' | 'scheduled' | 'rt-scheduled';
5
+
6
+ type TSDFStatus = 'loading' | 'error' | 'refetching' | 'success';
7
+ type ValidPayload = number | string | Record<string, unknown>;
8
+ type ValidStoreState = Record<string, unknown> | any[];
9
+
10
+ type DocumentStatus = TSDFStatus | 'idle';
11
+ type TSDFDocumentStoreState<State extends ValidStoreState, NError> = {
12
+ data: State | null;
13
+ error: NError | null;
14
+ status: DocumentStatus;
15
+ refetchOnMount: false | FetchType;
16
+ };
17
+ type TSDFUseDocumentReturn<Selected, NError> = {
18
+ status: DocumentStatus;
19
+ data: Selected;
20
+ error: NError | null;
21
+ isLoading: boolean;
22
+ };
23
+ declare function newTSDFDocumentStore<State extends ValidStoreState, NError>({ debugName, fetchFn, initialData, disableRefetchOnWindowFocus, disableRefetchOnMount: globalDisableRefetchOnMount, lowPriorityThrottleMs, mediumPriorityThrottleMs, dynamicRealtimeThrottleMs, disableInitialDataInvalidation, errorNormalizer, }: {
24
+ debugName?: string;
25
+ fetchFn: () => Promise<State>;
26
+ initialData?: State;
27
+ disableInitialDataInvalidation?: boolean;
28
+ errorNormalizer: (exception: unknown) => NError;
29
+ disableRefetchOnWindowFocus?: boolean;
30
+ disableRefetchOnMount?: boolean;
31
+ lowPriorityThrottleMs?: number;
32
+ mediumPriorityThrottleMs?: number;
33
+ dynamicRealtimeThrottleMs?: (lastFetchDuration: number) => number;
34
+ }): {
35
+ store: Store<TSDFDocumentStoreState<State, NError>>;
36
+ awaitFetch: () => Promise<{
37
+ data: State;
38
+ error: null;
39
+ } | {
40
+ data: null;
41
+ error: NError;
42
+ }>;
43
+ scheduleFetch: (fetchType: FetchType) => ScheduleFetchResults;
44
+ invalidateData: (priority?: FetchType) => void;
45
+ useDocument: <Selected = State | null>({ selector, disabled, returnRefetchingStatus, disableRefetchOnMount, returnIdleStatus, ensureIsLoaded, }?: {
46
+ selector?: ((data: State | null) => Selected) | undefined;
47
+ disabled?: boolean | undefined;
48
+ disableRefetchOnMount?: boolean | undefined;
49
+ returnIdleStatus?: boolean | undefined;
50
+ ensureIsLoaded?: boolean | undefined;
51
+ returnRefetchingStatus?: boolean | undefined;
52
+ }) => Readonly<TSDFUseDocumentReturn<Selected, NError>>;
53
+ updateState: (produceNewData: (draftData: State) => State | void | undefined) => void;
54
+ startMutation: () => () => boolean;
55
+ };
56
+ type TSDFDocumentStore<State extends ValidStoreState, NError> = ReturnType<typeof newTSDFDocumentStore<State, NError>>;
57
+
58
+ type CollectionItemStatus = TSDFStatus;
59
+ type TSFDCollectionItem<ItemState extends ValidStoreState, ItemPayload extends ValidPayload, NError> = {
60
+ data: ItemState | null;
61
+ error: NError | null;
62
+ status: CollectionItemStatus;
63
+ payload: ItemPayload;
64
+ refetchOnMount: false | FetchType;
65
+ wasLoaded: boolean;
66
+ };
67
+ type TSFDCollectionState<ItemState extends ValidStoreState, ItemPayload extends ValidPayload, NError> = Record<string, TSFDCollectionItem<ItemState, ItemPayload, NError> | null>;
68
+ type TSFDUseCollectionItemReturn<Selected, ItemPayload, NError> = {
69
+ data: Selected;
70
+ status: CollectionItemStatus | 'idle' | 'deleted';
71
+ payload: ItemPayload | undefined;
72
+ error: NError | null;
73
+ itemStateKey: string;
74
+ isLoading: boolean;
75
+ };
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, }: {
81
+ debugName?: string;
82
+ fetchFn: (params: ItemPayload) => Promise<ItemState>;
83
+ getCollectionItemKey?: (params: ItemPayload) => ValidPayload | any[];
84
+ errorNormalizer: (exception: unknown) => NError;
85
+ disableRefetchOnWindowFocus?: boolean;
86
+ disableRefetchOnMount?: boolean;
87
+ initialStateItems?: CollectionInitialStateItem<ItemPayload, ItemState>[];
88
+ disableInitialDataInvalidation?: boolean;
89
+ lowPriorityThrottleMs?: number;
90
+ mediumPriorityThrottleMs?: number;
91
+ dynamicRealtimeThrottleMs?: (lastFetchDuration: number) => number;
92
+ }): {
93
+ store: Store<TSFDCollectionState<ItemState, ItemPayload, NError>>;
94
+ scheduleFetch: {
95
+ (fetchType: FetchType, payload: ItemPayload): ScheduleFetchResults;
96
+ (fetchType: FetchType, payload: ItemPayload[]): ScheduleFetchResults[];
97
+ };
98
+ awaitFetch: (params: ItemPayload) => Promise<{
99
+ data: null;
100
+ error: NError;
101
+ } | {
102
+ data: ItemState;
103
+ error: null;
104
+ }>;
105
+ useMultipleItems: <Selected = ItemState | null>(queries: readonly ItemPayload[], { selector, omitPayload, returnIdleStatus, returnRefetchingStatus, disableRefetchOnMount, }?: {
106
+ selector?: ((data: ItemState | null) => Selected) | undefined;
107
+ omitPayload?: boolean | undefined;
108
+ disableRefetchOnMount?: boolean | undefined;
109
+ returnIdleStatus?: boolean | undefined;
110
+ returnRefetchingStatus?: boolean | undefined;
111
+ }) => readonly TSFDUseCollectionItemReturn<Selected, ItemPayload, NError>[];
112
+ useItem: <Selected_1 = ItemState | null>(payload: ItemPayload | undefined | false | null, options?: {
113
+ selector?: ((data: ItemState | null) => Selected_1) | undefined;
114
+ omitPayload?: boolean | undefined;
115
+ returnRefetchingStatus?: boolean | undefined;
116
+ disableRefetchOnMount?: boolean | undefined;
117
+ ensureIsLoaded?: boolean | undefined;
118
+ }) => TSFDUseCollectionItemReturn<Selected_1, ItemPayload, NError>;
119
+ getItemKey: (params: ItemPayload) => string;
120
+ getItemState: {
121
+ (fetchParam: ItemPayload): TSFDCollectionItem<ItemState, ItemPayload, NError> | null | undefined;
122
+ (fetchParams: ItemPayload[] | ((params: ItemPayload, data: ItemState | null) => boolean)): TSFDCollectionItem<ItemState, ItemPayload, NError>[];
123
+ };
124
+ startMutation: (fetchParams: ItemPayload | ItemPayload[] | ((params: ItemPayload, data: ItemState | null) => boolean)) => () => void;
125
+ invalidateItem: (itemPayload: ItemPayload | ItemPayload[] | ((params: ItemPayload, data: ItemState | null) => boolean), priority?: FetchType) => void;
126
+ updateItemState: (fetchParams: ItemPayload | ItemPayload[] | ((params: ItemPayload, data: ItemState | null) => boolean), produceNewData: (draftData: ItemState, collectionItem: TSFDCollectionItem<ItemState, ItemPayload, NError>) => void | ItemState, ifNothingWasUpdated?: () => void) => boolean;
127
+ addItemToState: (fetchParams: ItemPayload, data: ItemState) => void;
128
+ deleteItemState: (fetchParams: ItemPayload | ItemPayload[] | ((params: ItemPayload, data: ItemState | null) => boolean)) => void;
129
+ };
130
+ type TSDFCollectionStore<ItemState extends ValidStoreState, NError, FetchParams extends ValidPayload> = ReturnType<typeof newTSDFCollectionStore<ItemState, NError, FetchParams>>;
131
+
132
+ type QueryStatus = TSDFStatus | 'loadingMore';
133
+ type TSFDListQuery<NError, QueryPayload extends ValidPayload> = {
134
+ error: NError | null;
135
+ status: QueryStatus;
136
+ payload: QueryPayload;
137
+ hasMore: boolean;
138
+ wasLoaded: boolean;
139
+ refetchOnMount: false | FetchType;
140
+ items: string[];
141
+ };
142
+ type TSDFItemQuery<NError, ItemPayload> = {
143
+ error: NError | null;
144
+ status: Exclude<QueryStatus, 'loadingMore'>;
145
+ wasLoaded: boolean;
146
+ refetchOnMount: false | FetchType;
147
+ payload: ItemPayload;
148
+ };
149
+ type TSFDListQueryState<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = {
150
+ items: Record<string, ItemState | null>;
151
+ queries: Record<string, TSFDListQuery<NError, QueryPayload>>;
152
+ itemQueries: Record<string, TSDFItemQuery<NError, ItemPayload> | null>;
153
+ };
154
+ type TSFDUseListQueryReturn<Selected, ItemPayload, NError> = {
155
+ items: Selected[];
156
+ status: QueryStatus | 'idle';
157
+ payload: ItemPayload | undefined;
158
+ error: NError | null;
159
+ queryKey: string;
160
+ hasMore: boolean;
161
+ isLoading: boolean;
162
+ };
163
+ type TSFDUseListItemReturn<Selected, NError, ItemPayload> = {
164
+ data: Selected;
165
+ status: QueryStatus | 'idle' | 'deleted';
166
+ payload: ItemPayload | null;
167
+ error: NError | null;
168
+ isLoading: boolean;
169
+ };
170
+ type FetchListFnReturn<ItemState extends ValidStoreState, ItemPayload extends ValidPayload> = {
171
+ items: {
172
+ itemPayload: ItemPayload;
173
+ data: ItemState;
174
+ }[];
175
+ hasMore: boolean;
176
+ };
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, }: {
189
+ debugName?: string;
190
+ fetchListFn: (payload: QueryPayload, size: number) => Promise<FetchListFnReturn<ItemState, ItemPayload>>;
191
+ fetchItemFn?: (itemId: ItemPayload) => Promise<ItemState>;
192
+ errorNormalizer: (exception: unknown) => NError;
193
+ defaultQuerySize?: number;
194
+ initialData?: ListQueryStoreInitialData<ItemState, QueryPayload, ItemPayload>;
195
+ disableInitialDataInvalidation?: boolean;
196
+ disableRefetchOnWindowFocus?: boolean;
197
+ syncMutationsAndInvalidations?: {
198
+ syncQueries: (query1: QueryPayload, query2: QueryPayload) => boolean;
199
+ syncItemAndQuery: (itemPayload: ItemPayload, query: QueryPayload) => boolean;
200
+ };
201
+ disableRefetchOnMount?: boolean;
202
+ lowPriorityThrottleMs?: number;
203
+ mediumPriorityThrottleMs?: number;
204
+ dynamicRealtimeThrottleMs?: (lastFetchDuration: number) => number;
205
+ }): {
206
+ store: Store<TSFDListQueryState<ItemState, NError, QueryPayload, ItemPayload>>;
207
+ scheduleListQueryFetch: {
208
+ (fetchType: FetchType, payload: QueryPayload, size?: number): ScheduleFetchResults;
209
+ (fetchType: FetchType, payload: QueryPayload[], size?: number): ScheduleFetchResults[];
210
+ };
211
+ getQueryState: (params: QueryPayload) => TSFDListQuery<NError, QueryPayload> | undefined;
212
+ getQueryKey: (params: QueryPayload) => string;
213
+ deleteItemState: (itemId: ItemPayload | ItemPayload[] | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean)) => void;
214
+ getItemState: {
215
+ (itemPayload: ItemPayload[] | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean)): {
216
+ payload: ItemPayload;
217
+ data: ItemState;
218
+ }[];
219
+ (itemPayload: ItemPayload): ItemState | null | undefined;
220
+ };
221
+ loadMore: (params: QueryPayload, size: number) => ScheduleFetchResults;
222
+ invalidateQuery: (queryPayload: QueryPayload | QueryPayload[] | ((params: QueryPayload, data: TSFDListQuery<NError, QueryPayload>, queryKey: string) => boolean), priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
223
+ invalidateItem: (itemId: ItemPayload | ItemPayload[] | ((ItemPayload: ItemPayload, itemState: ItemState) => boolean), priority?: FetchType, ignoreInvalidationSync?: boolean) => void;
224
+ scheduleItemFetch: {
225
+ (fetchType: FetchType, itemPayload: ItemPayload): ScheduleFetchResults;
226
+ (fetchType: FetchType, itemPayload: ItemPayload[]): ScheduleFetchResults[];
227
+ };
228
+ awaitItemFetch: (itemPayload: ItemPayload) => Promise<{
229
+ data: null;
230
+ error: NError;
231
+ } | {
232
+ data: ItemState;
233
+ error: null;
234
+ }>;
235
+ awaitListQueryFetch: (params: QueryPayload, size?: number) => Promise<{
236
+ items: [
237
+ ];
238
+ error: NError;
239
+ hasMore: boolean;
240
+ } | {
241
+ items: {
242
+ data: ItemState;
243
+ itemKey: string;
244
+ }[];
245
+ error: null;
246
+ hasMore: boolean;
247
+ }>;
248
+ useMultipleListQueries: <SelectedItem = {
249
+ id: string;
250
+ data: ItemState;
251
+ }>(queries: readonly QueryPayload[], { itemSelector, omitPayload, returnIdleStatus, returnRefetchingStatus, loadSize, disableRefetchOnMount, }?: {
252
+ itemSelector?: ((data: ItemState, id: string) => SelectedItem) | undefined;
253
+ omitPayload?: boolean | undefined;
254
+ disableRefetchOnMount?: boolean | undefined;
255
+ returnIdleStatus?: boolean | undefined;
256
+ returnRefetchingStatus?: boolean | undefined;
257
+ loadSize?: number | undefined;
258
+ }) => readonly TSFDUseListQueryReturn<SelectedItem, QueryPayload, NError>[];
259
+ useListQuery: <SelectedItem_1 = {
260
+ id: string;
261
+ data: ItemState;
262
+ }>(payload: QueryPayload | false | null | undefined, options?: {
263
+ itemSelector?: ((data: ItemState, id: string) => SelectedItem_1) | undefined;
264
+ omitPayload?: boolean | undefined;
265
+ disableRefetchOnMount?: boolean | undefined;
266
+ returnIdleStatus?: boolean | undefined;
267
+ returnRefetchingStatus?: boolean | undefined;
268
+ ensureIsLoaded?: boolean | undefined;
269
+ loadSize?: number | undefined;
270
+ }) => TSFDUseListQueryReturn<SelectedItem_1, QueryPayload, NError>;
271
+ getItemKey: (params: ItemPayload) => string;
272
+ useItem: <SelectedItem_2 = ItemState | null>(itemPayload: ItemPayload | false | null | undefined, options?: {
273
+ selector?: ((data: ItemState | null, id: string) => SelectedItem_2) | undefined;
274
+ disableRefetchOnMount?: boolean | undefined;
275
+ returnIdleStatus?: boolean | undefined;
276
+ returnRefetchingStatus?: boolean | undefined;
277
+ ensureIsLoaded?: boolean | undefined;
278
+ }) => TSFDUseListItemReturn<SelectedItem_2, NError, ItemPayload>;
279
+ useMultipleItems: <SelectedItem_3 = ItemState | null>(itemsPayload_: ItemPayload[], { selector, returnIdleStatus, returnRefetchingStatus, disableRefetchOnMount, }?: {
280
+ selector?: ((data: ItemState | null, id: string) => SelectedItem_3) | undefined;
281
+ disableRefetchOnMount?: boolean | undefined;
282
+ returnIdleStatus?: boolean | undefined;
283
+ returnRefetchingStatus?: boolean | undefined;
284
+ }) => readonly TSFDUseListItemReturn<SelectedItem_3, NError, ItemPayload>[];
285
+ addItemToState: (itemPayload: ItemPayload, data: ItemState) => 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;
288
+ };
289
+ type TSFDListQueryStore<ItemState extends ValidStoreState, NError, QueryPayload extends ValidPayload, ItemPayload extends ValidPayload> = ReturnType<typeof newTSDFListQueryStore<ItemState, NError, QueryPayload, ItemPayload>>;
290
+
291
+ export { CollectionInitialStateItem, FetchListFnReturn, ListQueryStoreInitialData, TSDFCollectionStore, TSDFDocumentStore, TSDFDocumentStoreState, TSDFItemQuery, TSDFStatus, TSDFUseDocumentReturn, TSFDCollectionItem, TSFDCollectionState, TSFDListQuery, TSFDListQueryState, TSFDListQueryStore, TSFDUseCollectionItemReturn, TSFDUseListItemReturn, TSFDUseListQueryReturn, ValidPayload, ValidStoreState, newTSDFCollectionStore, newTSDFDocumentStore, newTSDFListQueryStore };