From 09d00c6383ebeba5ea9cf772646e7d0126eb325b Mon Sep 17 00:00:00 2001 From: Alberto Miranda Date: Sun, 16 Apr 2017 16:31:39 -0300 Subject: [PATCH] Passing dispatcher to plugins. This will enable plugins to dispatch events and interact with the renderer process; removed unused extension keys. --- src/plugins.js | 42 +++++++++++++++++++++++++++++++++--------- src/renderer/main.js | 7 +++++++ 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/src/plugins.js b/src/plugins.js index 8ce4f4a9..f337ff01 100644 --- a/src/plugins.js +++ b/src/plugins.js @@ -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 } diff --git a/src/renderer/main.js b/src/renderer/main.js index 7acb24f5..9189d6a2 100644 --- a/src/renderer/main.js +++ b/src/renderer/main.js @@ -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)