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
38 changes: 38 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,45 @@ 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 callbacks
for (const plugin of plugins) {
plugin.__onPluginContextUpdated?.(pluginContext)
}
},

/**
* @param {*} plugin - an object or function, functional plugins receive the context as an argument
*/
registerPlugin: (plugin) => {
plugins.push(plugin)

if (typeof plugin === "function") {
plugin(pluginContext, (onPluginContextUpdated) => {
plugin.__onPluginContextUpdated = (pluginContext) => {
onPluginContextUpdated(pluginContext)
}
})

// provide initial update
plugin.__onPluginContextUpdated?.(pluginContext)
}
},

unregisterPlugin: (plugin) => {
Expand Down
2 changes: 2 additions & 0 deletions src/subtitles/imscsubtitles.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ function IMSCSubtitles(
}
}

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

function pruneSegments() {
// Before sorting, check if we've gone back in time, so we know whether to prune from front or back of array
const seekedBack = segments[SEGMENTS_BUFFER_SIZE].number < segments[SEGMENTS_BUFFER_SIZE - 1].number
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
Loading