@@ -2,6 +2,7 @@ import PlaybackUtils from "./utils/playbackutils"
22import CallCallbacks from "./utils/callcallbacks"
33
44let plugins = [ ]
5+ const pluginContext = { }
56
67function callOnAllPlugins ( funcKey , evt ) {
78 const clonedEvent = PlaybackUtils . deepClone ( evt )
@@ -13,8 +14,37 @@ function callOnAllPlugins(funcKey, evt) {
1314}
1415
1516export 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 ) => {
0 commit comments