tsdf 8.0.0-beta.1 → 8.0.0-beta.3

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 +1 -0
  2. package/dist/main.d.ts +269 -0
  3. package/dist/main.js +1 -0
  4. package/package.json +4 -1
  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/src/main.ts DELETED
@@ -1,22 +0,0 @@
1
- export {
2
- newTSDFDocumentStore,
3
- TSDFDocumentStore,
4
- TSDFDocumentStoreState,
5
- TSDFUseDocumentReturn,
6
- } from './documentStore';
7
- export {
8
- newTSDFCollectionStore,
9
- TSDFCollectionStore,
10
- TSFDCollectionState,
11
- TSFDCollectionItem,
12
- TSFDUseCollectionItemReturn,
13
- } from './collectionStore';
14
- export {
15
- newTSDFListQueryStore,
16
- TSFDListQueryStore,
17
- TSFDListQueryState,
18
- TSDFItemQuery,
19
- TSFDListQuery,
20
- TSFDUseListQueryReturn,
21
- TSFDUseItemReturn,
22
- } from './listQueryStore';
@@ -1,5 +0,0 @@
1
- export type Status = 'loading' | 'error' | 'refetching' | 'success';
2
-
3
- export type ValidPayload = number | string | Record<string, unknown>;
4
-
5
- export type ValidStoreState = Record<string, unknown> | any[];
@@ -1,50 +0,0 @@
1
- import mitt from 'mitt';
2
- import { useMemo, useState } from 'react';
3
- import { useConst, useOnChange, useOnMittEvent } from './utils/hooks';
4
-
5
- export function useEnsureIsLoaded(
6
- ensureIsLoaded: boolean | undefined,
7
- enabled: boolean,
8
- forceFetch: () => void,
9
- ) {
10
- const isLoadedEvtEmitter = useConst(() => mitt<{ isLoaded: boolean }>());
11
-
12
- const [isForceLoading, setIsForceLoading] = useState(true);
13
-
14
- useOnChange(
15
- ensureIsLoaded && isForceLoading && enabled,
16
- ({ current }) => {
17
- if (current) {
18
- forceFetch();
19
- }
20
- },
21
- { callOnMount: true },
22
- );
23
-
24
- useOnMittEvent(isLoadedEvtEmitter, 'isLoaded', (isLoaded) => {
25
- if (ensureIsLoaded && enabled && isLoaded) {
26
- setIsForceLoading(false);
27
- }
28
- });
29
-
30
- function useModifyResult<T extends { isLoading: boolean; status: string }>(
31
- result: T,
32
- ) {
33
- return useMemo(() => {
34
- if (ensureIsLoaded) {
35
- const newStatus = enabled && isForceLoading ? 'loading' : result.status;
36
-
37
- return {
38
- ...result,
39
- isLoading: newStatus === 'loading',
40
- status: newStatus,
41
- };
42
- }
43
-
44
- return result;
45
- // eslint-disable-next-line react-hooks/exhaustive-deps
46
- }, [ensureIsLoaded, isForceLoading, result]);
47
- }
48
-
49
- return [useModifyResult, isLoadedEvtEmitter.emit] as const;
50
- }
@@ -1,20 +0,0 @@
1
- const ignore = Symbol();
2
-
3
- export function filterAndMap<T, R>(
4
- arr: T[],
5
- fn: (item: T, ignoreItem: symbol, index: number) => R | symbol,
6
- ): R[] {
7
- const result: R[] = [];
8
- let i = 0;
9
- for (const item of arr) {
10
- const mappedItem = fn(item, ignore, i);
11
-
12
- if (mappedItem !== ignore) {
13
- result.push(mappedItem as R);
14
- }
15
-
16
- i++;
17
- }
18
-
19
- return result;
20
- }
@@ -1,19 +0,0 @@
1
- import { filterAndMap } from './filterAndMap';
2
- import { isObject } from './isObject';
3
-
4
- export function getCacheId(input: any) {
5
- return typeof input === 'string'
6
- ? input
7
- : JSON.stringify(input && isObject(input) ? orderedProps(input) : input);
8
- }
9
-
10
- function orderedProps(obj: Record<string, unknown>) {
11
- // eslint-disable-next-line no-restricted-syntax
12
- return filterAndMap(Object.keys(obj).sort(), (k, ignore) => {
13
- const value = obj[k];
14
-
15
- if (value === undefined) return ignore;
16
-
17
- return { [k]: obj[k] };
18
- });
19
- }
@@ -1,110 +0,0 @@
1
- import { Emitter } from 'mitt';
2
- import { useEffect, useLayoutEffect, useMemo, useRef } from 'react';
3
- import { deepEqual, shallowEqual } from 't-state';
4
-
5
- function useDeepCompareMemoize(value: any, equalityFn = deepEqual) {
6
- const ref = useRef<any>();
7
- const signalRef = useRef<number>(0);
8
-
9
- if (!equalityFn(value, ref.current)) {
10
- ref.current = value;
11
- signalRef.current += 1;
12
- }
13
-
14
- return [signalRef.current];
15
- }
16
-
17
- export function useDeepMemo<T>(callback: () => T, deeps: any[]): T {
18
- // eslint-disable-next-line react-hooks/exhaustive-deps
19
- return useMemo(callback, useDeepCompareMemoize(deeps));
20
- }
21
-
22
- export function useShallowMemo<T>(callback: () => T, deeps: any[]): T {
23
- // eslint-disable-next-line react-hooks/exhaustive-deps
24
- return useMemo(callback, useDeepCompareMemoize(deeps, shallowEqual));
25
- }
26
-
27
- export function useLatestValue<T>(value: T) {
28
- const ref = useRef<{
29
- insideEffect: T;
30
- insideMemo: T;
31
- }>({ insideEffect: value, insideMemo: value });
32
-
33
- ref.current.insideMemo = value;
34
-
35
- useLayoutEffect(() => {
36
- ref.current.insideEffect = value;
37
- });
38
-
39
- return ref.current;
40
- }
41
-
42
- export function useOnMittEvent<
43
- T extends Record<string, any>,
44
- E extends keyof T,
45
- >(mitt: Emitter<T>, event: E, callback: (payload: T[E]) => void) {
46
- const latestCallback = useLatestValue(callback);
47
-
48
- useLayoutEffect(() => {
49
- const cb = (payload: T[E]) => {
50
- latestCallback.insideEffect(payload);
51
- };
52
-
53
- mitt.on(event, cb);
54
-
55
- return () => {
56
- mitt.off(event, cb);
57
- };
58
- }, [mitt, event, latestCallback]);
59
- }
60
-
61
- function usePrevious<T>(value: T, initial?: T): T | undefined {
62
- const ref = useRef<T | undefined>(initial);
63
- useEffect(() => {
64
- ref.current = value;
65
- });
66
- return ref.current;
67
- }
68
-
69
- type EqualityFn = (a: any, b: any) => boolean;
70
- type CleanupFn = () => void;
71
-
72
- type OnChangeOptions = {
73
- equalityFn?: EqualityFn;
74
- callOnMount?: boolean;
75
- layoutEffect?: boolean;
76
- };
77
-
78
- /**
79
- * A replacement to useEffect that not need an array of dependencies.
80
- * PS: by default the callback is not called on mount, to change this use the
81
- * callOnMount option.
82
- */
83
- export function useOnChange<T>(
84
- value: T,
85
- callBack: (values: { prev: T | undefined; current: T }) => void | CleanupFn,
86
- {
87
- equalityFn: areEqual = shallowEqual,
88
- callOnMount,
89
- layoutEffect,
90
- }: OnChangeOptions = {},
91
- ) {
92
- const useEffectFn = layoutEffect ? useLayoutEffect : useEffect;
93
-
94
- const prev = usePrevious(value, !callOnMount ? value : undefined);
95
- useEffectFn(() => {
96
- if (!areEqual(value, prev)) {
97
- return callBack({ prev, current: value });
98
- }
99
- });
100
- }
101
-
102
- export function useConst<T>(getValue: () => T) {
103
- const store = useRef<T>();
104
-
105
- if (store.current === undefined) {
106
- store.current = getValue();
107
- }
108
-
109
- return store.current;
110
- }
@@ -1,3 +0,0 @@
1
- export function isObject(value: any): value is Record<string, unknown> {
2
- return typeof value === 'object' && value !== null && !Array.isArray(value);
3
- }
package/src/utils/math.ts DELETED
@@ -1,7 +0,0 @@
1
- export function clampMax(value: number, max: number) {
2
- return value > max ? max : value;
3
- }
4
-
5
- export function clampMin(value: number, min: number) {
6
- return value < min ? min : value;
7
- }
@@ -1,3 +0,0 @@
1
- export function serializableClone<T>(input: T): T {
2
- return JSON.parse(JSON.stringify(input)) as T;
3
- }
@@ -1,16 +0,0 @@
1
- import { useLayoutEffect, useRef } from 'react';
2
-
3
- export function useLatestValue<T>(value: T) {
4
- const ref = useRef<{
5
- insideEffect: T;
6
- insideMemo: T;
7
- }>({ insideEffect: value, insideMemo: value });
8
-
9
- ref.current.insideMemo = value;
10
-
11
- useLayoutEffect(() => {
12
- ref.current.insideEffect = value;
13
- });
14
-
15
- return ref.current;
16
- }
@@ -1,146 +0,0 @@
1
- // Vitest Snapshot v1
2
-
3
- exports[`fetch query > fetch with size 1`] = `
4
- {
5
- "users||1": {
6
- "id": 1,
7
- "name": "User 1",
8
- },
9
- "users||2": {
10
- "id": 2,
11
- "name": "User 2",
12
- },
13
- "users||3": {
14
- "id": 3,
15
- "name": "User 3",
16
- },
17
- "users||4": {
18
- "id": 4,
19
- "name": "User 4",
20
- },
21
- "users||5": {
22
- "id": 5,
23
- "name": "User 5",
24
- },
25
- }
26
- `;
27
-
28
- exports[`fetch query > fetch with size 2`] = `
29
- {
30
- "products||1": {
31
- "id": 1,
32
- "name": "Product 1",
33
- },
34
- "products||2": {
35
- "id": 2,
36
- "name": "Product 2",
37
- },
38
- "products||3": {
39
- "id": 3,
40
- "name": "Product 3",
41
- },
42
- "products||4": {
43
- "id": 4,
44
- "name": "Product 4",
45
- },
46
- "products||5": {
47
- "id": 5,
48
- "name": "Product 5",
49
- },
50
- }
51
- `;
52
-
53
- exports[`fetch query > fetch with size 3`] = `
54
- {
55
- "products||1": {
56
- "id": 1,
57
- "name": "Product 1",
58
- },
59
- "products||10": {
60
- "id": 10,
61
- "name": "Product 10",
62
- },
63
- "products||2": {
64
- "id": 2,
65
- "name": "Product 2",
66
- },
67
- "products||3": {
68
- "id": 3,
69
- "name": "Product 3",
70
- },
71
- "products||4": {
72
- "id": 4,
73
- "name": "Product 4",
74
- },
75
- "products||5": {
76
- "id": 5,
77
- "name": "Product 5",
78
- },
79
- "products||6": {
80
- "id": 6,
81
- "name": "Product 6",
82
- },
83
- "products||7": {
84
- "id": 7,
85
- "name": "Product 7",
86
- },
87
- "products||8": {
88
- "id": 8,
89
- "name": "Product 8",
90
- },
91
- "products||9": {
92
- "id": 9,
93
- "name": "Product 9",
94
- },
95
- }
96
- `;
97
-
98
- exports[`fetch query > refetch list with updated data 1`] = `
99
- {
100
- "users||1": {
101
- "id": 1,
102
- "name": "User 1",
103
- },
104
- "users||2": {
105
- "id": 2,
106
- "name": "User 2",
107
- },
108
- "users||3": {
109
- "id": 3,
110
- "name": "User 3",
111
- },
112
- "users||4": {
113
- "id": 4,
114
- "name": "User 4",
115
- },
116
- "users||5": {
117
- "id": 5,
118
- "name": "User 5",
119
- },
120
- }
121
- `;
122
-
123
- exports[`fetch query > refetch list with updated data 2`] = `
124
- {
125
- "users||1": {
126
- "id": 1,
127
- "name": "Updated User 1",
128
- },
129
- "users||2": {
130
- "id": 2,
131
- "name": "User 2",
132
- },
133
- "users||3": {
134
- "id": 3,
135
- "name": "User 3",
136
- },
137
- "users||4": {
138
- "id": 4,
139
- "name": "User 4",
140
- },
141
- "users||5": {
142
- "id": 5,
143
- "name": "User 5",
144
- },
145
- }
146
- `;
@@ -1,9 +0,0 @@
1
- // Vitest Snapshot v1
2
-
3
- exports[`useMultipleItemsQuery sequential tests > invalidate all queries > refetch data after invalidations 2`] = `undefined`;
4
-
5
- exports[`useMultipleItemsQuery sequential tests > invalidate all queries > refetch data after invalidations 4`] = `undefined`;
6
-
7
- exports[`useMultipleItemsQuery sequential tests > load the queries 2`] = `undefined`;
8
-
9
- exports[`useMultipleItemsQuery sequential tests > load the queries 4`] = `undefined`;