Instantiates PluginManager
Optional
options: { Provides various configuration options:
Optional
PluginOptional classes to pass in which extends the plugin manager. A default implementation is available: PluginInvokeSupport
Optional
eventA customized name to prepend PluginManager events on the eventbus.
Optional
eventbus?: EventbusAn instance of '@typhonjs-plugin/eventbus' used as the plugin eventbus. If not provided a default eventbus is created.
Optional
manager?: PluginManagerOptionsThe plugin manager options.
Returns whether this plugin manager has been destroyed.
Returns whether this plugin manager has been destroyed.
Adds a plugin by the given configuration parameters. A plugin name
is always required. If no other options
are provided then the name
doubles as the NPM module / local file to load. The loading first checks for an
existing instance
to use as the plugin. Then the target
is chosen as the NPM module / local file to load.
By passing in options
this will be stored and accessible to the plugin during all callbacks.
Defines the plugin to load.
Optional
moduleData: objectOptional object hash to associate with plugin.
The PluginData that represents the plugin added.
Initializes multiple plugins in a single call.
An iterable list of plugin config object hash entries.
Optional
moduleData: objectOptional object hash to associate with all plugins.
An array of PluginData objects of all added plugins.
If an eventbus is assigned to this plugin manager then a new EventbusProxy wrapping this eventbus is returned.
It is added to this.#eventbusProxies
so †hat the instances are destroyed when the plugin manager is destroyed.
A proxy for the currently set Eventbus.
If an eventbus is assigned to this plugin manager then a new EventbusSecure wrapping this eventbus is returned.
It is added to this.#eventbusSecure
so †hat the instances are destroyed when the plugin manager is destroyed.
Optional
name: stringOptional name for the EventbusSecure instance.
A secure wrapper for the currently set Eventbus.
Destroys all managed plugins after unloading them.
A list of plugin names and removal success state.
Returns the enabled state of a plugin, a list of plugins, or all plugins.
Enabled state for single plugin or array of results for multiple plugins.
Returns a copy of the plugin manager options.
A copy of the plugin manager options.
Returns the event binding names registered on any associated plugin EventbusProxy.
Event binding names registered from the plugin.
Gets the plugin data for a plugin, list of plugins, or all plugins.
The plugin data for a plugin or list of plugins.
Gets a PluginEntry instance for the given plugin name. This method is primarily for PluginSupportImpl classes.
The plugin name to get.
The PluginEntry for the given plugin name.
Returns the event binding names registered on any associated plugin EventbusProxy.
Event binding names registered from the plugin.
Returns an iterable of plugin map keys (plugin names). This method is primarily for PluginSupportImpl classes.
An iterable of plugin map keys.
Returns an iterable of plugin map keys (plugin names). This method is primarily for PluginSupportImpl classes.
An iterable of plugin map keys.
Returns all plugin names or if enabled is set then return plugins matching the enabled state.
Optional
opts: { Options object. If undefined all plugin names are returned regardless of enabled state.
Optional
enabled?: booleanIf enabled is a boolean it will return plugins given their enabled state.
A list of plugin names optionally by enabled state.
Performs validation of a PluginConfig.
A PluginConfig to validate.
True if the given PluginConfig is valid.
Unloads / reloads the plugin invoking onPluginUnload
/ then onPluginReload
Options object.
Optional
instance?: objectOptional instance to replace.
Plugin name to reload.
Optional
silent?: booleanDoes not trigger any reload notification on the eventbus.
Result of reload attempt.
Removes a plugin by name or all names in an iterable list unloading them and clearing any event bindings automatically.
A list of plugin names and removal success state.
Removes all plugins after unloading them and clearing any event bindings automatically.
A list of plugin names and removal success state.
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.
Set optional parameters.
Defines optional parameters to set.
Provides a lightweight plugin manager for Node / NPM & the browser with eventbus 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.
A default eventbus will be created, but you may also pass in an eventbus from
@typhonjs-plugin/eventbus
and the plugin manager will register by default under these event categories:plugins:async:add
- PluginManager#addplugins:async:add:all
- PluginManager#addAllplugins:async:destroy:manager
- PluginManager#destroyplugins:async:remove
- PluginManager#removeplugins:async:remove:all
- PluginManager#removeAllplugins:get:enabled
- PluginManager#getEnabledplugins:get:options
- PluginManager#getOptionsplugins:get:plugin:by:event
- PluginManager#getPluginByEventplugins:get:plugin:data
- PluginManager#getPluginDataplugins:get:plugin:events
- PluginManager#getPluginEventsplugins:get:plugin:names
- PluginManager#getPluginNamesplugins:has:plugin
- PluginManager#hasPluginsplugins:is:valid:config
- PluginManager#isValidConfigplugins:set:enabled
- PluginManager#setEnabledplugins:set:options
- PluginManager#setOptionsAutomatically when a plugin is loaded and unloaded respective functions
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.By supporting ES Modules / CommonJS in Node and ES Modules in the browser the plugin manager is by nature asynchronous for the core methods of adding / removing plugins and destroying the manager. The lifecycle methods
onPluginLoad
andonPluginUnload
will be awaited on such that if a plugin returns a Promise or is an async method then it will complete before execution continues.It is recommended to interact with the plugin manager eventbus through an eventbus proxy. The
createEventbusProxy
method will return a proxy to the default or currently set eventbus.It should be noted that this module re-exports
@typhonjs-plugin/eventbus
which is available as named exports via theeventbus
subpath export:This reexport is for convenience as it provides one single distribution for Node & browser usage.
If external eventbus functionality is enabled by passing in an eventbus in the constructor of PluginManager it is important especially if using an existing process / global level eventbus instance from either this module or
@typhonjs-plugin/eventbus
to call PluginManager#destroy to clean up all plugin eventbus resources and the plugin manager event bindings; this is primarily a testing concern when running repeated tests over a reused eventbus.For more information on Eventbus functionality please see:
See
https://www.npmjs.com/package/@typhonjs-plugin/eventbus
The PluginManager instance can be extended through runtime composition by passing in classes that implement PluginSupportImpl. One such implementation is available PluginInvokeSupport which enables directly invoking methods of all or specific plugins. Please see the documentation for PluginInvokeSupport for more details.
Several abbreviated examples follow. Please see the wiki for more details: TODO: add wiki link
Example