Commando Library
Tiny abstraction over promises and command pattern to ease decoupling in Javascript project.
This project has no dependency and only add ~1.8k in amd minified version and < 700 bytes if gzipped.
What's in
- command launchers:
default
, which use a function as a command,promise
, which wraps your command in a promise
- command pool which basically create a map between event and commands
Version
AMD
- non minified:
dist/commando.amd.js
- minified version:
dist/commando.amd.min.js
Common JS
- the whole library:
dist/commonjs/commando.js
- or if you only want a part:
- command launcher:
dist/commonjs/commando/launcher
- command pool:
dist/commonjs/commando/pool
- command launcher:
Browser version
TODO
Usage
Command launcher without promise support
// given this simple commandvar { console;}; // Setup default launcher without promise support.var launcher = ; launcher; // it will print the following message to consolefoo bar
Command launcher with promise support
// Setup launcher with Promise object.var launcher = promise: Promise; // Execute a command// Command should have a constructor which conforms to this API:// function(resolve, reject) {} { // keep a reference on these two resolvers thissuccess = resolve; thiserror = reject;}; Commandprototype // do something and calls // e.g this function will invert a and b ok ? this : this;; var promise = launcher; // Add a callbackpromise;
Command pool
// (Optional create a command map)var commandMap = login: LoginCommand; // Pass some arguments// * eventHub (required) : any object with on, off and trigger methods// * commandMap (optional) : to register some commands// * options: (optional): Object to set some options. Supported options:// + launcher: String ('default', 'promise'): enable the setup of the launcher// using the default ones provided. If you want to setup a custom launcher use// the `withLauncher(object)` methodvar pool = eventHub commandMap options; // register a new command for `logout` eventpool; // register two command for `username:change` eventpool; // remove previously registered login commandpool; // remove all commands attached to event 'username:change'pool;
Chaining API
You can define add multiple commands at once as the addCommand
and delCommand
are chainable. Thus you can write the previous example like this:
// (Optional create a command map)var commandMap = login: LoginCommand; // Pass some arguments// * eventHub (required) : any object with on, off and trigger methods// * commandMap (optional) : to register some commands// * options: (optional): Object to set some options. Supported options:// + launcher: String ('default', 'promise'): enable the setup of the launcher// using the default ones provided. If you want to setup a custom launcher use// the `withLauncher(object)` methodvar pool = eventHub commandMap options; pool // register a new command for `logout` event // register two command for `username:change` event ; pool // remove previously registered login command // remove all commands attached to event 'username:change' ;
Example
TODO
API
See docs folder.
Roadmap
- add tests
- add examples
- add docs
- provide a browser version
License
MIT