Passing dispatcher to plugins.

This will enable plugins to dispatch events and interact with the
renderer process; removed unused extension keys.
This commit is contained in:
Alberto Miranda
2017-04-16 16:31:39 -03:00
parent 957c5b64e9
commit 09d00c6383
2 changed files with 40 additions and 9 deletions

View File

@@ -18,15 +18,8 @@ module.exports = class Plugins {
this.path = resolve(config.getConfigPath(), 'plugins')
log('path: ', this.path)
this.availableExtensions = new Set([
'onApp', 'onWindow', 'onRendererWindow', 'onUnload', 'middleware',
'reduceUI', 'reduceSessions', 'reduceTermGroups',
'decorateMenu', 'decorateTerm', 'decorateWindow',
'decorateTab', 'decorateNotification', 'decorateNotifications',
'decorateTabs', 'decorateConfig', 'decorateEnv',
'decorateTermGroup', 'getTermProps',
'getTabProps', 'getTabsProps', 'getTermGroupProps',
'mapTermsState', 'mapHeaderState', 'mapNotificationsState',
'mapTermsDispatch', 'mapHeaderDispatch', 'mapNotificationsDispatch'
'onApp', 'onWindow', 'decorateMenu', 'decorateWindow',
'decorateConfig', 'setDispatcher'
])
this.forceUpdate = false
@@ -82,6 +75,37 @@ module.exports = class Plugins {
}, ms('5h'))
}
initRenderer (dispatch) {
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 => {
if (plugin.setDispatcher) {
plugin.setDispatcher(dispatch)
}
})
}
on (action) {
this.modules.forEach(plugin => {
const actionName = this.capitalizeFirstLetter(action)
const actionHandler = plugin[`on${actionName}`]
if (actionHandler) actionHandler()
})
}
capitalizeFirstLetter (string) {
return string.charAt(0).toUpperCase() + string.slice(1)
}
didPluginsChange () {
return this.state.saved.installedPlugins !== this.id
}

View File

@@ -36,6 +36,9 @@ const telemetry = require('./lib/telemetry')
const sound = require('./lib/sound')
const TorrentPlayer = require('./lib/torrent-player')
const Plugins = require('../plugins')
const plugins = new Plugins()
// Perf optimization: Needed immediately, so do not lazy load it below
const TorrentListController = require('./controllers/torrent-list-controller')
@@ -114,6 +117,9 @@ function onState (err, _state) {
})
}
plugins.init(state)
plugins.initRenderer(dispatch)
// Add first page to location history
state.location.go({
url: 'home',
@@ -332,6 +338,7 @@ function dispatch (action, ...args) {
}
const handler = dispatchHandlers[action]
plugins.on(action)
if (handler) handler(...args)
else console.error('Missing dispatch handler: ' + action)