Skip to content

Commit dc457f0

Browse files
authored
BADGER-130: Test CDN failover (extended plugin functionality) (#413)
* feat: implemented functional plugins w/ context * fix: accounted for optional use of onPluginContextUpdated callback * chore: moved subtitle cdn failover logic to subtitles.js * chore: added onContextUpdated plugin event / removed functional plugins * fix: comments * fix: comments
1 parent 914792e commit dc457f0

6 files changed

Lines changed: 48 additions & 1 deletion

File tree

src/bigscreenplayer.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ function BigscreenPlayer() {
307307
callbacks.onError(reason)
308308
}
309309
})
310+
311+
Plugins.updateContext((context) => ({ ...context, mediaSources }))
310312
},
311313

312314
/**
@@ -737,7 +739,7 @@ function BigscreenPlayer() {
737739
},
738740

739741
/**
740-
* Register a plugin for extended events.
742+
* Register a plugin for extended events & custom functionality.
741743
* @function
742744
* @param {*} plugin
743745
*/

src/playercomponent.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,9 @@ function PlayerComponent(
335335
})
336336
}
337337

338+
// Not an ideal place for this, but I've been warned of a possible playercomponent rewrite
339+
Plugins.updateContext((context) => ({ ...context, attemptCdnFailover }))
340+
338341
function clearFatalErrorTimeout() {
339342
if (fatalErrorTimeout !== null) {
340343
clearTimeout(fatalErrorTimeout)

src/plugins.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import PlaybackUtils from "./utils/playbackutils"
22
import CallCallbacks from "./utils/callcallbacks"
33

44
let plugins = []
5+
const pluginContext = {}
56

67
function callOnAllPlugins(funcKey, evt) {
78
const clonedEvent = PlaybackUtils.deepClone(evt)
@@ -13,8 +14,37 @@ function callOnAllPlugins(funcKey, evt) {
1314
}
1415

1516
export default {
17+
/**
18+
* @param {function (*): *} updater - a function which accepts the current context, and returns a new context
19+
*/
20+
updateContext: (updater) => {
21+
const newContext = updater(PlaybackUtils.deepClone(pluginContext))
22+
23+
if (typeof newContext !== "object") {
24+
throw new TypeError("context must be an object")
25+
}
26+
27+
// update object (preserving reference)
28+
for (const prop of Object.keys(pluginContext)) {
29+
delete pluginContext[prop]
30+
}
31+
32+
Object.assign(pluginContext, newContext)
33+
34+
// call context update handlers
35+
for (const plugin of plugins) {
36+
plugin.onContextUpdated?.(pluginContext)
37+
}
38+
},
39+
40+
/**
41+
* @param {*} plugin - an object
42+
*/
1643
registerPlugin: (plugin) => {
1744
plugins.push(plugin)
45+
46+
// provide initial context
47+
plugin.onContextUpdated?.(pluginContext)
1848
},
1949

2050
unregisterPlugin: (plugin) => {

src/subtitles/imscsubtitles.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Plugins from "../plugins"
77
jest.mock("smp-imsc")
88
jest.mock("../utils/loadurl")
99
jest.mock("../plugins", () => ({
10+
updateContext: jest.fn(),
1011
interface: {
1112
onSubtitlesTimeout: jest.fn(),
1213
onSubtitlesXMLError: jest.fn(),

src/subtitles/legacysubtitles.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Renderer from "./renderer"
66

77
jest.mock("../utils/loadurl")
88
jest.mock("../plugins", () => ({
9+
updateContext: jest.fn(),
910
interface: {
1011
onSubtitlesTimeout: jest.fn(),
1112
onSubtitlesXMLError: jest.fn(),

src/subtitles/subtitles.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ function Subtitles(
133133
subtitlesContainer?.tearDown()
134134
}
135135

136+
function attemptSubtitleCdnFailover(opts) {
137+
hide()
138+
mediaSources
139+
.failoverSubtitles(opts)
140+
.then(() => show())
141+
.catch(() => DebugTool.info("No more CDNs available for subtitle failover"))
142+
}
143+
144+
Plugins.updateContext((context) => ({ ...context, attemptSubtitleCdnFailover }))
145+
136146
return {
137147
enable,
138148
disable,

0 commit comments

Comments
 (0)