Passing state and params to plugins.

Dispatcher now sends params to plugins; notifying about plugin load
errors; allowing local plugin paths not to end with a slash.
This commit is contained in:
Alberto Miranda
2017-04-19 09:00:11 -03:00
parent 09d00c6383
commit 687038560c
2 changed files with 15 additions and 19 deletions

View File

@@ -19,7 +19,7 @@ module.exports = class Plugins {
log('path: ', this.path) log('path: ', this.path)
this.availableExtensions = new Set([ this.availableExtensions = new Set([
'onApp', 'onWindow', 'decorateMenu', 'decorateWindow', 'onApp', 'onWindow', 'decorateMenu', 'decorateWindow',
'decorateConfig', 'setDispatcher' 'decorateConfig', 'initRenderer', 'onCheckForSubtitles'
]) ])
this.forceUpdate = false this.forceUpdate = false
@@ -75,30 +75,20 @@ module.exports = class Plugins {
}, ms('5h')) }, ms('5h'))
} }
initRenderer (dispatch) { initRenderer (params) {
this.setDispatcherOnPlugins(dispatch)
}
/**
* Pass WebTorrent renderer dispatcher to each plugin that's
* interested. Interested plugins will set a 'setDispatcher'
* method to get it.
*
* @param {object} dispatch WebTorrent dispatcher
*/
setDispatcherOnPlugins (dispatch) {
this.modules.forEach(plugin => { this.modules.forEach(plugin => {
if (plugin.setDispatcher) { if (plugin.initRenderer) {
plugin.setDispatcher(dispatch) plugin.initRenderer(params)
} }
}) })
} }
on (action) { on (action, params) {
log(`ON ${action}:`, params)
this.modules.forEach(plugin => { this.modules.forEach(plugin => {
const actionName = this.capitalizeFirstLetter(action) const actionName = this.capitalizeFirstLetter(action)
const actionHandler = plugin[`on${actionName}`] const actionHandler = plugin[`on${actionName}`]
if (actionHandler) actionHandler() if (actionHandler) actionHandler(params)
}) })
} }
@@ -332,6 +322,10 @@ module.exports = class Plugins {
const load = (path) => { const load = (path) => {
let mod let mod
if (!path.match(/\/$/)) {
path += '/'
}
try { try {
// eslint-disable-next-line import/no-dynamic-require // eslint-disable-next-line import/no-dynamic-require
mod = require(path) mod = require(path)
@@ -347,6 +341,8 @@ module.exports = class Plugins {
return mod return mod
} catch (err) { } catch (err) {
log('Require plugins ERROR:', err)
this.alert(`Error loading plugin: ${path}`)
// plugin not installed // plugin not installed
// node_modules removed? did a manual plugin uninstall? // node_modules removed? did a manual plugin uninstall?
// try installing and then loading if successfull // try installing and then loading if successfull

View File

@@ -118,7 +118,7 @@ function onState (err, _state) {
} }
plugins.init(state) plugins.init(state)
plugins.initRenderer(dispatch) plugins.initRenderer({dispatch, state})
// Add first page to location history // Add first page to location history
state.location.go({ state.location.go({
@@ -338,7 +338,7 @@ function dispatch (action, ...args) {
} }
const handler = dispatchHandlers[action] const handler = dispatchHandlers[action]
plugins.on(action) plugins.on(action, ...args)
if (handler) handler(...args) if (handler) handler(...args)
else console.error('Missing dispatch handler: ' + action) else console.error('Missing dispatch handler: ' + action)