]>
git.r.bdr.sh - rbdr/dotfiles/blob - atom/packages/ex-mode/node_modules/event-kit/lib/emitter.js
2 var Disposable
, Emitter
;
4 Disposable
= require('./disposable');
6 module
.exports
= Emitter
= (function() {
7 Emitter
.prototype.isDisposed
= false;
11 Section: Construction and Destruction
15 this.handlersByEventName
= {};
18 Emitter
.prototype.dispose = function() {
19 this.handlersByEventName
= null;
20 return this.isDisposed
= true;
25 Section: Event Subscription
28 Emitter
.prototype.on = function(eventName
, handler
) {
30 if (this.isDisposed
) {
31 throw new Error("Emitter has been disposed");
33 if (typeof handler
!== 'function') {
34 throw new Error("Handler must be a function");
36 if (currentHandlers
= this.handlersByEventName
[eventName
]) {
37 this.handlersByEventName
[eventName
] = currentHandlers
.concat(handler
);
39 this.handlersByEventName
[eventName
] = [handler
];
41 return new Disposable(this.off
.bind(this, eventName
, handler
));
44 Emitter
.prototype.off = function(eventName
, handlerToRemove
) {
45 var handler
, newHandlers
, oldHandlers
, _i
, _len
;
46 if (this.isDisposed
) {
49 if (oldHandlers
= this.handlersByEventName
[eventName
]) {
51 for (_i
= 0, _len
= oldHandlers
.length
; _i
< _len
; _i
++) {
52 handler
= oldHandlers
[_i
];
53 if (handler
!== handlerToRemove
) {
54 newHandlers
.push(handler
);
57 return this.handlersByEventName
[eventName
] = newHandlers
;
63 Section: Event Emission
66 Emitter
.prototype.emit = function(eventName
, value
) {
67 var handler
, handlers
, _i
, _len
, _ref
;
68 if (handlers
= (_ref
= this.handlersByEventName
) != null ? _ref
[eventName
] : void 0) {
69 for (_i
= 0, _len
= handlers
.length
; _i
< _len
; _i
++) {
70 handler
= handlers
[_i
];