Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added better handling for automatic dependency resolution between plugins #1412

Merged
merged 4 commits into from
Nov 1, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Added redundant plugin load validation.
Signed-off-by: TheDudeFromCI <thedudefromci@gmail.com>
  • Loading branch information
TheDudeFromCI committed Oct 29, 2020
commit bf136012db39e912a87343c785bcde084a424361
7 changes: 6 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1369,7 +1369,8 @@ See the `bot.settings` property.

#### bot.loadPlugin(plugin)

Injects a Plugin.
Injects a Plugin. Does nothing if the plugin is already loaded.

* `plugin` - function

```js
Expand All @@ -1392,6 +1393,10 @@ bot.once('login', function() {
Injects plugins see `bot.loadPlugin`.
* `plugins` - array of functions

#### bot.hasPlugin(plugin)

Checks if the given plugin is loaded (or scheduled to be loaded) on this bot.

#### bot.sleep(bedBlock, [cb])

Sleep in a bed. `bedBlock` should be a `Block` instance which is a bed. `cb` can have an err parameter if the bot cannot sleep.
Expand Down
2 changes: 2 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ export class Bot extends (EventEmitter as new () => TypedEmitter<BotEvents>) {

loadPlugins(plugins: Array<Plugin>): void;

hasPlugin(plugin: Plugin): boolean;

sleep(bedBlock: Block, cb?: (err?: Error) => void): void;

isABed(bedBlock: Block): void;
Expand Down
36 changes: 28 additions & 8 deletions lib/plugin_loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,49 @@ module.exports = inject

function inject (bot, options) {
let loaded = false
let pluginsToBeAdded = []
let pluginList = []
bot.once('inject_allowed', onInjectAllowed)

function onInjectAllowed () {
loaded = true
injectPlugins(pluginsToBeAdded)
injectPlugins()
}

function loadPlugin (plugin) {
assert.ok(typeof plugin === 'function', 'plugin needs to be a function')
if (!loaded) pluginsToBeAdded.push(plugin)
else plugin(bot, options)

if (hasPlugin(plugin)) {
return
}

pluginList.push(plugin)

if (loaded) {
plugin(bot, options)
}
}

function loadPlugins (plugins) {
// While type checking if already done in the other function, it's useful to do
// it here to prevent situations where only half the plugin list is loaded.
assert.ok(plugins.filter(plugin => typeof plugin === 'function').length === plugins.length, 'plugins need to be an array of functions')
if (loaded) return injectPlugins(plugins)
pluginsToBeAdded = pluginsToBeAdded.concat(plugins)
}
function injectPlugins (plugins) {

plugins.forEach((plugin) => {
loadPlugin(plugin)
})
}

function injectPlugins () {
pluginList.forEach((plugin) => {
plugin(bot, options)
})
}

function hasPlugin (plugin) {
return pluginList.indexOf(plugin) >= 0
}

bot.loadPlugin = loadPlugin
bot.loadPlugins = loadPlugins
bot.hasPlugin = hasPlugin
}