Plugin Container
Plugin-container is a design pattern similar to Observer, but with ore emphasis on data processing, state change, and cancelable actions.
Powerful and simple pattern to add extension points to your APIs or frameworks.
API Reference
https://cancerberosgx.github.io/plugin-container/interfaces/iplugincontainer.html
Install
npm install --save plugin-container
Usage
;const plugins = ;plugins;plugins; const output = plugins;console// the output is the transformation, in other, of all the plugins, in this case:// 'avacadabrahello world loremipsum worldflumflumloremipsumsrpic'
Use it in the browser
plugin-container supports very old browsers. Just use files in build/es3
, for example, after loading plugin-container-globals.js the global variable PluginContainer
will be available:
Or you can use the AMD version if you want:
Ideas / TODOs
Errors
if a plugin.execute throws an exception - what the container should do ?
asynchronous plugins
- executeAll is sync and plugins.execute() must be sync
Proposal:
// install an asynchronous plugin. If execute returns a promise / thenable - then the ocntainer will wait for it to resolve / reject before executing the next plugins
plugins.install({ name: 'my-async-plugin, priority: 1 execute: (input){ return new Promise(resolve=>{ return request('third/party/service').then(error=>{ if(error){ input.thirdPartyValidationError = error reject(error) } else{ input.thirdPartyValidationOk=!error; resolve(input) // so next plugins can keep processing the input } }) }) } })
// install a synchronous plugin - it wil lbe executed after the first one is resolved or rejected plugins.install( name: 'my-async-plugin, priority: 2 execute: function(input){ if(input.thirdPartyValidationOk){ doSomethingWith(input) } return input } )
Questions: What is the semantics of a rejected promise ? What the container should do ? probably rejected promises have same semantics that throws.
Cancelling
- use case: I want my users to be able to subscribe plugins before something happens and