Class PluginManager
Index
Constructors
Accessors
Methods
Constructors
constructor
- new
Plugin (options?): PluginManagerManager Instantiates PluginManager
Parameters
Optional
options: {
eventbus?: Eventbus;
eventPrepend?: string;
manager?: PluginManagerOptions;
PluginSupport?: PluginSupportConstructor | Iterable<PluginSupportConstructor, any, any>;
}Provides various configuration options:
Optional
eventbus?: EventbusAn instance of '@typhonjs-plugin/eventbus' used as the plugin eventbus. If not provided a default eventbus is created.
Optional
eventPrepend ?: stringA customized name to prepend PluginManager events on the eventbus.
Optional
manager?: PluginManagerOptionsThe plugin manager options.
Optional
PluginSupport ?: PluginSupportConstructor | Iterable<PluginSupportConstructor, any, any>Optional classes to pass in which extends the plugin manager. A default implementation is available: PluginInvokeSupport
Returns PluginManager
Accessors
isDestroyed
- get isDestroyed(): boolean
Returns whether this plugin manager has been destroyed.
Returns boolean
Returns whether this plugin manager has been destroyed.
Methods
add
- add(pluginConfig, moduleData?): Promise<PluginData>
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.
Optional
moduleData: objectOptional object hash to associate with plugin.
Returns Promise<PluginData>
The PluginData that represents the plugin added.
- pluginConfig: PluginConfig
addAll
- add
All (pluginConfigs, moduleData?): Promise<PluginData[]> Initializes multiple plugins in a single call.
Parameters
- pluginConfigs: Iterable<PluginConfig, any, any>
An iterable list of plugin config object hash entries.
Optional
moduleData: objectOptional object hash to associate with all plugins.
Returns Promise<PluginData[]>
An array of PluginData objects of all added plugins.
- pluginConfigs: Iterable<PluginConfig, any, any>
createEventbusProxy
- create
Eventbus (): EventbusProxyProxy 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.Returns EventbusProxy
A proxy for the currently set Eventbus.
createEventbusSecure
- create
Eventbus (name?): EventbusSecureSecure 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.Parameters
Optional
name: stringOptional name for the EventbusSecure instance.
Returns EventbusSecure
A secure wrapper for the currently set Eventbus.
destroy
- destroy(): Promise<DataOutPluginRemoved[]>
Destroys all managed plugins after unloading them.
Returns Promise<DataOutPluginRemoved[]>
A list of plugin names and removal success state.
getEnabled
- get
Enabled (opts?): boolean | DataOutPluginEnabled[] Returns the enabled state of a plugin, a list of plugins, or all plugins.
Parameters
Returns boolean | DataOutPluginEnabled[]
Enabled state for single plugin or array of results for multiple plugins.
getEventbus
getOptions
- get
Options (): PluginManagerOptions Returns a copy of the plugin manager options.
Returns PluginManagerOptions
A copy of the plugin manager options.
getPluginByEvent
- get
Plugin (opts): string[] | DataOutPluginEvents[]By Event Returns the event binding names registered on any associated plugin EventbusProxy.
Parameters
Returns string[] | DataOutPluginEvents[]
Event binding names registered from the plugin.
getPluginData
- get
Plugin (opts?): PluginData | PluginData[]Data Gets the plugin data for a plugin, list of plugins, or all plugins.
Parameters
Returns PluginData | PluginData[]
The plugin data for a plugin or list of plugins.
getPluginEntry
- get
Plugin (plugin): PluginEntryEntry Gets a PluginEntry instance for the given plugin name. This method is primarily for PluginSupportImpl classes.
Parameters
- plugin: string
The plugin name to get.
Returns PluginEntry
The PluginEntry for the given plugin name.
- plugin: string
getPluginEvents
- get
Plugin (opts?): DataOutPluginEvents[]Events Returns the event binding names registered on any associated plugin EventbusProxy.
Parameters
Returns DataOutPluginEvents[]
Event binding names registered from the plugin.
getPluginMapKeys
- get
Plugin (): Iterable<string, any, any>Map Keys Returns an iterable of plugin map keys (plugin names). This method is primarily for PluginSupportImpl classes.
Returns Iterable<string, any, any>
An iterable of plugin map keys.
getPluginMapValues
- get
Plugin (): Iterable<PluginEntry, any, any>Map Values Returns an iterable of plugin map keys (plugin names). This method is primarily for PluginSupportImpl classes.
Returns Iterable<PluginEntry, any, any>
An iterable of plugin map keys.
getPluginNames
- get
Plugin (opts?): string[]Names Returns all plugin names or if enabled is set then return plugins matching the enabled state.
Parameters
Optional
opts: {
enabled?: boolean;
}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.
Returns string[]
A list of plugin names optionally by enabled state.
hasPlugins
isValidConfig
- is
Valid (pluginConfig): booleanConfig Performs validation of a PluginConfig.
Parameters
- pluginConfig: PluginConfig
A PluginConfig to validate.
Returns boolean
True if the given PluginConfig is valid.
- pluginConfig: PluginConfig
reload
- reload(opts): Promise<boolean>
Unloads / reloads the plugin invoking
onPluginUnload
/ thenonPluginReload
Parameters
- opts: {
instance?: object;
plugin: string;
silent?: boolean;
}Options object.
Optional
instance?: objectOptional instance to replace.
plugin: string
Plugin name to reload.
Optional
silent?: booleanDoes not trigger any reload notification on the eventbus.
Returns Promise<boolean>
Result of reload attempt.
- opts: {
remove
- remove(opts): Promise<DataOutPluginRemoved[]>
Removes a plugin by name or all names in an iterable list unloading them and clearing any event bindings automatically.
Parameters
Returns Promise<DataOutPluginRemoved[]>
A list of plugin names and removal success state.
removeAll
- remove
All (): Promise<DataOutPluginRemoved[]> Removes all plugins after unloading them and clearing any event bindings automatically.
Returns Promise<DataOutPluginRemoved[]>
A list of plugin names and removal success state.
setEnabled
setEventbus
- set
Eventbus (opts): Promise<void> 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
Returns Promise<void>
setOptions
- set
Options (options): void Set optional parameters.
Parameters
- options: PluginManagerOptions
Defines optional parameters to set.
Returns void
- options: PluginManagerOptions
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