E$
A lightweight, isomorphic event emitter.
Overview
E$ can be used standalone or inherited by other classes.
// Standaloneconst emoney = ; // Inherited { ... }
E$ provides a clean way to interface with object instances.
emoney ;
E$ instances can communicate via the
handleE$
method.
const emitter = ;const watcher = ;watcher;emitter;tohavebeencalledonce;
E$ can be used to create a DOM-like event tree.
const emitter = ;const watcher1 = ;const watcher2 = ;const spy = sinon; watcher2 ; watcher1 ; emitter;
Methods
(static)
E$.is(subject) → {boolean}
Returns true if subject is E$-ish, false otherwise.
const emoney = ;const emoneyIsh = ;const somethingElse = ; emoney instanceof E$; // trueE$; // true emoneyIsh instanceof E$; // falseE$; // true E$; // false
{instance}
.$when(events, argsopt, fnopt) → Adds an event listener.
Parameter | Type | Description | Required |
---|---|---|---|
events |
string array |
The event(s) to be handled. | yes |
args |
variant array |
The argument(s) to be bound to the event handler. | no |
fn |
function E$ |
The event handler. If E$.is(fn) == true , the event will be bound to instance.handleE$ .If fn is falsy, the event will be bound to emoney.handleE$ . |
no |
// basic useemoney; // bind an argument to multiple eventsemoney;
{instance}
.$once(events, argsopt, fnopt) → Adds an event listener that is removed after the first time it is invoked.
Parameter | Type | Description | Required |
---|---|---|---|
events |
string array |
The event(s) to be handled. | yes |
args |
variant array |
The argument(s) to be bound to the event fn. | no |
fn |
function E$ |
The event handler. | no |
// basic useemoney; // bind an argument to multiple eventsemoney;
{instance}
.$emit(events, argsopt, callbackopt) → Emits an event.
Parameter | Type | Description | Required |
---|---|---|---|
events |
string array |
The event(s) to be emitted. | yes |
args |
variant array |
The argument(s) to be passed to the event handler. | no |
callback |
function |
A function to be executed at the end of the event chain (see event behavior). | no |
// basic useemoney; // pass an argument to multiple event handlersemoney; // pass multiple arguments to an event handleremoney;
{instance}
.$dispel(events, wildopt, fnopt) → Removes an event listener.
Parameter | Type | Description | Required |
---|---|---|---|
events |
string array null |
The event(s) to be removed. | yes |
wild |
boolean |
A boolean value denoting whether handlers bound to the wildcard event should be removed. | no |
fn |
function E$ |
The event handler. | no |
// remove any gnarly listeners bound to fnemoney; // remove all gnarly or rad listeners bound to any handleremoney; // remove all listeners bound to fn except wildcard listenersemoney; // remove all listeners bound to fnemoney; // remove all listenersemoney;
{instance}
.$watch(emitters) → Starts watching E$ instance(s).
Parameter | Type | Description | Required |
---|---|---|---|
emitters |
E$ array |
The E$ instance(s) to watch. | yes |
// watch a single emitterlistener; // watch multiple emitterslistener;
{instance}
.$unwatch(emitters) → Stops watching E$ instance(s).
Parameter | Type | Description | Required |
---|---|---|---|
emitters |
E$ array |
The E$ instance(s) to stop watching. | yes |
// stop watching a single emitterlistener; // stop watching multiple emitterslistener;
Events
Properties
Property | Type | Default | Description |
---|---|---|---|
target |
E$ |
n/a |
The event target. |
type |
string |
n/a |
The event type. |
defaultPrevented |
boolean |
false |
A flag denoting whether default was prevented. |
cancelBubble |
boolean |
false |
A flag denoting whether propagation was stopped. |
timeStamp |
number |
n/a |
The time at which the event was first triggered. |
Methods
.preventDefault()
Prevents the $emit callback from being executed.
emoney ; /** * > 'fn1' * > 'fn2' */
.stopPropagation()
Stops execution of the event chain and executes the emit callback.
emoney ; /** * > 'fn1' * > 'cb' */
Behavior
Build & Test
npm i && npm run build