Basic events emitter implementation
Typically, there are no strict restrictions on event names, as any string will be accepted.
Usually it's a lowercase string with no spaces. Possible values:
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.
npm install @cjssdk/emitter
Add the constructor to the scope:
var Emitter = ;
Create an instance:
var emitter = ;
Add listeners for some events:
Add listener that will be notified only one time:
Add multiple listeners at once:
Remove all instances of the given callback:
Remove all callbacks for the given event name:
Clears all events:
Execute each of the listeners in the given order with the supplied arguments:
It's a good idea to emit events only when there are some listeners:
if emitterevents'click'// notify listenersemitter;
There is a global var
DEVELOPwhich activates additional consistency checks and protection logic not available in release mode.
@cjssdk/emitter is released under the MIT License.