2016-03-13 18:28:29 +08:00
|
|
|
export class EventLimit<T> {
|
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private lastTriggerValue: T = null;
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
private handlers: Array<EventLimitHandler<T>> = [];
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public on(limit: T, handler: { (data?: T): void }) {
|
|
|
|
this.handlers.push(new EventLimitHandler(limit, handler));
|
|
|
|
if (this.lastTriggerValue != null) {
|
|
|
|
this.trigger(this.lastTriggerValue);
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public onSingle(limit: T, handler: { (data?: T): void }) {
|
|
|
|
this.handlers.push(new SingleFireEventLimitHandler(limit, handler));
|
|
|
|
if (this.lastTriggerValue != null) {
|
|
|
|
this.trigger(this.lastTriggerValue);
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public off(limit: T, handler: { (data?: T): void }) {
|
|
|
|
this.handlers = this.handlers.filter(h => h.handler !== handler && h.limit !== limit);
|
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public allOff() {
|
|
|
|
this.handlers = [];
|
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public trigger = (data?: T) => {
|
|
|
|
if (this.handlers) {
|
|
|
|
this.handlers.slice(0).forEach(h => {
|
|
|
|
if (h.limit <= data && (h.lastTriggerValue < h.limit || h.lastTriggerValue == null)) {
|
|
|
|
h.fire(data);
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
h.lastTriggerValue = data;
|
|
|
|
});
|
|
|
|
this.handlers = this.handlers.filter(h => h.isValid());
|
|
|
|
}
|
|
|
|
this.lastTriggerValue = data;
|
|
|
|
};
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
class EventLimitHandler<T> {
|
2017-06-11 04:32:56 +08:00
|
|
|
public lastTriggerValue: T = null;
|
|
|
|
|
|
|
|
constructor(public limit: T, public handler: { (data?: T): void }) {
|
|
|
|
}
|
|
|
|
|
|
|
|
public fire(data?: T) {
|
|
|
|
this.handler(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
public isValid(): boolean {
|
|
|
|
return true;
|
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
class SingleFireEventLimitHandler<T> extends EventLimitHandler<T> {
|
|
|
|
public fired = false;
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
constructor(public limit: T, public handler: { (data?: T): void }) {
|
|
|
|
super(limit, handler);
|
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
|
2017-06-11 04:32:56 +08:00
|
|
|
public fire(data?: T) {
|
|
|
|
if (this.fired == false) {
|
|
|
|
this.handler(data);
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
this.fired = true
|
|
|
|
}
|
|
|
|
|
|
|
|
public isValid(): boolean {
|
|
|
|
return this.fired === false;
|
|
|
|
}
|
2016-03-13 18:28:29 +08:00
|
|
|
}
|