projects/wms-framework/src/lib/utils/SubscriptionEvent.ts
Properties |
Methods |
|
constructor(addAccessor: (value: T) => void, removeAccessor: (value: T) => void)
|
Public disabled |
Type : boolean
|
Default value : false
|
Inherited from
SubscriptionEvent
|
Defined in
SubscriptionEvent:18
|
handlers |
Type : []
|
Default value : []
|
Inherited from
SubscriptionEvent
|
Defined in
SubscriptionEvent:23
|
Public addHandler | ||||||||||||
addHandler(handler: T, self: any)
|
||||||||||||
Inherited from
SubscriptionEvent
|
||||||||||||
Defined in
SubscriptionEvent:119
|
||||||||||||
Parameters :
Returns :
T
|
Public removeHandler | ||||||||||||
removeHandler(handler: T, self: any)
|
||||||||||||
Inherited from
SubscriptionEvent
|
||||||||||||
Defined in
SubscriptionEvent:124
|
||||||||||||
Parameters :
Returns :
void
|
Public fire | ||||||
fire(args: any[])
|
||||||
Inherited from
SubscriptionEvent
|
||||||
Defined in
SubscriptionEvent:92
|
||||||
Fires the event and executes each handler
Parameters :
Returns :
void
|
Public hasHandlers |
hasHandlers()
|
Inherited from
SubscriptionEvent
|
Defined in
SubscriptionEvent:107
|
Returns :
boolean
|
Public insertHandler |
insertHandler(index: number, handler: T, self: any)
|
Inherited from
SubscriptionEvent
|
Defined in
SubscriptionEvent:47
|
Inserts handler to the event at the position specified. Allows to add priority to a handler at handlers execution.
Returns :
T
|
Public removeAllHandlers |
removeAllHandlers()
|
Inherited from
SubscriptionEvent
|
Defined in
SubscriptionEvent:80
|
Removes all handlers from the event
Returns :
void
|
Public toFunction |
toFunction()
|
Inherited from
SubscriptionEvent
|
Defined in
SubscriptionEvent:20
|
Returns :
T
|
export class SubscriptionEvent<T> {
public disabled: boolean = false;
public toFunction(): T {
return <T>(<any>((...args: any[]) => this.fire(args)));
}
handlers: [T, any][] = [];
/**
* Add handler to the event
*
* @param {T} handler
* @param {*} [self=null]
* @returns {T}
* @memberof SubscriptionEvent
*/
public addHandler(handler: T, self: any = null): T {
this.handlers.push([handler, self]);
return handler;
}
/**
* Inserts handler to the event at the position specified.
* Allows to add priority to a handler at handlers execution.
*
* @param {T} handler
* @param {*} [self=null]
* @returns {T}
* @memberof SubscriptionEvent
*/
public insertHandler(index: number, handler: T, self: any = null): T {
this.handlers.splice(index, 0, [handler, self]);
return handler;
}
/**
* Removes handler from the event
*
* @param {T} handler
* @param {*} [self=null]
* @memberof SubscriptionEvent
*/
public removeHandler(handler: T, self: any = null): void {
if (handler) {
let idx = this.handlers.findIndex((h) => h[0] == handler && h[1] == self);
// Final search for lambda expression using toString()
idx =
idx > 0
? idx
: this.handlers.findIndex(
(h) => h[0].toString() == handler.toString() && h[1] == self
);
if (idx >= 0) {
this.handlers.splice(idx, 1);
}
}
}
/**
* Removes all handlers from the event
*
* @memberof SubscriptionEvent
*/
public removeAllHandlers(): void {
if (this.hasHandlers()) {
this.handlers.splice(0, this.handlers.length);
}
}
/**
* Fires the event and executes each handler
*
* @param {any[]} args
* @memberof SubscriptionEvent
*/
public fire(args: any[]): void {
if (!this.disabled) {
//Map will create a copy to be immune of changes in this.handlers like calls to removeHandler, if no copy of the collection is created
//changes in this.handlers tend to skip items in the loop
for (let handler of this.handlers.map((x) => {
return x;
})) {
//The handler to be invoked might be removed by code, so we have to double check
const idx = this.handlers.findIndex((h) => h == handler);
if (idx >= 0 && handler[0] instanceof Function) {
handler[0].apply(handler[1] ?? window, args);
}
}
}
}
public hasHandlers(): boolean {
return this.handlers.length != 0;
}
}
export class CustomSubscriptionEvent<T> extends SubscriptionEvent<T> {
constructor(
private addAccessor: (value: T) => void,
private removeAccessor: (value: T) => void
) {
super();
}
public addHandler(handler: T, self: any = null): T {
this.addAccessor(handler);
super.addHandler(handler, self);
return handler;
}
public removeHandler(handler: T, self: any = null): void {
this.removeAccessor(handler);
super.removeHandler(handler, self);
}
}