@applicaster/zapp-react-native-utils 15.0.0-alpha.5485312433 → 15.0.0-alpha.5531846549

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 (68) hide show
  1. package/README.md +0 -6
  2. package/actionsExecutor/ActionExecutorContext.tsx +83 -6
  3. package/appUtils/HooksManager/index.ts +35 -0
  4. package/appUtils/focusManager/treeDataStructure/Tree/__tests__/Tree.test.js +46 -0
  5. package/appUtils/focusManager/treeDataStructure/Tree/index.js +18 -18
  6. package/appUtils/focusManagerAux/utils/index.ts +2 -4
  7. package/appUtils/focusManagerAux/utils/utils.ios.ts +6 -3
  8. package/appUtils/localizationsHelper.ts +4 -0
  9. package/appUtils/playerManager/index.ts +9 -0
  10. package/appUtils/playerManager/player.ts +1 -1
  11. package/appUtils/playerManager/playerNative.ts +2 -1
  12. package/appUtils/playerManager/usePlayer.tsx +5 -3
  13. package/cellUtils/__tests__/cellUtils.test.ts +39 -0
  14. package/cellUtils/index.ts +11 -1
  15. package/dateUtils/__tests__/dayjs.test.ts +0 -3
  16. package/dateUtils/index.ts +2 -0
  17. package/manifestUtils/_internals/__tests__/index.test.js +41 -0
  18. package/manifestUtils/_internals/index.js +33 -0
  19. package/manifestUtils/defaultManifestConfigurations/player.js +6 -16
  20. package/manifestUtils/fieldUtils/__tests__/fieldUtils.test.js +49 -0
  21. package/manifestUtils/fieldUtils/index.js +54 -0
  22. package/manifestUtils/index.js +2 -0
  23. package/manifestUtils/keys.js +228 -0
  24. package/manifestUtils/mobileAction/button/__tests__/mobileActionButton.test.js +168 -0
  25. package/manifestUtils/mobileAction/button/index.js +140 -0
  26. package/manifestUtils/mobileAction/container/__tests__/mobileActionButtonsContainer.test.js +102 -0
  27. package/manifestUtils/mobileAction/container/index.js +73 -0
  28. package/manifestUtils/mobileAction/groups/__tests__/buildMobileActionButtonGroups.test.js +127 -0
  29. package/manifestUtils/mobileAction/groups/defaults.js +76 -0
  30. package/manifestUtils/mobileAction/groups/index.js +80 -0
  31. package/manifestUtils/platformIsTV.js +3 -1
  32. package/numberUtils/__tests__/toNumber.test.ts +27 -12
  33. package/numberUtils/__tests__/toPositiveNumber.test.ts +32 -4
  34. package/numberUtils/index.ts +5 -1
  35. package/package.json +3 -3
  36. package/reactHooks/analytics/__tests__/useSendAnalyticsOnPress.test.ts +537 -0
  37. package/reactHooks/cell-click/index.ts +2 -1
  38. package/reactHooks/dev/__tests__/useReRenderLog.test.ts +188 -0
  39. package/reactHooks/device/useIsTablet.tsx +14 -19
  40. package/reactHooks/events/index.ts +20 -0
  41. package/reactHooks/feed/index.ts +0 -2
  42. package/reactHooks/feed/useInflatedUrl.ts +1 -1
  43. package/reactHooks/index.ts +2 -0
  44. package/reactHooks/resolvers/useComponentResolver.ts +11 -1
  45. package/reactHooks/state/__tests__/useComponentScreenState.test.ts +246 -0
  46. package/reactHooks/state/index.ts +2 -0
  47. package/reactHooks/state/useComponentScreenState.ts +45 -0
  48. package/reactHooks/ui/__tests__/useFadeOutWhenBlurred.test.ts +580 -0
  49. package/rectUtils/__tests__/index.test.ts +549 -0
  50. package/rectUtils/index.ts +2 -2
  51. package/refreshUtils/RefreshCoordinator/__tests__/refreshCoordinator.test.ts +206 -0
  52. package/refreshUtils/RefreshCoordinator/index.ts +245 -0
  53. package/refreshUtils/RefreshCoordinator/utils/__tests__/getDataRefreshConfig.test.ts +104 -0
  54. package/refreshUtils/RefreshCoordinator/utils/index.ts +29 -0
  55. package/screenPickerUtils/__tests__/index.test.ts +333 -0
  56. package/screenPickerUtils/index.ts +5 -0
  57. package/screenUtils/index.ts +3 -0
  58. package/time/__tests__/BackgroundTimer.test.ts +156 -0
  59. package/time/__tests__/Timer.test.ts +236 -0
  60. package/typeGuards/__tests__/isString.test.ts +21 -0
  61. package/typeGuards/index.ts +4 -0
  62. package/utils/__tests__/clone.test.ts +158 -0
  63. package/utils/__tests__/path.test.ts +7 -0
  64. package/utils/clone.ts +7 -0
  65. package/utils/index.ts +2 -1
  66. package/reactHooks/componentsMap/index.ts +0 -55
  67. package/reactHooks/feed/__tests__/useFeedRefresh.test.tsx +0 -75
  68. package/reactHooks/feed/useFeedRefresh.tsx +0 -65
@@ -0,0 +1,236 @@
1
+ import { Timer } from "../Timer";
2
+ import BackgroundTimer from "../BackgroundTimer";
3
+
4
+ // Mock BackgroundTimer
5
+ jest.mock("../BackgroundTimer", () => ({
6
+ __esModule: true,
7
+ default: {
8
+ setTimeout: jest.fn((callback, delay) => {
9
+ return setTimeout(callback, delay);
10
+ }),
11
+ clearTimeout: jest.fn((id) => {
12
+ clearTimeout(id as NodeJS.Timeout);
13
+ }),
14
+ },
15
+ }));
16
+
17
+ describe("Timer", () => {
18
+ let mockCallback: jest.Mock;
19
+
20
+ beforeEach(() => {
21
+ jest.clearAllMocks();
22
+ jest.useFakeTimers();
23
+ mockCallback = jest.fn();
24
+ });
25
+
26
+ afterEach(() => {
27
+ jest.useRealTimers();
28
+ });
29
+
30
+ describe("constructor", () => {
31
+ it("should create timer with callback and remaining time", () => {
32
+ const timer = new Timer(mockCallback, 1000);
33
+
34
+ expect(timer).toBeInstanceOf(Timer);
35
+ });
36
+
37
+ it("should create timer with callback only (optional remaining param)", () => {
38
+ const timer = new Timer(mockCallback);
39
+
40
+ expect(timer).toBeInstanceOf(Timer);
41
+ });
42
+
43
+ it("should not start automatically", () => {
44
+ // eslint-disable-next-line no-new
45
+ new Timer(mockCallback, 1000);
46
+
47
+ expect(BackgroundTimer.setTimeout).not.toHaveBeenCalled();
48
+ });
49
+ });
50
+
51
+ describe("start", () => {
52
+ it("should call setTimeout with callback and remaining time", () => {
53
+ const timer = new Timer(mockCallback, 1000);
54
+
55
+ timer.start();
56
+
57
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalledWith(
58
+ mockCallback,
59
+ 1000
60
+ );
61
+ });
62
+
63
+ it("should reset remaining time to 0 after starting", () => {
64
+ const timer = new Timer(mockCallback, 5000);
65
+
66
+ timer.start();
67
+ timer.clear();
68
+ timer.start();
69
+
70
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalledTimes(2);
71
+ });
72
+ });
73
+
74
+ describe("pause", () => {
75
+ it("should clear timeout and calculate remaining time correctly", () => {
76
+ const startTime = 1000;
77
+ const pauseTime = 1500;
78
+
79
+ jest
80
+ .spyOn(Date, "now")
81
+ .mockReturnValueOnce(startTime) // start
82
+ .mockReturnValueOnce(pauseTime); // pause
83
+
84
+ const timer = new Timer(mockCallback, 2000);
85
+
86
+ timer.start();
87
+ timer.pause();
88
+
89
+ expect(BackgroundTimer.clearTimeout).toHaveBeenCalled();
90
+ });
91
+
92
+ it("should be safe to call pause multiple times", () => {
93
+ const timer = new Timer(mockCallback, 1000);
94
+
95
+ timer.start();
96
+ timer.pause();
97
+ timer.pause();
98
+
99
+ expect(BackgroundTimer.clearTimeout).toHaveBeenCalledTimes(2);
100
+ });
101
+ });
102
+
103
+ describe("resume", () => {
104
+ it("should restart timer with remaining time after pause", () => {
105
+ const startTime = 1000;
106
+ const pauseTime = 1500;
107
+
108
+ jest
109
+ .spyOn(Date, "now")
110
+ .mockReturnValueOnce(startTime)
111
+ .mockReturnValueOnce(pauseTime);
112
+
113
+ const timer = new Timer(mockCallback, 2000);
114
+
115
+ timer.start();
116
+ timer.pause();
117
+ timer.resume();
118
+
119
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalledTimes(2);
120
+ });
121
+
122
+ it("should be callable without prior pause", () => {
123
+ const timer = new Timer(mockCallback, 1000);
124
+
125
+ timer.resume();
126
+
127
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalled();
128
+ });
129
+ });
130
+
131
+ describe("clear", () => {
132
+ it("should clear timeout and be safe to call without starting timer", () => {
133
+ const timer = new Timer(mockCallback, 1000);
134
+
135
+ expect(() => {
136
+ timer.clear();
137
+ }).not.toThrow();
138
+
139
+ expect(BackgroundTimer.clearTimeout).toHaveBeenCalled();
140
+ });
141
+
142
+ it("should be callable multiple times", () => {
143
+ const timer = new Timer(mockCallback, 1000);
144
+
145
+ timer.start();
146
+ timer.clear();
147
+ timer.clear();
148
+
149
+ expect(BackgroundTimer.clearTimeout).toHaveBeenCalledTimes(2);
150
+ });
151
+ });
152
+
153
+ describe("timer lifecycle", () => {
154
+ it("should support complete lifecycle: start -> pause -> resume -> clear", () => {
155
+ const timer = new Timer(mockCallback, 1000);
156
+
157
+ timer.start();
158
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalledTimes(1);
159
+
160
+ timer.pause();
161
+ expect(BackgroundTimer.clearTimeout).toHaveBeenCalledTimes(1);
162
+
163
+ timer.resume();
164
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalledTimes(2);
165
+
166
+ timer.clear();
167
+ expect(BackgroundTimer.clearTimeout).toHaveBeenCalledTimes(2);
168
+ });
169
+ });
170
+
171
+ describe("edge cases", () => {
172
+ it("should handle zero delay", () => {
173
+ const timer = new Timer(mockCallback, 0);
174
+
175
+ timer.start();
176
+
177
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalledWith(mockCallback, 0);
178
+ });
179
+
180
+ it("should handle very long delays", () => {
181
+ const longDelay = Number.MAX_SAFE_INTEGER;
182
+ const timer = new Timer(mockCallback, longDelay);
183
+
184
+ timer.start();
185
+
186
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalledWith(
187
+ mockCallback,
188
+ longDelay
189
+ );
190
+ });
191
+
192
+ it("should handle negative delays gracefully", () => {
193
+ const timer = new Timer(mockCallback, -1000);
194
+
195
+ expect(() => {
196
+ timer.start();
197
+ }).not.toThrow();
198
+ });
199
+
200
+ it("should maintain callback reference through lifecycle", () => {
201
+ const specificCallback = jest.fn();
202
+ const timer = new Timer(specificCallback, 1000);
203
+
204
+ timer.start();
205
+
206
+ expect(BackgroundTimer.setTimeout).toHaveBeenCalledWith(
207
+ specificCallback,
208
+ 1000
209
+ );
210
+ });
211
+ });
212
+
213
+ describe("callback execution safety", () => {
214
+ it("should not execute callback immediately on start", () => {
215
+ const timer = new Timer(mockCallback, 1000);
216
+
217
+ timer.start();
218
+
219
+ expect(mockCallback).not.toHaveBeenCalled();
220
+ });
221
+
222
+ it("should not execute callback when paused or cleared", () => {
223
+ const timer1 = new Timer(mockCallback, 100);
224
+ const timer2 = new Timer(mockCallback, 100);
225
+
226
+ timer1.start();
227
+ timer1.pause();
228
+ timer2.start();
229
+ timer2.clear();
230
+
231
+ jest.advanceTimersByTime(200);
232
+
233
+ expect(mockCallback).not.toHaveBeenCalled();
234
+ });
235
+ });
236
+ });
@@ -0,0 +1,21 @@
1
+ import { isString } from "../index";
2
+
3
+ describe("isString", () => {
4
+ it("should return true for string primitives", () => {
5
+ expect(isString("hello")).toBe(true);
6
+ expect(isString("")).toBe(true);
7
+ });
8
+
9
+ it("should return true for String object instances", () => {
10
+ // eslint-disable-next-line no-new-wrappers
11
+ expect(isString(new String("test"))).toBe(true);
12
+ });
13
+
14
+ it("should return false for non-string values", () => {
15
+ expect(isString(123)).toBe(false);
16
+ expect(isString(null)).toBe(false);
17
+ expect(isString(undefined)).toBe(false);
18
+ expect(isString({})).toBe(false);
19
+ expect(isString([])).toBe(false);
20
+ });
21
+ });
@@ -1,3 +1,7 @@
1
1
  export const isString = (value: unknown) => {
2
+ if (typeof value === "object" && value instanceof String) {
3
+ return true;
4
+ }
5
+
2
6
  return typeof value === "string";
3
7
  };
@@ -0,0 +1,158 @@
1
+ import { clone } from "../clone";
2
+
3
+ describe("clone", () => {
4
+ const originalStructuredClone = global.structuredClone;
5
+
6
+ afterEach(() => {
7
+ // Restore the original structuredClone after each test
8
+ global.structuredClone = originalStructuredClone;
9
+ });
10
+
11
+ describe("when structuredClone is available", () => {
12
+ beforeEach(() => {
13
+ // Mock structuredClone if it's not available
14
+ if (typeof global.structuredClone === "undefined") {
15
+ global.structuredClone = jest.fn((value) =>
16
+ JSON.parse(JSON.stringify(value))
17
+ );
18
+ }
19
+ });
20
+
21
+ test("clones primitive values", () => {
22
+ expect(clone(42)).toBe(42);
23
+ expect(clone("test")).toBe("test");
24
+ expect(clone(true)).toBe(true);
25
+ expect(clone(null)).toBe(null);
26
+ });
27
+
28
+ test("clones simple objects", () => {
29
+ const original = { a: 1, b: 2 };
30
+ const cloned = clone(original);
31
+
32
+ expect(cloned).toEqual(original);
33
+ expect(cloned).not.toBe(original);
34
+ });
35
+
36
+ test("clones nested objects", () => {
37
+ const original = { a: { b: { c: 1 } } };
38
+ const cloned = clone(original);
39
+
40
+ expect(cloned).toEqual(original);
41
+ expect(cloned).not.toBe(original);
42
+ expect(cloned.a).not.toBe(original.a);
43
+ expect(cloned.a.b).not.toBe(original.a.b);
44
+ });
45
+
46
+ test("clones arrays", () => {
47
+ const original = [1, 2, 3];
48
+ const cloned = clone(original);
49
+
50
+ expect(cloned).toEqual(original);
51
+ expect(cloned).not.toBe(original);
52
+ });
53
+
54
+ test("clones arrays with nested objects", () => {
55
+ const original = [{ a: 1 }, { b: 2 }];
56
+ const cloned = clone(original);
57
+
58
+ expect(cloned).toEqual(original);
59
+ expect(cloned).not.toBe(original);
60
+ expect(cloned[0]).not.toBe(original[0]);
61
+ expect(cloned[1]).not.toBe(original[1]);
62
+ });
63
+
64
+ test("does not mutate the original value", () => {
65
+ const original = { a: 1, b: { c: 2 } };
66
+ const cloned = clone(original);
67
+
68
+ cloned.a = 99;
69
+ cloned.b.c = 99;
70
+
71
+ expect(original.a).toBe(1);
72
+ expect(original.b.c).toBe(2);
73
+ });
74
+ });
75
+
76
+ describe("when structuredClone is not available (fallback to cloneDeep)", () => {
77
+ beforeEach(() => {
78
+ // Delete structuredClone to force fallback to cloneDeep
79
+ delete (global as any).structuredClone;
80
+ });
81
+
82
+ test("clones primitive values using cloneDeep", () => {
83
+ expect(clone(42)).toBe(42);
84
+ expect(clone("test")).toBe("test");
85
+ expect(clone(true)).toBe(true);
86
+ expect(clone(null)).toBe(null);
87
+ });
88
+
89
+ test("clones simple objects using cloneDeep", () => {
90
+ const original = { a: 1, b: 2 };
91
+ const cloned = clone(original);
92
+
93
+ expect(cloned).toEqual(original);
94
+ expect(cloned).not.toBe(original);
95
+ });
96
+
97
+ test("clones nested objects using cloneDeep", () => {
98
+ const original = { a: { b: { c: 1 } } };
99
+ const cloned = clone(original);
100
+
101
+ expect(cloned).toEqual(original);
102
+ expect(cloned).not.toBe(original);
103
+ expect(cloned.a).not.toBe(original.a);
104
+ expect(cloned.a.b).not.toBe(original.a.b);
105
+ });
106
+
107
+ test("clones arrays using cloneDeep", () => {
108
+ const original = [1, 2, 3];
109
+ const cloned = clone(original);
110
+
111
+ expect(cloned).toEqual(original);
112
+ expect(cloned).not.toBe(original);
113
+ });
114
+
115
+ test("clones arrays with nested objects using cloneDeep", () => {
116
+ const original = [{ a: 1 }, { b: 2 }];
117
+ const cloned = clone(original);
118
+
119
+ expect(cloned).toEqual(original);
120
+ expect(cloned).not.toBe(original);
121
+ expect(cloned[0]).not.toBe(original[0]);
122
+ expect(cloned[1]).not.toBe(original[1]);
123
+ });
124
+
125
+ test("does not mutate the original value when using cloneDeep", () => {
126
+ const original = { a: 1, b: { c: 2 } };
127
+ const cloned = clone(original);
128
+
129
+ cloned.a = 99;
130
+ cloned.b.c = 99;
131
+
132
+ expect(original.a).toBe(1);
133
+ expect(original.b.c).toBe(2);
134
+ });
135
+
136
+ test("handles complex objects with multiple levels", () => {
137
+ const original = {
138
+ name: "test",
139
+ data: {
140
+ items: [
141
+ { id: 1, value: "a" },
142
+ { id: 2, value: "b" },
143
+ ],
144
+ meta: { count: 2 },
145
+ },
146
+ };
147
+
148
+ const cloned = clone(original);
149
+
150
+ expect(cloned).toEqual(original);
151
+ expect(cloned).not.toBe(original);
152
+ expect(cloned.data).not.toBe(original.data);
153
+ expect(cloned.data.items).not.toBe(original.data.items);
154
+ expect(cloned.data.items[0]).not.toBe(original.data.items[0]);
155
+ expect(cloned.data.meta).not.toBe(original.data.meta);
156
+ });
157
+ });
158
+ });
@@ -31,3 +31,10 @@ test("example 4", () => {
31
31
 
32
32
  expect(path(route, xs)).toBeUndefined();
33
33
  });
34
+
35
+ test("example 5", () => {
36
+ const route = ["a", "b", 0];
37
+ const xs = { a: { b: [1, 2, 3] } };
38
+
39
+ expect(path(route, xs)).toBe(1);
40
+ });
package/utils/clone.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { cloneDeep } from "lodash";
2
+
3
+ export const clone = (value) => {
4
+ return typeof structuredClone !== "undefined"
5
+ ? structuredClone(value) // no support for hermes, fallback to cloneDeep
6
+ : cloneDeep(value);
7
+ };
package/utils/index.ts CHANGED
@@ -20,8 +20,9 @@ export { mapAccum } from "./mapAccum";
20
20
 
21
21
  export { mergeRight } from "./mergeRight";
22
22
 
23
+ export { clone } from "./clone";
24
+
23
25
  export {
24
- cloneDeep as clone,
25
26
  flatten,
26
27
  drop,
27
28
  size,
@@ -1,55 +0,0 @@
1
- import * as React from "react";
2
-
3
- const layoutReducer = (state, { payload }) => {
4
- return state.map((item, index, _state) => ({
5
- height: index === payload.index ? payload.height : item.height,
6
- y:
7
- index > 0
8
- ? _state.slice(0, index).reduce((acc, value) => acc + value.height, 0)
9
- : 0,
10
- }));
11
- };
12
-
13
- export const useComponentsLayout = (count, onFinishLoadingVisible) => {
14
- const [itemsLayout, dispatchItemLayout] = React.useReducer(
15
- layoutReducer,
16
- Array.from({ length: count }, () => ({ height: 0, y: 0 }))
17
- );
18
-
19
- const notified = React.useRef(false);
20
- const [listHeight, setListHeight] = React.useState<number>(0);
21
-
22
- const onItemLayout = React.useCallback(
23
- (index) => (event) => {
24
- const { height } = event.nativeEvent.layout;
25
- dispatchItemLayout({ payload: { index, height } });
26
- },
27
- []
28
- );
29
-
30
- const onListLayout = React.useCallback((event) => {
31
- const { height } = event.nativeEvent.layout;
32
- setListHeight(height);
33
- }, []);
34
-
35
- React.useEffect(() => {
36
- if (!notified.current) {
37
- const finishLoadingVisible = !!itemsLayout.find(
38
- (item) => item.y > listHeight
39
- );
40
-
41
- if (finishLoadingVisible) {
42
- notified.current = true;
43
- onFinishLoadingVisible?.();
44
- }
45
- }
46
- }, [itemsLayout, listHeight]);
47
-
48
- return React.useMemo(
49
- () => ({
50
- onItemLayout,
51
- onListLayout,
52
- }),
53
- []
54
- );
55
- };
@@ -1,75 +0,0 @@
1
- import { renderHook } from "@testing-library/react-native";
2
-
3
- jest.useFakeTimers();
4
-
5
- jest.mock("@applicaster/zapp-react-native-utils/reactHooks/navigation", () => ({
6
- useIsScreenActive: () => true,
7
- }));
8
-
9
- const { useFeedRefresh, feedRefreshLogger } = require("..");
10
-
11
- describe("useFeedRefresh", () => {
12
- const reloadData = jest.fn();
13
-
14
- const component = {
15
- id: "foo",
16
- rules: { enable_data_refreshing: true, refreshing_interval: 61 },
17
- };
18
-
19
- it("Calls reloadData after passed time lapses", () => {
20
- renderHook(() => useFeedRefresh({ reloadData, component }));
21
-
22
- expect(reloadData).not.toBeCalled();
23
- jest.runOnlyPendingTimers();
24
- expect(reloadData).toBeCalled();
25
- });
26
-
27
- it("Logs warning message if refresh time set below minimum value", () => {
28
- const loggerSpy = jest.spyOn(feedRefreshLogger, "warning");
29
-
30
- renderHook(() =>
31
- useFeedRefresh({
32
- reloadData,
33
- component: {
34
- ...component,
35
- rules: { enable_data_refreshing: true, refreshing_interval: 2 },
36
- },
37
- })
38
- );
39
-
40
- expect(loggerSpy).toBeCalled();
41
- });
42
-
43
- it("Calls reloadData on a Component that had a timer setup and was re-mounted", () => {
44
- reloadData.mockReset();
45
-
46
- // it is not called before rendering hook"
47
- expect(reloadData).not.toBeCalled();
48
-
49
- // it is not called when rendering new component (Not previously rendered)
50
- renderHook(() =>
51
- useFeedRefresh({ reloadData, component: { ...component, id: "bar" } })
52
- );
53
-
54
- expect(reloadData).not.toBeCalled();
55
-
56
- // it is called when rendering previously rendered component",
57
- renderHook(() => useFeedRefresh({ reloadData, component }));
58
- expect(reloadData).toBeCalled();
59
- });
60
-
61
- it("Doesn't refresh data when enable_data_refreshing if false", () => {
62
- reloadData.mockReset();
63
-
64
- renderHook(() =>
65
- useFeedRefresh({
66
- reloadData,
67
- component: { ...component, rules: { enable_data_refreshing: false } },
68
- })
69
- );
70
-
71
- jest.runOnlyPendingTimers();
72
-
73
- expect(reloadData).not.toBeCalled();
74
- });
75
- });
@@ -1,65 +0,0 @@
1
- import * as React from "react";
2
- import { path, max } from "ramda";
3
- import { isTrue } from "@applicaster/zapp-react-native-utils/booleanUtils";
4
- import { toNumberWithDefault } from "@applicaster/zapp-react-native-utils/numberUtils";
5
- import { useIsScreenActive } from "@applicaster/zapp-react-native-utils/reactHooks/navigation";
6
-
7
- import { reactHooksLogger } from "../logger";
8
- import { ReloadDataFunction } from "./useFeedLoader";
9
-
10
- export const feedRefreshLogger = reactHooksLogger.addSubsystem("feed-refresh");
11
-
12
- type Props = {
13
- reloadData?: ReloadDataFunction;
14
- component: ZappUIComponent;
15
- };
16
-
17
- const previousTimersMap = {};
18
- const minimumRefreshingInterval = 5;
19
- const defaultRefreshInterval = 60;
20
-
21
- export const useFeedRefresh = ({ reloadData, component }: Props): void => {
22
- const isDataRefreshingEnabled = isTrue(
23
- path(["rules", "enable_data_refreshing"], component)
24
- );
25
-
26
- const refreshing_interval = toNumberWithDefault(
27
- defaultRefreshInterval,
28
- component?.rules?.refreshing_interval
29
- );
30
-
31
- const refreshingIntervalInMilliseconds =
32
- max(refreshing_interval, minimumRefreshingInterval) * 1000;
33
-
34
- const isScreenActive = useIsScreenActive();
35
-
36
- React.useEffect(() => {
37
- if (isDataRefreshingEnabled && refreshing_interval && isScreenActive) {
38
- if (refreshing_interval < minimumRefreshingInterval) {
39
- feedRefreshLogger.warning({
40
- message: `You set your feed refresh interval to ${refreshing_interval} when minimum value is ${minimumRefreshingInterval}seconds.Your feed will refresh after ${minimumRefreshingInterval}seconds.`,
41
- });
42
- }
43
-
44
- if (!reloadData || typeof reloadData !== "function") {
45
- feedRefreshLogger.warning({
46
- message:
47
- "reloadData function is undefined, feed refresh feature won't work properly",
48
- });
49
- }
50
-
51
- if (previousTimersMap[component?.id]) {
52
- reloadData?.();
53
- }
54
-
55
- const refreshInterval = setInterval(() => {
56
- reloadData?.();
57
- }, refreshingIntervalInMilliseconds);
58
-
59
- return () => {
60
- previousTimersMap[component?.id] = true;
61
- clearInterval(refreshInterval);
62
- };
63
- }
64
- }, [reloadData, isScreenActive]);
65
- };