Radar.js
A simple wrapper for objects that emits events around specified methods. It effectively sandwiches object methods between two event emitters.
Installation
npm install radar.js
Quick Start
var Radar = radar = methods: "leave" ; var bear = radar; radar; radar; radar; radar; bear;bear; // Bear is thinking about leaving...// Bear is leaving...// Bear is thinking about staying...// Bear is staying...// Bear left.// Bear stayed.
Sync vs. Async
This library works with both synchronous and asynchronous methods, but it does require an adherence to a certain asynchronous convention -- callback parameters MUST be the last parameter supplied to a method. For example...
var okay = radar; var notOkay = radar;
API
Radar(options) [constructor]
Create a new Radar object with some optional configuration.
Usage
var radar = ;var radar = methods: "leave" ;
Arguments
name | req | type |
---|---|---|
options | n | obj |
Options
name | req | type | default | description |
---|---|---|---|---|
separator | n | str |
":" |
separates the prefix and method name (prefix:method) |
methods | n | arr |
[] |
list of methods to wrap, if they exist |
Radar.wrap(object, options)
Wraps designated methods within an object with before and after event emitters.
Usage
var bear = radar; var fox = radar
Arguments
name | req | type |
---|---|---|
object | y | obj |
options | y | obj |
Options
name | req | type | default | description |
---|---|---|---|---|
prefix | y | str |
"" |
prefixes the event names attached to this object |
separator | n | str |
":" |
overrides the default separator for the object argument's events only |
methods | n | arr |
[] |
list of additional methods to wrap for the object argument only |
Radar.before(eventName, handler)
Trigger handler before the wrapped method is called on the object
. The context (this
) within this
method is the object
which the event was registered against.
Usage
radar;
Arguments
name | req | type |
---|---|---|
eventName | y | str |
handler | y | func |
Radar.after(eventName, handler)
Trigger handler after the wrapped method is called on the object
. The context (this
) within this
method is the object
which the event was registered against.
Usage
radar;
Arguments
name | req | type |
---|---|---|
eventName | y | str |
handler | y | func |