STB SDK base events emitter implementation
Almost all components in STB SDK emit events: a Model emits an event each time an attribute is set, a router emits an event when a page is opened.
All components which emit events are instances of Emitter
.
Typically, there aren't any strict restrictions on event names, as any string will be accepted. Usually it's a lowercase string with no spaces. Possible values: click
, move
, focus:item
.
Functions can then be attached to objects, to be executed when an event is emitted. These functions are called listeners. Inside a listener function, this refers to the Emitter
that the listener was attached to.
Installation
npm install stb-emitter
Usage
Add the constructor to the scope:
var Emitter = ;
Create an instance:
var emitter = ;
Add listeners for some events:
emitter;emitter;
Add listener that will be notified only one time:
emitter;
Add multiple listeners at once:
emitter;
Remove all instances of the given callback:
emitter;
Remove all callbacks for the given event name:
emitter;
Clears all events:
emitter;
Execute each of the listeners in the given order with the supplied arguments:
emitter;emitter;
Performance notes
It's a good idea to emit event only when there are some listeners:
if emitterevents'click' // notify listeners emitter;
Debug mode
There is a global var
DEBUG
which activates additional consistency checks and protection logic not available in release mode.
In debug mode the constructor is exposed to the global namespace as window.Emitter
.
Contribution
If you have any problem or suggestion please open an issue here. Pull requests are welcomed with respect to the JavaScript Code Style.
License
stb-emitter
is released under the GPL-3.0 License.