Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/bigscreenplayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,8 @@ function BigscreenPlayer() {
callbacks.onError(reason)
}
})

Plugins.updateContext((context) => ({ ...context, mediaSources }))
},

/**
Expand Down Expand Up @@ -737,7 +739,7 @@ function BigscreenPlayer() {
},

/**
* Register a plugin for extended events.
* Register a plugin for extended events & custom functionality.
* @function
* @param {*} plugin
*/
Expand Down
3 changes: 3 additions & 0 deletions src/playercomponent.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,9 @@ function PlayerComponent(
})
}

// Not an ideal place for this, but I've been warned of a possible playercomponent rewrite
Plugins.updateContext((context) => ({ ...context, attemptCdnFailover }))

function clearFatalErrorTimeout() {
if (fatalErrorTimeout !== null) {
clearTimeout(fatalErrorTimeout)
Expand Down
30 changes: 30 additions & 0 deletions src/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import PlaybackUtils from "./utils/playbackutils"
import CallCallbacks from "./utils/callcallbacks"

let plugins = []
const pluginContext = {}

function callOnAllPlugins(funcKey, evt) {
const clonedEvent = PlaybackUtils.deepClone(evt)
Expand All @@ -13,8 +14,37 @@ function callOnAllPlugins(funcKey, evt) {
}

export default {
/**
* @param {function (*): *} updater - a function which accepts the current context, and returns a new context
*/
updateContext: (updater) => {
const newContext = updater(PlaybackUtils.deepClone(pluginContext))

if (typeof newContext !== "object") {
throw new TypeError("context must be an object")
}

// update object (preserving reference)
for (const prop of Object.keys(pluginContext)) {
delete pluginContext[prop]
}

Object.assign(pluginContext, newContext)

// call context update handlers
for (const plugin of plugins) {
plugin.onContextUpdated?.(pluginContext)
}
},

/**
* @param {*} plugin - an object
*/
registerPlugin: (plugin) => {
plugins.push(plugin)

// provide initial context
plugin.onContextUpdated?.(pluginContext)
},

unregisterPlugin: (plugin) => {
Expand Down
1 change: 1 addition & 0 deletions src/subtitles/imscsubtitles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Plugins from "../plugins"
jest.mock("smp-imsc")
jest.mock("../utils/loadurl")
jest.mock("../plugins", () => ({
updateContext: jest.fn(),
interface: {
onSubtitlesTimeout: jest.fn(),
onSubtitlesXMLError: jest.fn(),
Expand Down
1 change: 1 addition & 0 deletions src/subtitles/legacysubtitles.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Renderer from "./renderer"

jest.mock("../utils/loadurl")
jest.mock("../plugins", () => ({
updateContext: jest.fn(),
interface: {
onSubtitlesTimeout: jest.fn(),
onSubtitlesXMLError: jest.fn(),
Expand Down
10 changes: 10 additions & 0 deletions src/subtitles/subtitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ function Subtitles(
subtitlesContainer?.tearDown()
}

function attemptSubtitleCdnFailover(opts) {
hide()
mediaSources
.failoverSubtitles(opts)
.then(() => show())
.catch(() => DebugTool.info("No more CDNs available for subtitle failover"))
}

Plugins.updateContext((context) => ({ ...context, attemptSubtitleCdnFailover }))

return {
enable,
disable,
Expand Down
Loading