@applicaster/zapp-react-native-utils 15.1.4 → 16.0.0-alpha.1042942411
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/README.md +0 -6
- package/actionUtils/index.ts +7 -0
- package/actionsExecutor/ActionExecutor.ts +8 -10
- package/actionsExecutor/ActionExecutorContext.tsx +45 -340
- package/actionsExecutor/actions/appRestart.ts +24 -0
- package/actionsExecutor/actions/confirmDialog.ts +53 -0
- package/actionsExecutor/actions/index.ts +30 -0
- package/actionsExecutor/actions/localStorageRemove.ts +27 -0
- package/actionsExecutor/actions/localStorageSet.ts +28 -0
- package/actionsExecutor/actions/localStorageToggleFlag.ts +28 -0
- package/actionsExecutor/actions/navigateToScreen.ts +123 -0
- package/actionsExecutor/actions/refreshComponent.ts +79 -0
- package/actionsExecutor/actions/screenSetVariable.ts +35 -0
- package/actionsExecutor/actions/screenToggleFlag.ts +38 -0
- package/actionsExecutor/actions/sendCloudEvent.ts +88 -0
- package/actionsExecutor/actions/sessionStorageRemove.ts +19 -0
- package/actionsExecutor/actions/sessionStorageSet.ts +20 -0
- package/actionsExecutor/actions/sessionStorageToggleFlag.ts +15 -0
- package/actionsExecutor/actions/showToast.ts +31 -0
- package/actionsExecutor/actions/switchLayout.ts +40 -0
- package/actionsExecutor/feedDecorator.ts +6 -6
- package/actionsExecutor/types.ts +59 -0
- package/adsUtils/__tests__/createVMAP.test.ts +419 -0
- package/adsUtils/index.ts +2 -2
- package/analyticsUtils/README.md +1 -1
- package/appDataUtils/__tests__/urlScheme.test.ts +678 -0
- package/appUtils/HooksManager/__tests__/__snapshots__/hooksManager.test.js.snap +0 -188
- package/appUtils/HooksManager/__tests__/hooksManager.test.js +16 -2
- package/appUtils/HooksManager/index.ts +12 -18
- package/appUtils/RiverFocusManager/{index.js → index.ts} +25 -18
- package/appUtils/accessibilityManager/const.ts +4 -0
- package/appUtils/accessibilityManager/utils.ts +0 -1
- package/appUtils/contextKeysManager/__tests__/getKeys/failure.test.ts +7 -2
- package/appUtils/contextKeysManager/__tests__/getKeys/success.test.ts +48 -0
- package/appUtils/contextKeysManager/contextResolver.ts +51 -22
- package/appUtils/contextKeysManager/index.ts +65 -10
- package/appUtils/contextKeysManager/utils/index.ts +38 -25
- package/appUtils/focusManager/__tests__/__snapshots__/focusManager.test.js.snap +3 -0
- package/appUtils/focusManager/index.ios.ts +43 -4
- package/appUtils/focusManager/treeDataStructure/Tree/__tests__/Tree.test.js +46 -0
- package/appUtils/focusManager/treeDataStructure/Tree/index.js +18 -18
- package/appUtils/focusManagerAux/utils/index.ios.ts +122 -0
- package/appUtils/focusManagerAux/utils/index.ts +11 -3
- package/appUtils/focusManagerAux/utils/utils.ios.ts +202 -3
- package/appUtils/keyCodes/keys/keys.web.ts +1 -4
- package/appUtils/orientationHelper.ts +2 -4
- package/appUtils/platform/platformUtils.ts +12 -1
- package/appUtils/playerManager/player.ts +4 -0
- package/appUtils/playerManager/playerNative.ts +30 -21
- package/appUtils/playerManager/usePlayerState.tsx +14 -2
- package/bottomSheet/__tests__/scrollContext.test.tsx +35 -0
- package/bottomSheet/index.ts +17 -0
- package/cloudEventsUtils/__tests__/index.test.ts +529 -0
- package/cloudEventsUtils/index.ts +65 -1
- package/componentsUtils/index.ts +8 -0
- package/configurationUtils/__tests__/manifestKeyParser.test.ts +26 -26
- package/enumUtils/__tests__/getEnumKeyByEnumValue.test.ts +207 -0
- package/errorUtils/__tests__/GeneralError.test.ts +97 -0
- package/errorUtils/__tests__/HttpStatusCode.test.ts +344 -0
- package/errorUtils/__tests__/MissingPluginError.test.ts +113 -0
- package/errorUtils/__tests__/NetworkError.test.ts +202 -0
- package/errorUtils/__tests__/getParsedResponse.test.ts +188 -0
- package/errorUtils/__tests__/invariant.test.ts +112 -0
- package/focusManager/aux/index.ts +1 -1
- package/headersUtils/__tests__/headersUtils.test.js +11 -1
- package/headersUtils/index.ts +2 -1
- package/manifestUtils/_internals/__tests__/index.test.js +41 -0
- package/manifestUtils/_internals/index.js +33 -0
- package/manifestUtils/defaultManifestConfigurations/generalContent.js +35 -0
- package/manifestUtils/defaultManifestConfigurations/player.js +63 -5
- package/manifestUtils/index.js +3 -1
- package/manifestUtils/keys.js +236 -0
- package/manifestUtils/mobileAction/button/__tests__/mobileActionButton.test.js +168 -0
- package/manifestUtils/mobileAction/button/index.js +156 -0
- package/manifestUtils/mobileAction/container/__tests__/mobileActionButtonsContainer.test.js +102 -0
- package/manifestUtils/mobileAction/container/index.js +75 -0
- package/manifestUtils/mobileAction/groups/__tests__/buildMobileActionButtonGroups.test.js +127 -0
- package/manifestUtils/mobileAction/groups/defaults.js +78 -0
- package/manifestUtils/mobileAction/groups/index.js +80 -0
- package/manifestUtils/tvAction/container/index.js +1 -1
- package/numberUtils/__tests__/isMinusZero.test.ts +20 -0
- package/numberUtils/__tests__/isZero.test.ts +27 -0
- package/numberUtils/__tests__/requireNumber.test.ts +50 -0
- package/numberUtils/__tests__/toNumber.test.ts +54 -0
- package/numberUtils/__tests__/toNumberWithDefault.test.ts +64 -0
- package/numberUtils/__tests__/toNumberWithDefaultZero.test.ts +48 -0
- package/numberUtils/__tests__/toPositiveNumber.test.ts +193 -0
- package/numberUtils/index.ts +46 -5
- package/package.json +4 -4
- package/pluginUtils/index.ts +4 -0
- package/reactHooks/actions/index.ts +51 -1
- package/reactHooks/advertising/index.ts +2 -2
- package/reactHooks/analytics/__tests__/useSendAnalyticsOnPress.test.ts +537 -0
- package/reactHooks/app/__tests__/useAppState.test.ts +1 -1
- package/reactHooks/autoscrolling/__tests__/useTrackCurrentAutoScrollingElement.test.ts +1 -1
- package/reactHooks/autoscrolling/__tests__/useTrackedView.test.tsx +1 -2
- package/reactHooks/cell-click/__tests__/index.test.js +1 -3
- package/reactHooks/cell-click/index.ts +15 -7
- package/reactHooks/configuration/__tests__/index.test.tsx +1 -1
- package/reactHooks/connection/__tests__/index.test.js +1 -1
- package/reactHooks/debugging/__tests__/index.test.js +4 -4
- package/reactHooks/dev/__tests__/useReRenderLog.test.ts +188 -0
- package/reactHooks/device/useIsTablet.tsx +14 -19
- package/reactHooks/device/useMemoizedIsTablet.ts +3 -3
- package/reactHooks/feed/__mocks__/useMarkPipesDataStale.ts +4 -0
- package/reactHooks/feed/__tests__/useBatchLoading.test.tsx +32 -23
- package/reactHooks/feed/__tests__/useBuildPipesUrl.test.tsx +19 -19
- package/reactHooks/feed/__tests__/useEntryScreenId.test.tsx +4 -1
- package/reactHooks/feed/__tests__/useFeedLoader.test.tsx +42 -30
- package/reactHooks/feed/__tests__/useInflatedUrl.test.tsx +26 -1
- package/reactHooks/feed/index.ts +5 -3
- package/reactHooks/feed/useBatchLoading.ts +17 -3
- package/reactHooks/feed/useEntryScreenId.ts +2 -2
- package/reactHooks/feed/{usePipesCacheReset.ts → useMarkPipesDataStale.ts} +14 -4
- package/reactHooks/flatList/useLoadNextPageIfNeeded.ts +13 -16
- package/reactHooks/layout/__tests__/index.test.tsx +1 -1
- package/reactHooks/layout/__tests__/useLayoutVersion.test.tsx +1 -1
- package/reactHooks/layout/useDimensions/__tests__/{useDimensions.test.ts → useDimensions.test.tsx} +105 -25
- package/reactHooks/layout/useDimensions/useDimensions.ts +2 -2
- package/reactHooks/navigation/__tests__/index.test.tsx +2 -4
- package/reactHooks/navigation/index.ts +7 -6
- package/reactHooks/navigation/useRoute.ts +8 -6
- package/reactHooks/player/TVSeekControlller/TVSeekController.ts +27 -10
- package/reactHooks/player/__tests__/useAutoSeek._test.tsx +1 -1
- package/reactHooks/player/__tests__/useTapSeek._test.ts +1 -1
- package/reactHooks/resolvers/__tests__/useCellResolver.test.tsx +1 -1
- package/reactHooks/resolvers/__tests__/useComponentResolver.test.tsx +1 -1
- package/reactHooks/resolvers/useCellResolver.ts +6 -2
- package/reactHooks/resolvers/useComponentResolver.ts +8 -2
- package/reactHooks/screen/__tests__/useCurrentScreenData.test.tsx +2 -2
- package/reactHooks/screen/__tests__/useIsStandaloneFullscreen.test.ts +114 -0
- package/reactHooks/screen/__tests__/useScreenBackgroundColor.test.tsx +1 -1
- package/reactHooks/screen/__tests__/useScreenData.test.tsx +1 -1
- package/reactHooks/screen/__tests__/useTargetScreenData.test.tsx +12 -4
- package/reactHooks/screen/useTargetScreenData.ts +4 -2
- package/reactHooks/state/useComponentScreenState.ts +1 -1
- package/reactHooks/state/useRivers.ts +1 -1
- package/reactHooks/ui/__tests__/useFadeOutWhenBlurred.test.ts +580 -0
- package/reactHooks/usePluginConfiguration.ts +2 -2
- package/reactHooks/utils/__tests__/index.test.js +1 -1
- package/reactHooks/utils/index.ts +3 -2
- package/rectUtils/__tests__/index.test.ts +549 -0
- package/rectUtils/index.ts +2 -2
- package/refreshUtils/RefreshCoordinator/__tests__/refreshCoordinator.test.ts +206 -0
- package/refreshUtils/RefreshCoordinator/index.ts +245 -0
- package/refreshUtils/RefreshCoordinator/utils/__tests__/getDataRefreshConfig.test.ts +104 -0
- package/refreshUtils/RefreshCoordinator/utils/index.ts +29 -0
- package/riverComponetsMeasurementProvider/index.tsx +1 -1
- package/screenPickerUtils/__tests__/index.test.ts +333 -0
- package/screenState/__tests__/index.test.ts +1 -1
- package/screenUtils/index.ts +3 -0
- package/searchUtils/const.ts +7 -0
- package/searchUtils/index.ts +3 -0
- package/stringUtils/__tests__/stringUtils.test.js +42 -0
- package/stringUtils/index.ts +3 -3
- package/testUtils/index.tsx +30 -21
- package/time/__tests__/BackgroundTimer.test.ts +156 -0
- package/time/__tests__/Timer.test.ts +236 -0
- package/typeGuards/__tests__/isString.test.ts +21 -0
- package/typeGuards/index.ts +4 -0
- package/uiActionsRegistrator/index.ts +203 -0
- package/utils/__tests__/mergeRight.test.ts +48 -0
- package/utils/__tests__/path.test.ts +7 -0
- package/utils/__tests__/selectors.test.ts +124 -0
- package/utils/index.ts +13 -0
- package/utils/mergeRight.ts +5 -0
- package/utils/path.ts +6 -3
- package/utils/pathOr.ts +5 -1
- package/utils/selectors.ts +46 -0
- package/zappFrameworkUtils/HookCallback/callbackNavigationAction.ts +1 -1
- package/zappFrameworkUtils/HookCallback/hookCallbackManifestExtensions.config.js +1 -1
- package/zappFrameworkUtils/loginPluginUtils.ts +1 -1
- package/reactHooks/componentsMap/index.ts +0 -55
- package/reactHooks/feed/__mocks__/usePipesCacheReset.ts +0 -1
- package/reactHooks/feed/__tests__/useFeedRefresh.test.tsx +0 -75
- 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
|
+
});
|
package/typeGuards/index.ts
CHANGED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/* eslint-disable no-dupe-class-members */
|
|
2
|
+
import { Observable } from "rxjs";
|
|
3
|
+
import { createLogger } from "../logger";
|
|
4
|
+
|
|
5
|
+
const { log_debug, log_warning, log_verbose } = createLogger({
|
|
6
|
+
subsystem: "UIActionsRegistry",
|
|
7
|
+
category: "ActionRegistration",
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* The runtime value of a UI action.
|
|
12
|
+
*
|
|
13
|
+
* This is the object that consumers interact with (render buttons for,
|
|
14
|
+
* invoke on press, read initial state from, etc.). It intentionally keeps an
|
|
15
|
+
* open shape (`[K: string]: any`) so that both the modern registry-based
|
|
16
|
+
* actions and the legacy context-provider based actions can be represented by
|
|
17
|
+
* the same structure.
|
|
18
|
+
*/
|
|
19
|
+
export type RegisteredActionValue = {
|
|
20
|
+
state?: any;
|
|
21
|
+
getInitialState?: () => Promise<void>;
|
|
22
|
+
initialEntryState?: (item: ZappEntry | ZappFeed) => CellActionEntryState;
|
|
23
|
+
invokeAction: (
|
|
24
|
+
item: ZappEntry | ZappFeed,
|
|
25
|
+
options?: InvokeArgsOptions
|
|
26
|
+
) => void;
|
|
27
|
+
isActionAvailable?: (item: ZappEntry | ZappFeed) => boolean;
|
|
28
|
+
addListener?: (
|
|
29
|
+
entryId: string,
|
|
30
|
+
listener: CellActionStateListenerFunction
|
|
31
|
+
) => CellActionStateRemoveListenerFunction;
|
|
32
|
+
/**
|
|
33
|
+
* Optional RX stream of the action state for a given entry. When not
|
|
34
|
+
* provided, `observeEntryState` derives one from `initialEntryState` +
|
|
35
|
+
* `addListener` (see below).
|
|
36
|
+
*/
|
|
37
|
+
observeEntryState?: (
|
|
38
|
+
entry: ZappEntry | ZappFeed
|
|
39
|
+
) => Observable<CellActionEntryState>;
|
|
40
|
+
[K: string]: any;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export type RegisteredAction = {
|
|
44
|
+
identifier: string; // Plugin identifier or synthetic name
|
|
45
|
+
action: RegisteredActionValue;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* A function that, given an optional context (currently just the `entry`),
|
|
50
|
+
* returns the list of actions it wants to contribute.
|
|
51
|
+
*
|
|
52
|
+
* Returning an array is what allows a single provider (plugin or otherwise) to
|
|
53
|
+
* expose more than one action, and to compute those actions based on the entry.
|
|
54
|
+
*/
|
|
55
|
+
export type UIActionProvider = (params: {
|
|
56
|
+
entry?: ZappEntry | ZappFeed;
|
|
57
|
+
}) => RegisteredAction[];
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Returns an RX `Observable` that emits the action state for `entry`.
|
|
61
|
+
*
|
|
62
|
+
* If the action already exposes its own `observeEntryState`, it is used as-is.
|
|
63
|
+
* Otherwise we build the stream from the legacy imperative API:
|
|
64
|
+
* - it immediately emits `initialEntryState(entry)` (if available), and
|
|
65
|
+
* - it subscribes to further changes via `addListener(entryId, listener)`,
|
|
66
|
+
* unsubscribing automatically when the observable is torn down.
|
|
67
|
+
*
|
|
68
|
+
* This lets every action - old or new - be consumed reactively through a single
|
|
69
|
+
* mechanism, without each consumer having to wire up `addListener` manually.
|
|
70
|
+
*/
|
|
71
|
+
export function observeEntryState(
|
|
72
|
+
action: RegisteredActionValue | undefined,
|
|
73
|
+
entry: ZappEntry | ZappFeed
|
|
74
|
+
): Observable<CellActionEntryState> {
|
|
75
|
+
if (typeof action?.observeEntryState === "function") {
|
|
76
|
+
return action.observeEntryState(entry);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
return new Observable<CellActionEntryState>((subscriber) => {
|
|
80
|
+
const initialState = action?.initialEntryState?.(entry);
|
|
81
|
+
|
|
82
|
+
if (initialState !== undefined) {
|
|
83
|
+
subscriber.next(initialState);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const removeListener = action?.addListener?.(
|
|
87
|
+
String((entry as ZappEntry)?.id),
|
|
88
|
+
(state: CellActionEntryState) => subscriber.next(state)
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
return () => removeListener?.();
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
class UIActionsRegistry {
|
|
96
|
+
private registeredActions: Record<string, UIActionProvider> = {};
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Register a dynamic action provider under a unique `name`.
|
|
100
|
+
* The provider can return one or more actions and may use the passed `entry`.
|
|
101
|
+
* Returns an unregister function.
|
|
102
|
+
*/
|
|
103
|
+
registerActionProvider(
|
|
104
|
+
name: string,
|
|
105
|
+
actionProvider: UIActionProvider
|
|
106
|
+
): () => void {
|
|
107
|
+
const isOverride = Boolean(this.registeredActions[name]);
|
|
108
|
+
|
|
109
|
+
if (isOverride) {
|
|
110
|
+
log_warning(
|
|
111
|
+
`registerActionProvider: overriding existing provider for "${name}"`,
|
|
112
|
+
{ name }
|
|
113
|
+
);
|
|
114
|
+
} else {
|
|
115
|
+
log_debug(`registerActionProvider: registered "${name}"`, { name });
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
this.registeredActions[name] = actionProvider;
|
|
119
|
+
|
|
120
|
+
return () => {
|
|
121
|
+
// Only remove if it's still the same provider to avoid races where a
|
|
122
|
+
// newer registration replaced this one.
|
|
123
|
+
if (this.registeredActions[name] === actionProvider) {
|
|
124
|
+
delete this.registeredActions[name];
|
|
125
|
+
log_debug(`registerActionProvider: unregistered "${name}"`, { name });
|
|
126
|
+
} else {
|
|
127
|
+
log_debug(
|
|
128
|
+
`registerActionProvider: skipped unregister for "${name}" – newer provider is active`,
|
|
129
|
+
{ name }
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Convenience helper to register a single, static action from anywhere in the
|
|
137
|
+
* app (it does not need to be a plugin). Returns an unregister function.
|
|
138
|
+
*/
|
|
139
|
+
registerAction(
|
|
140
|
+
identifier: string,
|
|
141
|
+
action: RegisteredActionValue
|
|
142
|
+
): () => void {
|
|
143
|
+
log_debug(`registerAction: registering single action "${identifier}"`, {
|
|
144
|
+
identifier,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
return this.registerActionProvider(identifier, () => [
|
|
148
|
+
{ identifier, action },
|
|
149
|
+
]);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
getEntryActions(entry: ZappEntry): RegisteredAction[] {
|
|
153
|
+
const results = Object.keys(this.registeredActions)
|
|
154
|
+
.map((key) => this.registeredActions[key]({ entry }))
|
|
155
|
+
.flatMap((a) => a);
|
|
156
|
+
|
|
157
|
+
log_verbose(
|
|
158
|
+
`getEntryActions: resolved ${results.length} action(s) for entry "${entry?.id}"`,
|
|
159
|
+
{ entryId: entry?.id, identifiers: results.map((r) => r.identifier) }
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
return results;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
getActions(name: string, entry?: ZappEntry | ZappFeed): RegisteredAction[] {
|
|
166
|
+
if (!this.registeredActions[name]) {
|
|
167
|
+
log_warning(`getActions: no provider found for "${name}"`, { name });
|
|
168
|
+
|
|
169
|
+
return [];
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const results = this.registeredActions[name]({ entry }) ?? [];
|
|
173
|
+
|
|
174
|
+
log_verbose(
|
|
175
|
+
`getActions: resolved ${results.length} action(s) for "${name}"`,
|
|
176
|
+
{
|
|
177
|
+
name,
|
|
178
|
+
entryId: (entry as ZappEntry)?.id,
|
|
179
|
+
identifiers: results.map((r) => r.identifier),
|
|
180
|
+
}
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
return results;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Returns the runtime value of the first action registered under `name`, or
|
|
188
|
+
* `undefined`. This mirrors the shape returned by the legacy `useActions`
|
|
189
|
+
* hook, easing migration of single-action consumers.
|
|
190
|
+
*/
|
|
191
|
+
getAction(
|
|
192
|
+
name: string,
|
|
193
|
+
entry?: ZappEntry | ZappFeed
|
|
194
|
+
): RegisteredActionValue | undefined {
|
|
195
|
+
return this.getActions(name, entry)[0]?.action;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
hasAction(name: string): boolean {
|
|
199
|
+
return Boolean(this.registeredActions[name]);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
export const uiActionsRegistry = new UIActionsRegistry();
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { mergeRight } from "../mergeRight";
|
|
2
|
+
|
|
3
|
+
describe("mergeRight", () => {
|
|
4
|
+
test("merges two objects with no overlapping keys", () => {
|
|
5
|
+
const a = { x: 1, y: 2 };
|
|
6
|
+
const b = { z: 3 };
|
|
7
|
+
|
|
8
|
+
const result = mergeRight(a, b);
|
|
9
|
+
|
|
10
|
+
expect(result).toEqual({ x: 1, y: 2, z: 3 });
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test("overwrites keys from the second object", () => {
|
|
14
|
+
const a = { x: 1, y: 2 };
|
|
15
|
+
const b = { y: 10, z: 3 };
|
|
16
|
+
|
|
17
|
+
const result = mergeRight(a, b);
|
|
18
|
+
|
|
19
|
+
expect(result).toEqual({ x: 1, y: 10, z: 3 });
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test("does not mutate the original objects", () => {
|
|
23
|
+
const a = { x: 1 };
|
|
24
|
+
const b = { y: 2 };
|
|
25
|
+
|
|
26
|
+
const result = mergeRight(a, b);
|
|
27
|
+
|
|
28
|
+
expect(result).not.toBe(a);
|
|
29
|
+
expect(result).not.toBe(b);
|
|
30
|
+
expect(a).toEqual({ x: 1 });
|
|
31
|
+
expect(b).toEqual({ y: 2 });
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
test("works with empty objects", () => {
|
|
35
|
+
expect(mergeRight({}, { a: 1 })).toEqual({ a: 1 });
|
|
36
|
+
expect(mergeRight({ a: 1 }, {})).toEqual({ a: 1 });
|
|
37
|
+
expect(mergeRight({}, {})).toEqual({});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
test("works with nested objects (shallow merge only)", () => {
|
|
41
|
+
const a = { x: { nested: 1 }, y: 2 };
|
|
42
|
+
const b = { x: { nested: 10 }, z: 3 };
|
|
43
|
+
|
|
44
|
+
const result = mergeRight(a, b);
|
|
45
|
+
|
|
46
|
+
expect(result).toEqual({ x: { nested: 10 }, y: 2, z: 3 });
|
|
47
|
+
});
|
|
48
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPluginsByPathSelector,
|
|
3
|
+
createPluginsByModuleSelector,
|
|
4
|
+
combinePluginSelectors,
|
|
5
|
+
} from "../selectors";
|
|
6
|
+
|
|
7
|
+
describe("Plugin Selectors", () => {
|
|
8
|
+
const mockPlugins = [
|
|
9
|
+
{
|
|
10
|
+
identifier: "plugin1",
|
|
11
|
+
module: {
|
|
12
|
+
urlScheme: { host: "test" },
|
|
13
|
+
player: { type: "default" },
|
|
14
|
+
},
|
|
15
|
+
customField: true,
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
identifier: "plugin2",
|
|
19
|
+
module: {
|
|
20
|
+
urlScheme: { host: "other" },
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
{
|
|
24
|
+
identifier: "plugin3",
|
|
25
|
+
module: {
|
|
26
|
+
player: { type: "custom" },
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
];
|
|
30
|
+
|
|
31
|
+
const mockState = {
|
|
32
|
+
plugins: mockPlugins,
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
describe("createPluginsByPathSelector", () => {
|
|
36
|
+
it("filters plugins by string path", () => {
|
|
37
|
+
const selector = createPluginsByPathSelector("customField");
|
|
38
|
+
const result = selector(mockState);
|
|
39
|
+
|
|
40
|
+
expect(result).toHaveLength(1);
|
|
41
|
+
expect(result[0].identifier).toBe("plugin1");
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it("filters plugins by array path", () => {
|
|
45
|
+
const selector = createPluginsByPathSelector(["module", "urlScheme"]);
|
|
46
|
+
const result = selector(mockState);
|
|
47
|
+
|
|
48
|
+
expect(result).toHaveLength(2);
|
|
49
|
+
expect(result.map((p) => p.identifier)).toEqual(["plugin1", "plugin2"]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("handles missing plugins array", () => {
|
|
53
|
+
const selector = createPluginsByPathSelector("customField");
|
|
54
|
+
const result = selector({});
|
|
55
|
+
|
|
56
|
+
expect(result).toEqual([]);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("handles non-existent path", () => {
|
|
60
|
+
const selector = createPluginsByPathSelector("nonexistent");
|
|
61
|
+
const result = selector(mockState);
|
|
62
|
+
|
|
63
|
+
expect(result).toEqual([]);
|
|
64
|
+
});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
describe("createPluginsByModuleSelector", () => {
|
|
68
|
+
it("filters plugins by module path", () => {
|
|
69
|
+
const selector = createPluginsByModuleSelector("player");
|
|
70
|
+
const result = selector(mockState);
|
|
71
|
+
|
|
72
|
+
expect(result).toHaveLength(2);
|
|
73
|
+
expect(result.map((p) => p.identifier)).toEqual(["plugin1", "plugin3"]);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("handles non-existent module", () => {
|
|
77
|
+
const selector = createPluginsByModuleSelector("nonexistent");
|
|
78
|
+
const result = selector(mockState);
|
|
79
|
+
|
|
80
|
+
expect(result).toEqual([]);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe("combinePluginSelectors", () => {
|
|
85
|
+
it("combines multiple selectors with AND logic", () => {
|
|
86
|
+
const urlSchemeSelector = createPluginsByModuleSelector("urlScheme");
|
|
87
|
+
const playerSelector = createPluginsByModuleSelector("player");
|
|
88
|
+
|
|
89
|
+
const combinedSelector = combinePluginSelectors(
|
|
90
|
+
urlSchemeSelector,
|
|
91
|
+
playerSelector
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
const result = combinedSelector(mockState);
|
|
95
|
+
|
|
96
|
+
expect(result).toHaveLength(1);
|
|
97
|
+
expect(result[0].identifier).toBe("plugin1");
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it("returns empty array when no plugins match all conditions", () => {
|
|
101
|
+
const urlSchemeSelector = createPluginsByModuleSelector("urlScheme");
|
|
102
|
+
const customSelector = createPluginsByPathSelector("nonexistent");
|
|
103
|
+
|
|
104
|
+
const combinedSelector = combinePluginSelectors(
|
|
105
|
+
urlSchemeSelector,
|
|
106
|
+
customSelector
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
const result = combinedSelector(mockState);
|
|
110
|
+
|
|
111
|
+
expect(result).toEqual([]);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("handles empty state", () => {
|
|
115
|
+
const selector = combinePluginSelectors(
|
|
116
|
+
createPluginsByModuleSelector("urlScheme")
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const result = selector({});
|
|
120
|
+
|
|
121
|
+
expect(result).toEqual([]);
|
|
122
|
+
});
|
|
123
|
+
});
|
|
124
|
+
});
|
package/utils/index.ts
CHANGED
|
@@ -18,6 +18,8 @@ export { take } from "./take";
|
|
|
18
18
|
|
|
19
19
|
export { mapAccum } from "./mapAccum";
|
|
20
20
|
|
|
21
|
+
export { mergeRight } from "./mergeRight";
|
|
22
|
+
|
|
21
23
|
export { clone } from "./clone";
|
|
22
24
|
|
|
23
25
|
export {
|
|
@@ -37,9 +39,20 @@ export {
|
|
|
37
39
|
last,
|
|
38
40
|
toLower,
|
|
39
41
|
isEqual as equals,
|
|
42
|
+
uniq,
|
|
40
43
|
uniqWith,
|
|
41
44
|
flowRight as compose,
|
|
42
45
|
partial,
|
|
46
|
+
clamp,
|
|
43
47
|
reverse,
|
|
48
|
+
takeRight,
|
|
49
|
+
fromPairs,
|
|
50
|
+
sortBy,
|
|
51
|
+
merge,
|
|
52
|
+
values,
|
|
53
|
+
head,
|
|
54
|
+
findIndex,
|
|
55
|
+
set,
|
|
44
56
|
compact,
|
|
57
|
+
identity,
|
|
45
58
|
} from "lodash";
|
package/utils/path.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { pathOr } from "./pathOr";
|
|
2
2
|
|
|
3
|
-
export const path =
|
|
4
|
-
|
|
3
|
+
export const path = <T = any>(
|
|
4
|
+
route: (string | number) | (string | number)[],
|
|
5
|
+
record: any
|
|
6
|
+
) => {
|
|
7
|
+
return pathOr<T>(undefined, route, record);
|
|
5
8
|
};
|