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:
@@ -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
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user