appolo-event-dispatcher
Advanced tools
Comparing version 6.0.10 to 6.0.11
@@ -69,17 +69,6 @@ "use strict"; | ||
let handler = this[CallbacksSymbol][event]; | ||
if ((!handler || !handler.isRoutingKey) && this[RoutingKeysSymbol]) { | ||
let routingKeysIndex = this[RoutingKeysSymbol], routingKeys = this[RoutingKeysCacheSymbol]; | ||
let routingKeys = this._eventDispatcherGetRoutingKeys(handler, event); | ||
if (routingKeys.length) { | ||
for (let i = 0, len = routingKeys.length; i < len; i++) { | ||
let routingKey = routingKeysIndex[routingKeys[i]]; | ||
if (!routingKey) { | ||
continue; | ||
} | ||
let cacheKey = routingKey.key + event; | ||
let shouldFireEvent = routingKey.cache[cacheKey]; | ||
if (shouldFireEvent === undefined) { | ||
shouldFireEvent = routingKey.cache[cacheKey] = routingKey.regex.test(event); | ||
} | ||
if (shouldFireEvent) { | ||
this.fireEvent(routingKey.key, ...args); | ||
} | ||
this.fireEvent(routingKeys[i], ...args); | ||
} | ||
@@ -105,2 +94,23 @@ } | ||
} | ||
_eventDispatcherGetRoutingKeys(handler, event) { | ||
if ((handler && handler.isRoutingKey) || !this[RoutingKeysSymbol]) { | ||
return []; | ||
} | ||
let keys = [], routingKeysIndex = this[RoutingKeysSymbol], routingKeys = this[RoutingKeysCacheSymbol]; | ||
for (let i = 0, len = routingKeys.length; i < len; i++) { | ||
let routingKey = routingKeysIndex[routingKeys[i]]; | ||
if (!routingKey) { | ||
continue; | ||
} | ||
let cacheKey = routingKey.key + event; | ||
let shouldFireEvent = routingKey.cache[cacheKey]; | ||
if (shouldFireEvent === undefined) { | ||
shouldFireEvent = routingKey.cache[cacheKey] = routingKey.regex.test(event); | ||
} | ||
if (shouldFireEvent) { | ||
keys.push(routingKey.key); | ||
} | ||
} | ||
return keys; | ||
} | ||
removeListenersByScope(scope) { | ||
@@ -133,6 +143,14 @@ let keys = Object.keys(this[CallbacksSymbol] || {}); | ||
let handler = this[CallbacksSymbol][event]; | ||
let routingKeys = this._eventDispatcherGetRoutingKeys(handler, event); | ||
if (routingKeys.length) { | ||
for (let i = 0, len = routingKeys.length; i < len; i++) { | ||
if (this.hasListener(routingKeys[i], fn, scope)) { | ||
return true; | ||
} | ||
} | ||
} | ||
if (!handler || !handler.callbacks.length) { | ||
return false; | ||
} | ||
if (arguments.length == 1) { | ||
if (arguments.length == 1 || !fn) { | ||
return true; | ||
@@ -149,7 +167,13 @@ } | ||
listenerCount(event) { | ||
let handler = this[CallbacksSymbol][event]; | ||
let handler = this[CallbacksSymbol][event], sum = 0; | ||
let routingKeys = this._eventDispatcherGetRoutingKeys(handler, event); | ||
if (routingKeys.length) { | ||
for (let i = 0, len = routingKeys.length; i < len; i++) { | ||
sum += this.listenerCount(routingKeys[i]); | ||
} | ||
} | ||
if (!handler) { | ||
return 0; | ||
return sum; | ||
} | ||
return handler.callbacks.length; | ||
return sum + handler.callbacks.length; | ||
} | ||
@@ -156,0 +180,0 @@ } |
"use strict"; | ||
import {ICallback, IEventOptions} from "./IEventOptions"; | ||
import {ICallback, IEventOptions, IHandler} from "./IEventOptions"; | ||
import {IEventDispatcher} from "./IEventDispatcher"; | ||
@@ -14,3 +14,3 @@ import {RoutingKey} from "./routingKey"; | ||
protected [CallbacksSymbol]: { [index: string]: { callbacks: ICallback[], isRoutingKey: boolean } }; | ||
protected [CallbacksSymbol]: { [index: string]: IHandler }; | ||
protected [RoutingKeysSymbol]: { [index: string]: { key: string, regex: RegExp, cache: { [index: string]: boolean } } }; | ||
@@ -102,25 +102,7 @@ | ||
if ((!handler || !handler.isRoutingKey) && this[RoutingKeysSymbol]) { | ||
let routingKeys = this._eventDispatcherGetRoutingKeys(handler, event); | ||
let routingKeysIndex = this[RoutingKeysSymbol], | ||
routingKeys = this[RoutingKeysCacheSymbol]; | ||
if (routingKeys.length) { | ||
for (let i = 0, len = routingKeys.length; i < len; i++) { | ||
let routingKey = routingKeysIndex[routingKeys[i]]; | ||
if (!routingKey) { | ||
continue; | ||
} | ||
let cacheKey = routingKey.key + event; | ||
let shouldFireEvent = routingKey.cache[cacheKey]; | ||
if (shouldFireEvent === undefined) { | ||
shouldFireEvent = routingKey.cache[cacheKey] = routingKey.regex.test(event) | ||
} | ||
if (shouldFireEvent) { | ||
this.fireEvent(routingKey.key, ...args) | ||
} | ||
this.fireEvent(routingKeys[i], ...args) | ||
} | ||
@@ -154,2 +136,36 @@ } | ||
private _eventDispatcherGetRoutingKeys(handler: IHandler, event: string): string[] { | ||
if ((handler && handler.isRoutingKey) || !this[RoutingKeysSymbol]) { | ||
return []; | ||
} | ||
let keys = [], | ||
routingKeysIndex = this[RoutingKeysSymbol], | ||
routingKeys = this[RoutingKeysCacheSymbol]; | ||
for (let i = 0, len = routingKeys.length; i < len; i++) { | ||
let routingKey = routingKeysIndex[routingKeys[i]]; | ||
if (!routingKey) { | ||
continue; | ||
} | ||
let cacheKey = routingKey.key + event; | ||
let shouldFireEvent = routingKey.cache[cacheKey]; | ||
if (shouldFireEvent === undefined) { | ||
shouldFireEvent = routingKey.cache[cacheKey] = routingKey.regex.test(event) | ||
} | ||
if (shouldFireEvent) { | ||
keys.push(routingKey.key) | ||
} | ||
} | ||
return keys; | ||
} | ||
public removeListenersByScope(scope: any): void { | ||
@@ -185,3 +201,3 @@ | ||
public hasListener(event: string, fn?: (...args: any[]) => any, scope?: any): boolean { | ||
public hasListener(event: string, fn ?: (...args: any[]) => any, scope ?: any): boolean { | ||
if (!this[CallbacksSymbol]) { | ||
@@ -193,2 +209,12 @@ return false; | ||
let routingKeys = this._eventDispatcherGetRoutingKeys(handler, event); | ||
if (routingKeys.length) { | ||
for (let i = 0, len = routingKeys.length; i < len; i++) { | ||
if (this.hasListener(routingKeys[i], fn, scope)) { | ||
return true; | ||
} | ||
} | ||
} | ||
if (!handler || !handler.callbacks.length) { | ||
@@ -198,3 +224,3 @@ return false; | ||
if (arguments.length == 1) { | ||
if (arguments.length == 1 || !fn) { | ||
return true; | ||
@@ -215,11 +241,20 @@ } | ||
public listenerCount(event: string): number { | ||
let handler = this[CallbacksSymbol][event]; | ||
let handler = this[CallbacksSymbol][event], | ||
sum = 0; | ||
let routingKeys = this._eventDispatcherGetRoutingKeys(handler, event); | ||
if (routingKeys.length) { | ||
for (let i = 0, len = routingKeys.length; i < len; i++) { | ||
sum += this.listenerCount(routingKeys[i]); | ||
} | ||
} | ||
if (!handler) { | ||
return 0; | ||
return sum; | ||
} | ||
return handler.callbacks.length | ||
return sum + handler.callbacks.length | ||
} | ||
} | ||
@@ -10,2 +10,7 @@ export interface IEventOptions { | ||
options?: IEventOptions | ||
} | ||
} | ||
export interface IHandler{ | ||
callbacks: ICallback[], | ||
isRoutingKey: boolean | ||
} |
@@ -23,3 +23,3 @@ { | ||
"main": "./index.js", | ||
"version": "6.0.10", | ||
"version": "6.0.11", | ||
"license": "MIT", | ||
@@ -26,0 +26,0 @@ "repository": { |
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
33416
516