event-dispatch
Advanced tools
Comparing version 0.4.0 to 0.4.1
@@ -1,5 +0,11 @@ | ||
import { EventsHandler } from "./EventsHandler"; | ||
export declare class EventDispatcher { | ||
private handlers; | ||
addHandler(handler: EventsHandler): void; | ||
remove(eventName: string): void; | ||
remove(eventNames: string[]): void; | ||
remove(callback: (data: any) => void): void; | ||
detach(detachFrom: any, eventName?: string): void; | ||
detach(detachFrom: any, eventNames?: string[]): void; | ||
detach(detachFrom: any, callback?: (data: any) => void): void; | ||
attach(attachTo: any, eventName: string, callback: (data: any) => void): void; | ||
attach(attachTo: any, eventNames: string[], callback: (data: any) => void): void; | ||
on(eventName: string, callback: (data: any) => void): void; | ||
@@ -6,0 +12,0 @@ on(eventNames: string[], callback: (data: any) => void): void; |
@@ -8,12 +8,48 @@ "use strict"; | ||
// ------------------------------------------------------------------------- | ||
this.handlers = []; | ||
this.handlers = {}; | ||
} | ||
// ------------------------------------------------------------------------- | ||
// Adders | ||
// ------------------------------------------------------------------------- | ||
EventDispatcher.prototype.addHandler = function (handler) { | ||
this.handlers.push(handler); | ||
EventDispatcher.prototype.remove = function (eventNameOrNamesOrCallback) { | ||
var _this = this; | ||
if (eventNameOrNamesOrCallback instanceof Array) { | ||
eventNameOrNamesOrCallback.forEach(function (eventName) { return _this.remove(eventName); }); | ||
} | ||
else if (eventNameOrNamesOrCallback instanceof Function) { | ||
Object.keys(this.handlers).forEach(function (key) { | ||
_this.handlers[key] | ||
.filter(function (handler) { return handler.callback === eventNameOrNamesOrCallback; }) | ||
.forEach(function (handler) { return _this.handlers[key].splice(_this.handlers[key].indexOf(handler), 1); }); | ||
}); | ||
} | ||
else if (typeof eventNameOrNamesOrCallback === "string") { | ||
this.handlers[eventNameOrNamesOrCallback] = []; | ||
} | ||
}; | ||
EventDispatcher.prototype.on = function (eventNameOrNames, callback) { | ||
EventDispatcher.prototype.detach = function (detachFrom, eventNameOrNamesOrCallback) { | ||
var _this = this; | ||
if (eventNameOrNamesOrCallback instanceof Array) { | ||
eventNameOrNamesOrCallback.forEach(function (eventName) { return _this.remove(eventName); }); | ||
} | ||
else if (eventNameOrNamesOrCallback instanceof Function) { | ||
Object.keys(this.handlers).forEach(function (key) { | ||
_this.handlers[key] | ||
.filter(function (handler) { return handler.callback === eventNameOrNamesOrCallback; }) | ||
.forEach(function (handler) { return _this.handlers[key].splice(_this.handlers[key].indexOf(handler), 1); }); | ||
}); | ||
} | ||
else if (typeof eventNameOrNamesOrCallback === "string") { | ||
var key_1 = eventNameOrNamesOrCallback; | ||
this.handlers[key_1] | ||
.filter(function (handler) { return handler.attachedTo === detachFrom; }) | ||
.forEach(function (handler) { return _this.handlers[key_1].splice(_this.handlers[key_1].indexOf(handler), 1); }); | ||
} | ||
else { | ||
Object.keys(this.handlers).forEach(function (key) { | ||
_this.handlers[key] | ||
.filter(function (handler) { return handler.attachedTo === detachFrom; }) | ||
.forEach(function (handler) { return _this.handlers[key].splice(_this.handlers[key].indexOf(handler), 1); }); | ||
}); | ||
} | ||
}; | ||
EventDispatcher.prototype.attach = function (attachTo, eventNameOrNames, callback) { | ||
var _this = this; | ||
var eventNames = []; | ||
@@ -27,9 +63,10 @@ if (eventNameOrNames instanceof Array) { | ||
eventNames.forEach(function (eventName) { | ||
_this.handlers.push((_a = {}, | ||
_a[eventName] = callback, | ||
_a | ||
)); | ||
var _a; | ||
if (!_this.handlers[eventName]) | ||
_this.handlers[eventName] = []; | ||
_this.handlers[eventName].push({ attachedTo: attachTo, callback: callback }); | ||
}); | ||
}; | ||
EventDispatcher.prototype.on = function (eventNameOrNames, callback) { | ||
this.attach(undefined, eventNameOrNames, callback); | ||
}; | ||
EventDispatcher.prototype.dispatch = function (eventNameOrNames, data) { | ||
@@ -41,9 +78,8 @@ var _this = this; | ||
} | ||
else { | ||
else if (typeof eventNameOrNames === "string") { | ||
eventNames = [eventNameOrNames]; | ||
} | ||
eventNames.forEach(function (eventName) { | ||
_this.handlers | ||
.filter(function (handler) { return handler.hasOwnProperty(eventName); }) | ||
.forEach(function (handler) { return handler[eventName](data); }); | ||
if (_this.handlers[eventName]) | ||
_this.handlers[eventName].forEach(function (handler) { return handler.callback(data); }); | ||
MetadataRegistry_1.defaultMetadataRegistry | ||
@@ -50,0 +86,0 @@ .collectEventsHandlers |
{ | ||
"name": "event-dispatch", | ||
"private": false, | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "Dispatching and listening for application events in Typescript", | ||
@@ -31,2 +31,3 @@ "license": "Apache-2.0", | ||
"del": "^2.0.2", | ||
"es6-shim": "^0.35.1", | ||
"gulp": "^3.9.0", | ||
@@ -33,0 +34,0 @@ "gulp-mocha": "^2.2.0", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
22502
289
17