PluginManager
Index
Constructors
Methods
- add
- addAll
- addAllAsync
- addAsync
- createEventProxy
- destroy
- destroyAsync
- getAllPluginData
- getEventbus
- getExtraEventData
- getMethodNames
- getOptions
- getPluginData
- getPluginEnabled
- getPluginEventNames
- getPluginMethodNames
- getPluginNames
- getPluginOptions
- getPluginsByEventName
- getPluginsEnabled
- getPluginsEventNames
- hasMethod
- hasPlugin
- hasPluginMethod
- invoke
- invokeAsync
- invokeAsyncEvent
- invokeSync
- invokeSyncEvent
- isValidConfig
- remove
- removeAll
- removeAllAsync
- removeAsync
- setEventbus
- setEventbusAsync
- setExtraEventData
- setOptions
- setPluginEnabled
- setPluginsEnabled
Constructors
constructor
Instantiates PluginManager
Parameters
options: { eventPrepend?: string; eventbus?: EventEmitter; throwNoMethod: boolean; throwNoPlugin: boolean } = ...
Provides various configuration options:
options.eventbus - An instance of ‘backbone-esnext-events’ used as the plugin eventbus.
options.eventPrepend - A customized name to prepend PluginManager events on the eventbus.
options.throwNoMethod - If true then when a method fails to be invoked by any plugin an exception will be thrown.
options.throwNoPlugin- If true then when no plugin is matched to be invoked an exception will be thrown.
optionalextraEventData: string
Provides additional optional data to attach to PluginEvent callbacks.
Returns default
Methods
add
Adds a plugin by the given configuration parameters. A plugin
name
is always required. If no other options are provided then thename
doubles as the NPM module / local file to load. The loading first checks for an existinginstance
to use as the plugin. Then thetarget
is chosen as the NPM module / local file to load. By passing inoptions
this will be stored and accessible to the plugin during all callbacks.Parameters
pluginConfig: PluginConfig
Defines the plugin to load.
optionalmoduleData: any
Optional object hash to associate with plugin.
Returns PluginData
addAll
Initializes multiple plugins in a single call.
Parameters
pluginConfigs: PluginConfig[] = []
An array of plugin config object hash entries.
optionalmoduleData: any
Optional object hash to associate with all plugins.
Returns PluginData[]
addAllAsync
Initializes multiple plugins in a single call.
Parameters
pluginConfigs: PluginConfig[] = []
An array of plugin config object hash entries.
optionalmoduleData: any
Optional object hash to associate with all plugins.
Returns Promise<PluginData[]>
addAsync
Adds a plugin by the given configuration parameters. A plugin
name
is always required. If no other options are provided then thename
doubles as the NPM module / local file to load. The loading first checks for an existinginstance
to use as the plugin. Then thetarget
is chosen as the NPM module / local file to load. By passing inoptions
this will be stored and accessible to the plugin during all callbacks.Parameters
pluginConfig: PluginConfig
Defines the plugin to load.
optionalmoduleData: any
Optional object hash to associate with plugin.
Returns Promise<PluginData>
createEventProxy
If an eventbus is assigned to this plugin manager then a new EventEmitter wrapping this eventbus is returned.
Returns EventEmitter
destroy
Destroys all managed plugins after unloading them.
Returns void
destroyAsync
Destroys all managed plugins after unloading them.
Returns Promise<void>
getAllPluginData
Returns all plugin data or if a boolean is passed in will return plugin data by current enabled state.
Parameters
enabled: boolean
If enabled is a boolean it will return plugins given their enabled state.
Returns PluginData[]
getEventbus
Returns any associated eventbus.
Returns EventEmitter
getExtraEventData
Returns any extra event data associated with PluginEvents.
Returns any
getMethodNames
Returns all method names or if a boolean is passed in will return method names for plugins by current enabled state.
Parameters
enabled: boolean
If enabled is a boolean it will return plugin methods names given their enabled state.
pluginName: string
If a string then just this plugins methods names are returned.
Returns string[]
getOptions
Returns a copy of the plugin manager options.
Returns PluginManagerOptions
getPluginData
Gets the plugin data for a plugin by name.
Parameters
pluginName: string
A plugin name.
Returns PluginData
getPluginEnabled
Returns the enabled state of a plugin.
Parameters
pluginName: string
Plugin name to set state.
Returns boolean
getPluginEventNames
Returns the event binding names registered on any associated plugin EventProxy.
Parameters
pluginName: string
Plugin name to set state.
Returns string[]
getPluginMethodNames
Returns all plugin names or if a boolean is passed in will return plugin names by current enabled state.
Parameters
enabled: boolean
If enabled is a boolean it will return plugins given their enabled state.
Returns { method: string; plugin: string }[]
getPluginNames
Returns all plugin names or if a boolean is passed in will return plugin names by current enabled state.
Parameters
enabled: boolean
If enabled is a boolean it will return plugins given their enabled state.
Returns string[]
getPluginOptions
Returns a copy of the given plugin options.
Parameters
pluginName: string
Plugin name to retrieve.
Returns any
getPluginsByEventName
Returns the plugin names that registered the given event binding name.
Parameters
eventName: string
An event name that plugins may have registered.
Returns string[]
getPluginsEnabled
Returns the enabled state of a list of plugins.
Parameters
pluginNames: string[]
An array / iterable of plugin names.
Returns { enabled: boolean; pluginName: string }[]
getPluginsEventNames
Returns the event binding names registered from each plugin.
Parameters
optionalnameOrList: string | string[]
An array / iterable of plugin names.
Returns { events: string[]; pluginName: string }[]
hasMethod
Returns true if there is at least one plugin loaded with the given method name.
Parameters
methodName: string
Method name to test.
Returns boolean
hasPlugin
Returns true if there is a plugin loaded with the given plugin name.
Parameters
pluginName: string
Plugin name to test.
Returns boolean
hasPluginMethod
Returns true if there is a plugin loaded with the given plugin name that also has a method with the given method name.
Parameters
pluginName: string
Plugin name to test.
methodName: string
Method name to test.
Returns boolean
invoke
This dispatch method simply invokes any plugin targets for the given methodName..
Parameters
methodName: string
Method name to invoke.
optionalargs: any
Optional arguments. An array will be spread as multiple arguments.
optionalnameOrList: string | string[]
An optional plugin name or array / iterable of plugin names to invoke.
Returns void
invokeAsync
This dispatch method uses ES6 Promises and adds any returned results to an array which is added to a Promise.all construction which passes back a Promise which waits until all Promises complete. Any target invoked may return a Promise or any result. This is very useful to use for any asynchronous operations.
Parameters
methodName: string
Method name to invoke.
optionalargs: any[]
Optional arguments. An array will be spread as multiple arguments.
optionalnameOrList: string | string[]
An optional plugin name or array / iterable of plugin names to invoke.
Returns Promise<any>
invokeAsyncEvent
This dispatch method synchronously passes to and returns from any invoked targets a PluginEvent.
Parameters
methodName: string
Method name to invoke.
optionalcopyProps: any
plugin event object.
optionalpassthruProps: any
if true, event has plugin option.
optionalnameOrList: string | string[]
An optional plugin name or array / iterable of plugin names to invoke.
Returns Promise<void | default>
invokeSync
This dispatch method synchronously passes back a single value or an array with all results returned by any invoked targets.
Parameters
methodName: string
Method name to invoke.
optionalargs: any
Optional arguments. An array will be spread as multiple arguments.
optionalnameOrList: string | string[]
An optional plugin name or array / iterable of plugin names to invoke.
Returns any
invokeSyncEvent
This dispatch method synchronously passes to and returns from any invoked targets a PluginEvent.
Parameters
methodName: string
Method name to invoke.
optionalcopyProps: any
plugin event object.
optionalpassthruProps: any
optionalnameOrList: string | string[]
An optional plugin name or array / iterable of plugin names to invoke.
Returns default
isValidConfig
Performs validation of a PluginConfig.
Parameters
pluginConfig: PluginConfig
A PluginConfig to validate.
Returns boolean
remove
Removes a plugin by name after unloading it and clearing any event bindings automatically.
Parameters
pluginName: string
The plugin name to remove.
Returns boolean
removeAll
Removes all plugins after unloading them and clearing any event bindings automatically.
Returns void
removeAllAsync
Removes all plugins after unloading them and clearing any event bindings automatically.
Returns Promise<any>
removeAsync
Removes a plugin by name after unloading it and clearing any event bindings automatically.
Parameters
pluginName: string
The plugin name to remove.
Returns Promise<boolean>
setEventbus
Sets the eventbus associated with this plugin manager. If any previous eventbus was associated all plugin manager events will be removed then added to the new eventbus. If there are any existing plugins being managed their events will be removed from the old eventbus and then
onPluginLoad
will be called with the new eventbus.Parameters
targetEventbus: EventEmitter
The target eventbus to associate.
eventPrepend: string = "plugins"
An optional string to prepend to all of the event binding targets.
Returns default
setEventbusAsync
Sets the eventbus associated with this plugin manager. If any previous eventbus was associated all plugin manager events will be removed then added to the new eventbus. If there are any existing plugins being managed their events will be removed from the old eventbus and then
onPluginLoad
will be called with the new eventbus.Parameters
targetEventbus: EventEmitter
The target eventbus to associate.
optionaleventPrepend: string
An optional string to prepend to all of the event binding targets.
Returns Promise<default>
setExtraEventData
Sets any extra event data attached to PluginEvent
extra
field.Parameters
extraEventData: any = ...
Adds extra data to PluginEvent
extra
field.
Returns void
setOptions
Set optional parameters. All parameters are off by default.
Parameters
optionaloptions: PluginManagerOptions
Defines optional parameters to set.
Returns void
setPluginEnabled
Enables or disables a single plugin.
Parameters
pluginName: string
Plugin name to set state.
enabled: boolean
The new enabled state.
Returns boolean
setPluginsEnabled
Enables or disables a set of plugins given an array or iterabe of plugin names.
Parameters
pluginNames: string[]
An array / iterable of plugin names.
enabled: boolean
The new enabled state.
Returns boolean
Provides a lightweight plugin manager for Node / NPM with optional
backbone-esnext-events
integration for plugins in a safe and protected manner across NPM modules, local files, and preloaded object instances. This pattern facilitates message passing between modules versus direct dependencies / method invocation.It isn’t necessary to use an eventbus associated with the plugin manager though invocation then relies on invoking methods directly with the plugin manager instance.
When passing in an eventbus from
backbone-esnext-events
the plugin manager will register by default under these event categories:plugins:add
- addplugins:add:all
- addAllplugins:async:add
- addAsyncplugins:async:add:all
- addAllAsyncplugins:async:destroy:manager
- destroyAsyncplugins:async:invoke
- invokeAsyncplugins:async:invoke:event
- invokeAsyncEventplugins:async:remove
- removeAsyncplugins:async:remove:all
- removeAllAsyncplugins:create:event:proxy
- createEventProxyplugins:destroy:manager
- destroyplugins:get:all:plugin:data
- getAllPluginDataplugins:get:extra:event:data
- getExtraEventDataplugins:get:method:names
- getMethodNamesplugins:get:options
- getOptionsplugins:get:plugin:data
- getPluginDataplugins:get:plugin:enabled
- getPluginEnabledplugins:get:plugin:event:names
- getPluginEventNamesplugins:get:plugin:method:names
- getPluginMethodNamesplugins:get:plugin:names
- getPluginNamesplugins:get:plugin:options
- getPluginOptionsplugins:get:plugins:enabled
- getPluginsEnabledplugins:get:plugins:by:event:name
- getPluginsByEventNameplugins:get:plugins:event:names
- getPluginsEventNamesplugins:has:method
- hasMethodplugins:has:plugin
- hasPluginplugins:has:plugin:method
- hasPluginMethodplugins:invoke
- invokeplugins:is:valid:config
- isValidConfigplugins:remove
- removeplugins:remove:all
- removeAllplugins:set:extra:event:data
- setExtraEventDataplugins:set:options
- setOptionsplugins:set:plugin:enabled
- setPluginEnabledplugins:set:plugins:enabled
- setPluginsEnabledplugins:sync:invoke
- invokeSyncplugins:sync:invoke:event
- invokeSyncEventAutomatically when a plugin is loaded and unloaded respective callbacks
onPluginLoad
andonPluginUnload
will be attempted to be invoked on the plugin. This is an opportunity for the plugin to receive any associated eventbus and wire itself into it. It should be noted that a protected proxy around the eventbus is passed to the plugins such that when the plugin is removed automatically all events registered on the eventbus are cleaned up without a plugin author needing to do this manually in theonPluginUnload
callback. This solves any dangling event binding issues.The plugin manager also supports asynchronous operation with the methods ending in
Async
along with event bindings that includeasync
. For asynchronous variations ofadd
,destroy
, andremove
the lifecycle methodsonPluginLoad
andonPluginUnload
will be awaited on such that if a plugin returns a Promise or is an async method then it must complete before execution continues. One can use Promises to interact with the plugin manager asynchronously, but usage via async / await is recommended.If eventbus functionality is enabled it is important especially if using a process / global level eventbus such as
backbone-esnext-eventbus
to call destroy to clean up all plugin eventbus resources and the plugin manager event bindings.import Events from ‘backbone-esnext-events’; // Imports the TyphonEvents class for local usage. ::or alternatively:: import eventbus from ‘backbone-esnext-eventbus’; // Imports a global / process level eventbus.
import PluginManager from ‘typhonjs-plugin-manager’;
const pluginManager = new PluginManager({ eventbus });
pluginManager.add({ name: ‘an-npm-plugin-enabled-module’ }); pluginManager.add({ name: ‘my-local-module’, target: ‘./myModule.js’ });
// Let’s say an-npm-plugin-enabled-module responds to ‘cool:event’ which returns ‘true’. // Let’s say my-local-module responds to ‘hot:event’ which returns ‘false’. // Both of the plugin / modules will have ‘onPluginLoaded’ invoked with a proxy to the eventbus and any plugin // options defined.
// One can then use the eventbus functionality to invoke associated module / plugin methods even retrieving results. assert(eventbus.triggerSync(‘cool:event’) === true); assert(eventbus.triggerSync(‘hot:event’) === false);
// One can also indirectly invoke any method of the plugin via: eventbus.triggerSync(‘plugins:invoke:sync:event’, ‘aCoolMethod’); // Any plugin with a method named
aCoolMethod
is invoked. eventbus.triggerSync(‘plugins:invoke:sync:event’, ‘aCoolMethod’, {}, {}, ‘an-npm-plugin-enabled-module’); // specific invocation.// The 3rd parameter will make a copy of the hash and the 4th defines a pass through object hash sending a single // event / object hash to the invoked method.
// ———————–
// Given that
backbone-esnext-eventbus
defines a global / process level eventbus you can import it in an entirely // different file or even NPM module and invoke methods of loaded plugins like this:import eventbus from ‘backbone-esnext-eventbus’;
eventbus.triggerSync(‘plugins:invoke’, ‘aCoolMethod’); // Any plugin with a method named
aCoolMethod
is invoked.assert(eventbus.triggerSync(‘cool:event’) === true);
eventbus.trigger(‘plugins:remove’, ‘an-npm-plugin-enabled-module’); // Removes the plugin and unregisters events.
assert(eventbus.triggerSync(‘cool:event’) === true); // Will now fail!
// In this case though when using the global eventbus be mindful to always call
pluginManager.destroy()
in the main // thread of execution scope to remove all plugins and the plugin manager event bindings!