Table of Contents
Zephyr
Plugin functionality for modular libraries.
For an implementation using this module see air.
Install
npm i zephyr --save
Usage
var plug = // create the plugin system sys = ;// load pluginssys;// create a componentvar component = ;// do something with the plugin functionality
Options
proto
: A reference to the prototype object.type
: A reference to the class to instantiate.main
: An alternative main function (factory).plugin
: Override the default plugin function.hooks
: Array of functions invoked as constructor hooks.field
: String name of field for plugin function.
Plugins
Plugins are functions invoked in the scope of a class prototype that typically decorate the prototype object (using this
) but may also add static methods or load other plugins.
Loading Plugins
To load plugin(s) call the plugin
function passing an array of plugin functions:
var plug = sys = ;sys;
It is possible to pass a configuration object at runtime to a plugin by using an object with a plugin
function and a conf
object:
var plug = sys = ;var plugins = { // do something with the runtime configuration // initialize the plugin } conf: foo: 'bar' ;sys;
Creating Plugins
The most common use case for plugins is to decorate the class prototype with functions that are available on instances returned by the main function, these are referred to as instance plugins. Plugins may also decorate the main function these are referred to as static plugins.
Plugin implementations may mix functionality, for clarity the examples show the distinct styles.
Instance Plugins
To create an instance plugin just assign a function to this
within the plugin function:
module { // decorate class prototype this { // return this to allow chaining on this function return this; }}
Now load the plugin and invoke the instance method:
var comp plug = // create the plugin system sys = ;// load the pluginsys;// get the instance from the main functioncomp = ;// invoke the plugin methodcomp;
Static Plugins
To decorate the main function with static functions assign to this.main
.
module { thismain { // implement method functionality }}
You can then invoke the function on the plugin system:
var plug = sys = ;sys;sysmethod;
Composite Plugins
You can depend upon other plugins by calling this.plugin
within the plugin function. This allows plugins to composite other plugins in order to resolve plugin dependencies or provide plugin groups (related plugins).
module { this;}
By convention plugins are singular and plugin groups are plural.
Named Plugin
Typically a plugin will be a single module (file) and the plugin function is exported, however sometimes you may prefer to export a class or other function; in this case the plugin initialization function may be assigned to the exported object and referenced using the field
option.
Consider a module that exports a class but also wishes to expose a plugin function:
{} Component { // implement plugin functionality} moduleexports = Component;
We can then configure the plugin system by specifying the field
option with the name of the function, in this case init
:
var zephyr = main = ;moduleexports = main;
Then we can require the file when loading the plugin and the init
function will be invoked for plugin initialization:
main;
Configuration
Plugins accept a single argument which is a configuration object optionally passed when loading the plugin. Useful when a plugin wishes to add functionality conditionally. For example:
module { conf = conf || {}; // implement default logic ifconfext // implement extended logic }
Then a consumer of the plugin system could enable the extended logic:
sys
Hooks
For some plugin systems it is useful to be able to add functionality in the scope of the component instance rather than the prototype. For example to add a default listener for an event, set properties on the instance or start running logic on component creation (or based on the plugin configuration).
Pass an array as the hooks
option:
var plug = sys = ;
And an additional register
method is available on plugin
:
{ // do something on component instantiation}module { // register the constructor hook thisplugin;}
Note that hooks are only applied when the component is created with the main function:
var plug = sys = ;sys;// constructor hooks are appliedvar comp = ;// bypass constructor hooks, probably not desirablecomp = ;
Systems
A plugin system is the result of invoking the zephyr
function:
var plug = sys = ;moduleexports = sys;
Which allows the ability to mix multiple components using plugins in the same code base. Typically you would export the main function returned as the plugin system.
Extend
Pass the proto
and type
options to extend the plugin system:
var plug = ; // custom constructor {} var proto = PluginSystemprototype// extend the prototype with base functionality// available to all pluginsvar sys = ;moduleexports = sys;
For an example implementation see air.js.
Source
; { 'use strict' { opts = opts || {}; /** * Default plugin class. */ {} var main hooks = optshooks proto = optsproto || Componentprototype; /** * Plugin method. * * @param plugins Array of plugin functions. */ { var z method conf; forz in plugins iftypeof pluginsz === 'function' method = pluginsz; else method = pluginszplugin; conf = pluginszconf; ifoptsfield && typeof methodoptsfield === 'function' method = methodoptsfield; method; return main; } /** * Create an instance of the class represented by *Type* and proxy * all arguments to the constructor. */ { var args = Arrayprototypeslice; { return mainType; } Fnprototype = mainTypeprototype; return ; } /** * Invoke constructor hooks by proxying to the main construct * function and invoking registered hook functions in the scope * of the created component. */ { var comp = hookproxy; forvar i = 0;i < hookslength;i++ hooksi; return comp; } /** * Register a constructor hook function. * * @param fn The constructor hook. */ { iftypeof fn === 'function' && !~hooks hooks; } main = optsmain || construct; // hooks enabled, wrap main function aop style ifArray hookproxy = main; main = hook; // class to construct mainType = optstype || Component; // static and instance plugin method mainplugin = protoplugin = optsplugin || plugin; // hooks enabled, decorate with register function ifArray mainpluginregister = register; // reference to the main function for static assignment protomain = main; return main; } moduleexports = plug;};
Developer
Developer workflow is via gulp but should be executed as npm
scripts to enable shell execution where necessary.
Test
Run the headless test suite using phantomjs:
npm test
To run the tests in a browser context open test/index.html or use the server npm start
.
Start
Serve the test files from a web server with:
npm start
Cover
Run the test suite and generate code coverage:
npm run cover
Lint
Run the source tree through eslint:
npm run lint
Clean
Remove generated files:
npm run clean
Spec
Compile the test specifications:
npm run spec
Instrument
Generate instrumented code from lib
in instrument
:
npm run instrument
Readme
Generate the project readme file (requires mdp):
npm run readme
License
Everything is MIT. Read the license if you feel inclined.
Generated by mdp(1).