appolo-event-dispatcher
Advanced tools
Comparing version 6.0.11 to 6.0.12
@@ -5,8 +5,10 @@ "use strict"; | ||
class Event { | ||
constructor() { | ||
constructor(_opts) { | ||
this._opts = _opts; | ||
this.EVENT_NAME = "event"; | ||
this._eventEventDispatcher = new eventDispatcher_1.EventDispatcher(); | ||
this._opts = Object.assign({}, { await: false, parallel: true }, _opts); | ||
} | ||
on(fn, scope, options) { | ||
this._eventEventDispatcher.on(this.EVENT_NAME, fn, scope, options); | ||
on(fn, scope) { | ||
this._eventEventDispatcher.on(this.EVENT_NAME, fn, scope, this._opts); | ||
} | ||
@@ -17,6 +19,6 @@ un(fn, scope) { | ||
once(fn, scope) { | ||
this._eventEventDispatcher.once(this.EVENT_NAME, fn, scope); | ||
this._eventEventDispatcher.once(this.EVENT_NAME, fn, scope, this._opts); | ||
} | ||
fireEvent(payload) { | ||
this._eventEventDispatcher.fireEvent(this.EVENT_NAME, payload); | ||
return this._eventEventDispatcher.fireEvent(this.EVENT_NAME, payload); | ||
} | ||
@@ -23,0 +25,0 @@ removeAllListeners() { |
@@ -7,2 +7,8 @@ "use strict"; | ||
export class Event<T> { | ||
constructor(private _opts?: { await: boolean, parallel: boolean }) { | ||
this._opts = Object.assign({}, {await: false, parallel: true}, _opts) | ||
} | ||
private readonly EVENT_NAME = "event"; | ||
@@ -13,4 +19,4 @@ | ||
public on(fn: (payload: T) => any, scope?: any, options?: IEventOptions): void { | ||
this._eventEventDispatcher.on(this.EVENT_NAME, fn, scope, options) | ||
public on(fn: (payload: T) => any, scope?: any): void { | ||
this._eventEventDispatcher.on(this.EVENT_NAME, fn, scope, this._opts) | ||
} | ||
@@ -23,7 +29,7 @@ | ||
public once(fn?: (payload: T) => any, scope?: any): Promise<any> | void { | ||
this._eventEventDispatcher.once(this.EVENT_NAME, fn, scope); | ||
this._eventEventDispatcher.once(this.EVENT_NAME, fn, scope, this._opts); | ||
} | ||
public fireEvent(payload: T): void { | ||
this._eventEventDispatcher.fireEvent(this.EVENT_NAME, payload) | ||
public fireEvent(payload: T): Promise<any> { | ||
return this._eventEventDispatcher.fireEvent(this.EVENT_NAME, payload) | ||
} | ||
@@ -30,0 +36,0 @@ |
@@ -21,3 +21,3 @@ "use strict"; | ||
scope: scope, | ||
options: options || {} | ||
options: Object.assign({ await: false, parallel: true }, options || {}) | ||
}); | ||
@@ -36,7 +36,7 @@ if (!handler.isRoutingKey && routingKey_1.RoutingKey.isRoutingRoute(event)) { | ||
if (fn) { | ||
return this.on(event, fn, scope, Object.assign({}, options, { once: true })); | ||
return this.on(event, fn, scope, Object.assign(Object.assign({}, options), { once: true })); | ||
} | ||
return new Promise((resolve) => { | ||
fn = (...args) => resolve(args.length > 1 ? args : args[0]); | ||
this.on(event, fn, scope, Object.assign({}, options, { once: true })); | ||
this.on(event, fn, scope, Object.assign(Object.assign({}, options), { once: true })); | ||
}); | ||
@@ -66,3 +66,3 @@ } | ||
} | ||
fireEvent(event, ...args) { | ||
async fireEvent(event, ...args) { | ||
if (!this[CallbacksSymbol]) { | ||
@@ -81,2 +81,3 @@ return; | ||
} | ||
let parallelPromises = []; | ||
for (let i = handler.callbacks.length - 1; i >= 0; i--) { | ||
@@ -87,3 +88,3 @@ let callback = handler.callbacks[i]; | ||
} | ||
callback.fn.apply((callback.scope || null), args); | ||
let result = callback.fn.apply((callback.scope || null), args); | ||
if (callback.options.once) { | ||
@@ -96,3 +97,14 @@ handler.callbacks.splice(i, 1); | ||
} | ||
if (callback.options.await) { | ||
if (callback.options.parallel) { | ||
parallelPromises.push(result); | ||
} | ||
else { | ||
await result; | ||
} | ||
} | ||
} | ||
if (parallelPromises.length) { | ||
await Promise.all(parallelPromises); | ||
} | ||
} | ||
@@ -99,0 +111,0 @@ _eventDispatcherGetRoutingKeys(handler, event) { |
@@ -32,3 +32,3 @@ "use strict"; | ||
scope: scope, | ||
options: options || {} | ||
options: Object.assign({await: false, parallel: true}, options || {}) | ||
}); | ||
@@ -94,3 +94,3 @@ | ||
public fireEvent(event: string, ...args: any[]): void { | ||
public async fireEvent(event: string, ...args: any[]): Promise<any> { | ||
@@ -114,2 +114,3 @@ if (!this[CallbacksSymbol]) { | ||
} | ||
let parallelPromises: Promise<any>[] = []; | ||
@@ -123,3 +124,3 @@ for (let i = handler.callbacks.length - 1; i >= 0; i--) { | ||
callback.fn.apply((callback.scope || null), args); | ||
let result = callback.fn.apply((callback.scope || null), args); | ||
@@ -133,5 +134,17 @@ if (callback.options.once) { | ||
} | ||
} | ||
if (callback.options.await) { | ||
if (callback.options.parallel) { | ||
parallelPromises.push(result) | ||
} else { | ||
await result; | ||
} | ||
} | ||
} | ||
if (parallelPromises.length) { | ||
await Promise.all(parallelPromises) | ||
} | ||
} | ||
@@ -138,0 +151,0 @@ |
export interface IEventOptions { | ||
once?: boolean, | ||
await?: boolean, | ||
parallel?: boolean | ||
} | ||
@@ -12,5 +15,5 @@ | ||
export interface IHandler{ | ||
export interface IHandler { | ||
callbacks: ICallback[], | ||
isRoutingKey: boolean | ||
} |
@@ -20,2 +20,3 @@ "use strict"; | ||
} | ||
exports.RoutingKey = RoutingKey; | ||
RoutingKey.Rules = [ | ||
@@ -27,3 +28,2 @@ [new RegExp('\\.', 'g'), '\\.'], | ||
RoutingKey.RegexRoute = new RegExp("\\*|#"); | ||
exports.RoutingKey = RoutingKey; | ||
//# sourceMappingURL=routingKey.js.map |
@@ -23,3 +23,3 @@ { | ||
"main": "./index.js", | ||
"version": "6.0.11", | ||
"version": "6.0.12", | ||
"license": "MIT", | ||
@@ -34,10 +34,9 @@ "repository": { | ||
"devDependencies": { | ||
"@types/bluebird": "^3.5.22", | ||
"@types/chai": "^4.1.2", | ||
"@types/mocha": "^5.0.0", | ||
"bluebird": "^3.5.1", | ||
"chai": "^4.1.2", | ||
"mocha": "^5.0.5", | ||
"typescript": "^2.8.1" | ||
"@types/bluebird": "^3.5.27", | ||
"@types/chai": "^4.2.1", | ||
"@types/mocha": "^5.2.7", | ||
"chai": "^4.2.0", | ||
"mocha": "^6.2.0", | ||
"typescript": "^3.6.2" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
35279
6
546