@applicaster/zapp-react-native-utils 16.0.0-rc.106 → 16.0.0-rc.108
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/appUtils/playerManager/__tests__/playerToolbox.test.ts +97 -0
- package/appUtils/playerManager/index.ts +2 -0
- package/appUtils/playerManager/playerToolbox.ts +192 -0
- package/package.json +2 -2
- package/uiActionsRegistrator/__tests__/subscribe.test.ts +26 -0
- package/uiActionsRegistrator/registry.ts +16 -0
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
const mockRegister = jest.fn();
|
|
2
|
+
let mockXray: Record<string, unknown> | null = {};
|
|
3
|
+
|
|
4
|
+
jest.mock("../../../logger", () => ({
|
|
5
|
+
getXray: () => mockXray,
|
|
6
|
+
}));
|
|
7
|
+
|
|
8
|
+
const loadToolbox = () => {
|
|
9
|
+
let module;
|
|
10
|
+
|
|
11
|
+
jest.isolateModules(() => {
|
|
12
|
+
module = require("../playerToolbox");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
return module as typeof import("../playerToolbox");
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const player = (overrides = {}) => ({
|
|
19
|
+
getSeekableDuration: () => 100,
|
|
20
|
+
getContentPosition: () => 40,
|
|
21
|
+
seekTo: jest.fn(),
|
|
22
|
+
...overrides,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const manager = (active) =>
|
|
26
|
+
({ getActivePlayer: () => active, isCasting: () => false }) as any;
|
|
27
|
+
|
|
28
|
+
const registeredTools = async (active) => {
|
|
29
|
+
mockRegister.mockResolvedValue(undefined);
|
|
30
|
+
mockXray = { registerReactToolbox: mockRegister };
|
|
31
|
+
await loadToolbox().registerPlayerToolbox(manager(active));
|
|
32
|
+
|
|
33
|
+
const { tools } = mockRegister.mock.calls[0][0];
|
|
34
|
+
|
|
35
|
+
return Object.fromEntries(tools.map((tool) => [tool.name, tool.handler]));
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
describe("playerToolbox", () => {
|
|
39
|
+
beforeEach(() => {
|
|
40
|
+
mockRegister.mockReset();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("does nothing when X-Ray has no React toolbox API", async () => {
|
|
44
|
+
mockXray = {};
|
|
45
|
+
|
|
46
|
+
await expect(
|
|
47
|
+
loadToolbox().registerPlayerToolbox(manager(null))
|
|
48
|
+
).resolves.toBeUndefined();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("does nothing when getXray returns null", async () => {
|
|
52
|
+
mockXray = null;
|
|
53
|
+
|
|
54
|
+
await expect(
|
|
55
|
+
loadToolbox().registerPlayerToolbox(manager(null))
|
|
56
|
+
).resolves.toBeUndefined();
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("swallows a failed registration", async () => {
|
|
60
|
+
mockRegister.mockRejectedValue(new Error("no native bridge"));
|
|
61
|
+
mockXray = { registerReactToolbox: mockRegister };
|
|
62
|
+
|
|
63
|
+
await expect(
|
|
64
|
+
loadToolbox().registerPlayerToolbox(manager(null))
|
|
65
|
+
).resolves.toBeUndefined();
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("seeks to an absolute position, from the end when negative, clamped", async () => {
|
|
69
|
+
const active = player();
|
|
70
|
+
const tools = await registeredTools(active);
|
|
71
|
+
|
|
72
|
+
expect(tools.seek({ position: 30 })).toEqual({ position: 30 });
|
|
73
|
+
expect(tools.seek({ position: -10 })).toEqual({ position: 90 });
|
|
74
|
+
expect(tools.seek({ position: 500 })).toEqual({ position: 100 });
|
|
75
|
+
expect(tools.seek({ position: -500 })).toEqual({ position: 0 });
|
|
76
|
+
expect(active.seekTo.mock.calls).toEqual([[30], [90], [100], [0]]);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it("offsets from the current position and refuses zero", async () => {
|
|
80
|
+
const tools = await registeredTools(player());
|
|
81
|
+
|
|
82
|
+
expect(tools.offset({ seconds: 15 })).toEqual({ from: 40, position: 55 });
|
|
83
|
+
expect(tools.offset({ seconds: -60 })).toEqual({ from: 40, position: 0 });
|
|
84
|
+
|
|
85
|
+
expect(() => tools.offset({ seconds: 0 })).toThrow(
|
|
86
|
+
"Ignored zero relative seek"
|
|
87
|
+
);
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it("refuses to seek a stream that is not seekable", async () => {
|
|
91
|
+
const tools = await registeredTools(
|
|
92
|
+
player({ getSeekableDuration: () => 0 })
|
|
93
|
+
);
|
|
94
|
+
|
|
95
|
+
expect(() => tools.seek({ position: 10 })).toThrow("Seek is not supported");
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
import { setUserLanguagePreference } from "./userLanguagePreference";
|
|
12
12
|
import { PlayerRole } from "./const";
|
|
13
13
|
import { RNPlayerManager } from "./nativePlayerManager";
|
|
14
|
+
import { registerPlayerToolbox } from "./playerToolbox";
|
|
14
15
|
|
|
15
16
|
const { log_error, log_debug, log_warning } = createLogger({
|
|
16
17
|
category: "General",
|
|
@@ -54,6 +55,7 @@ export class PlayerManager {
|
|
|
54
55
|
public static getInstance() {
|
|
55
56
|
if (!PlayerManager.instance) {
|
|
56
57
|
PlayerManager.instance = new PlayerManager();
|
|
58
|
+
registerPlayerToolbox(PlayerManager.instance);
|
|
57
59
|
}
|
|
58
60
|
|
|
59
61
|
return PlayerManager.instance;
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import { getXray } from "../../logger";
|
|
2
|
+
import type { PlayerManager } from "./index";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* X-Ray `player.*` toolbox: lets an agent read and drive the active player without screenshots.
|
|
6
|
+
* Registered by the PlayerManager singleton itself, so it exists wherever players do. Skipped
|
|
7
|
+
* when the installed X-Ray plugin has no React toolbox API.
|
|
8
|
+
*
|
|
9
|
+
* How the player is shown (video modal mode, mini player) is navigation state: see `nav.*`.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
type ToolSpec = {
|
|
13
|
+
name: string;
|
|
14
|
+
description: string;
|
|
15
|
+
parameters?: Array<{
|
|
16
|
+
name: string;
|
|
17
|
+
type: "number";
|
|
18
|
+
description: string;
|
|
19
|
+
}>;
|
|
20
|
+
handler: (args: Record<string, unknown>) => unknown;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
type RegisterReactToolbox = (toolbox: {
|
|
24
|
+
name: string;
|
|
25
|
+
description: string;
|
|
26
|
+
tools: ToolSpec[];
|
|
27
|
+
}) => Promise<void>;
|
|
28
|
+
|
|
29
|
+
let registration: Promise<void> | undefined;
|
|
30
|
+
|
|
31
|
+
const toNumber = (value: unknown) =>
|
|
32
|
+
typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
33
|
+
|
|
34
|
+
const clamp = (value: number, max: number) => Math.min(Math.max(value, 0), max);
|
|
35
|
+
|
|
36
|
+
/** The range a seek can reach: the DVR window on live, the content on VOD. */
|
|
37
|
+
const requireSeekable = (player: {
|
|
38
|
+
getSeekableDuration: () => number | undefined;
|
|
39
|
+
}) => {
|
|
40
|
+
const seekable = toNumber(player.getSeekableDuration());
|
|
41
|
+
|
|
42
|
+
if (!seekable) throw new Error("Seek is not supported");
|
|
43
|
+
|
|
44
|
+
return seekable;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const tools = (manager: PlayerManager): ToolSpec[] => {
|
|
48
|
+
const requirePlayer = () => {
|
|
49
|
+
const player = manager.getActivePlayer();
|
|
50
|
+
|
|
51
|
+
if (!player) throw new Error("No active player");
|
|
52
|
+
|
|
53
|
+
return player;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return [
|
|
57
|
+
{
|
|
58
|
+
name: "state",
|
|
59
|
+
description:
|
|
60
|
+
"The active player: entry, position and duration in seconds, playing/buffering/ad/live/muted, " +
|
|
61
|
+
"role (Primary, Cell, Chromecast) and casting. active: false when nothing plays.",
|
|
62
|
+
handler: () => {
|
|
63
|
+
const player = manager.getActivePlayer();
|
|
64
|
+
|
|
65
|
+
if (!player) return { active: false, casting: manager.isCasting() };
|
|
66
|
+
|
|
67
|
+
const entry = player.getEntry();
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
active: true,
|
|
71
|
+
playerId: player.playerId,
|
|
72
|
+
entry: entry ? { id: entry.id, title: entry.title } : null,
|
|
73
|
+
position: toNumber(player.getPosition()),
|
|
74
|
+
duration: toNumber(player.getDuration()),
|
|
75
|
+
// isPlaying() only means "not paused", which is also true before the stream starts.
|
|
76
|
+
playing: !!player.isReadyToPlay() && player.isPlaying(),
|
|
77
|
+
buffering: !!player.isBuffering(),
|
|
78
|
+
readyToPlay: !!player.isReadyToPlay(),
|
|
79
|
+
ad: player.isAd(),
|
|
80
|
+
live: !!player.isLive(),
|
|
81
|
+
muted: !!player.getIsMuted(),
|
|
82
|
+
role: player.playerRole,
|
|
83
|
+
casting: manager.isCasting(),
|
|
84
|
+
};
|
|
85
|
+
},
|
|
86
|
+
},
|
|
87
|
+
{
|
|
88
|
+
name: "play",
|
|
89
|
+
description: "Resume the active player.",
|
|
90
|
+
handler: () => {
|
|
91
|
+
requirePlayer().play();
|
|
92
|
+
|
|
93
|
+
return "OK";
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: "pause",
|
|
98
|
+
description: "Pause the active player.",
|
|
99
|
+
handler: () => {
|
|
100
|
+
requirePlayer().pause();
|
|
101
|
+
|
|
102
|
+
return "OK";
|
|
103
|
+
},
|
|
104
|
+
},
|
|
105
|
+
{
|
|
106
|
+
name: "seek",
|
|
107
|
+
description:
|
|
108
|
+
"Seek to an absolute position, as the player's console command does: seconds from the start, " +
|
|
109
|
+
"or from the end when negative. Clamped to the seekable range. Where it lands can differ: " +
|
|
110
|
+
"server-side ads snap back to the break before the target; read player.state afterwards.",
|
|
111
|
+
parameters: [
|
|
112
|
+
{
|
|
113
|
+
name: "position",
|
|
114
|
+
type: "number",
|
|
115
|
+
description:
|
|
116
|
+
"Seconds from the start; negative values count back from the end.",
|
|
117
|
+
},
|
|
118
|
+
],
|
|
119
|
+
handler: ({ position }) => {
|
|
120
|
+
const player = requirePlayer();
|
|
121
|
+
const seconds = toNumber(position);
|
|
122
|
+
|
|
123
|
+
if (seconds === null) {
|
|
124
|
+
throw new Error("'position' must be a number of seconds");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
const seekable = requireSeekable(player);
|
|
128
|
+
|
|
129
|
+
const target =
|
|
130
|
+
seconds >= 0
|
|
131
|
+
? Math.min(seconds, seekable)
|
|
132
|
+
: clamp(seekable + seconds, seekable);
|
|
133
|
+
|
|
134
|
+
player.seekTo(target);
|
|
135
|
+
|
|
136
|
+
return { position: target };
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: "offset",
|
|
141
|
+
description:
|
|
142
|
+
"Seek relative to the current position, as the player's console command does: positive " +
|
|
143
|
+
"forward, negative back. Clamped to the seekable range.",
|
|
144
|
+
parameters: [
|
|
145
|
+
{
|
|
146
|
+
name: "seconds",
|
|
147
|
+
type: "number",
|
|
148
|
+
description: "Seconds to move: positive forward, negative back.",
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
handler: ({ seconds }) => {
|
|
152
|
+
const player = requirePlayer();
|
|
153
|
+
const delta = toNumber(seconds);
|
|
154
|
+
|
|
155
|
+
if (delta === null) throw new Error("'seconds' must be a number");
|
|
156
|
+
|
|
157
|
+
if (delta === 0) throw new Error("Ignored zero relative seek");
|
|
158
|
+
|
|
159
|
+
const seekable = requireSeekable(player);
|
|
160
|
+
const from = toNumber(player.getContentPosition()) ?? 0;
|
|
161
|
+
const target = clamp(from + delta, seekable);
|
|
162
|
+
|
|
163
|
+
player.seekTo(target);
|
|
164
|
+
|
|
165
|
+
return { from, position: target };
|
|
166
|
+
},
|
|
167
|
+
},
|
|
168
|
+
];
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
export const registerPlayerToolbox = (manager: PlayerManager) => {
|
|
172
|
+
if (registration) return registration;
|
|
173
|
+
|
|
174
|
+
const register = (
|
|
175
|
+
getXray() as
|
|
176
|
+
| { registerReactToolbox?: RegisterReactToolbox }
|
|
177
|
+
| null
|
|
178
|
+
| undefined
|
|
179
|
+
)?.registerReactToolbox;
|
|
180
|
+
|
|
181
|
+
if (!register) return Promise.resolve();
|
|
182
|
+
|
|
183
|
+
registration = register({
|
|
184
|
+
name: "player",
|
|
185
|
+
description: "Inspect and control the active player.",
|
|
186
|
+
tools: tools(manager),
|
|
187
|
+
}).catch(() => {
|
|
188
|
+
registration = undefined;
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
return registration;
|
|
192
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@applicaster/zapp-react-native-utils",
|
|
3
|
-
"version": "16.0.0-rc.
|
|
3
|
+
"version": "16.0.0-rc.108",
|
|
4
4
|
"description": "Applicaster Zapp React Native utilities package",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"homepage": "https://github.com/applicaster/quickbrick#readme",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@applicaster/applicaster-types": "16.0.0-rc.
|
|
30
|
+
"@applicaster/applicaster-types": "16.0.0-rc.108",
|
|
31
31
|
"buffer": "^5.2.1",
|
|
32
32
|
"camelize": "^1.0.0",
|
|
33
33
|
"dayjs": "^1.11.10",
|
|
@@ -105,3 +105,29 @@ describe("isActionAvailableFor", () => {
|
|
|
105
105
|
expect(isActionAvailableFor(undefined, entry)).toBe(true);
|
|
106
106
|
});
|
|
107
107
|
});
|
|
108
|
+
|
|
109
|
+
describe("uiActionsRegistry.notifyProviderChanged", () => {
|
|
110
|
+
it("notifies subscribers for a registered provider", () => {
|
|
111
|
+
const unregister = uiActionsRegistry.registerAction("live-action", value);
|
|
112
|
+
const listener = jest.fn();
|
|
113
|
+
const unsubscribe = uiActionsRegistry.subscribe(listener);
|
|
114
|
+
|
|
115
|
+
uiActionsRegistry.notifyProviderChanged("live-action");
|
|
116
|
+
|
|
117
|
+
expect(listener).toHaveBeenCalledTimes(1);
|
|
118
|
+
|
|
119
|
+
unsubscribe();
|
|
120
|
+
unregister();
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("ignores names that are not registered", () => {
|
|
124
|
+
const listener = jest.fn();
|
|
125
|
+
const unsubscribe = uiActionsRegistry.subscribe(listener);
|
|
126
|
+
|
|
127
|
+
uiActionsRegistry.notifyProviderChanged("unknown-action");
|
|
128
|
+
|
|
129
|
+
expect(listener).not.toHaveBeenCalled();
|
|
130
|
+
|
|
131
|
+
unsubscribe();
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -246,6 +246,22 @@ class UIActionsRegistry {
|
|
|
246
246
|
this.changeListeners.slice().forEach((listener) => listener());
|
|
247
247
|
}
|
|
248
248
|
|
|
249
|
+
/**
|
|
250
|
+
* Tells consumers that a registered provider now returns different actions.
|
|
251
|
+
*
|
|
252
|
+
* Resolved actions are snapshots: an action whose availability depends on
|
|
253
|
+
* live state (cast devices found, downloads allowed) hands out a new value
|
|
254
|
+
* when that state changes, and consumers that resolved the old one keep
|
|
255
|
+
* asking it until they re-resolve. Registration alone never tells them.
|
|
256
|
+
*/
|
|
257
|
+
notifyProviderChanged(name: string): void {
|
|
258
|
+
if (!this.registeredActions[name]) {
|
|
259
|
+
return;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
this.notifyChanged();
|
|
263
|
+
}
|
|
264
|
+
|
|
249
265
|
/**
|
|
250
266
|
* Register a dynamic action provider under a unique `name`.
|
|
251
267
|
* The provider can return one or more actions and may use the passed `entry`.
|