2020-04-26 22:22:39 +02:00
|
|
|
/*
|
2022-01-13 13:54:34 +01:00
|
|
|
* (C) Copyright 2017-2022 OpenVidu (https://openvidu.io)
|
2020-04-26 22:22:39 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { Event as Event } from '../OpenViduInternal/Events/Event';
|
2022-01-13 13:54:34 +01:00
|
|
|
import { EventMap } from '../OpenViduInternal/Events/EventMap/EventMap';
|
2022-01-10 12:46:39 +01:00
|
|
|
|
2020-04-26 22:22:39 +02:00
|
|
|
import EventEmitter = require('wolfy87-eventemitter');
|
2020-05-04 20:01:56 +02:00
|
|
|
import { OpenViduLogger } from '../OpenViduInternal/Logger/OpenViduLogger';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
const logger: OpenViduLogger = OpenViduLogger.getInstance();
|
2020-04-26 22:22:39 +02:00
|
|
|
|
|
|
|
export abstract class EventDispatcher {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
userHandlerArrowHandler: WeakMap<(event: Event) => void, (event: Event) => void> = new WeakMap();
|
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
ee = new EventEmitter();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds function `handler` to handle event `type`
|
|
|
|
*
|
|
|
|
* @returns The EventDispatcher object
|
|
|
|
*/
|
2022-01-13 13:54:34 +01:00
|
|
|
abstract on<K extends keyof (EventMap)>(type: K, handler: (event: (EventMap)[K]) => void): this;
|
2020-04-26 22:22:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds function `handler` to handle event `type` just once. The handler will be automatically removed after first execution
|
|
|
|
*
|
|
|
|
* @returns The object that dispatched the event
|
|
|
|
*/
|
2022-01-13 13:54:34 +01:00
|
|
|
abstract once<K extends keyof (EventMap)>(type: K, handler: (event: (EventMap)[K]) => void): this;
|
2020-04-26 22:22:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Removes a `handler` from event `type`. If no handler is provided, all handlers will be removed from the event
|
|
|
|
*
|
|
|
|
* @returns The object that dispatched the event
|
|
|
|
*/
|
2022-01-13 13:54:34 +01:00
|
|
|
abstract off<K extends keyof (EventMap)>(type: K, handler?: (event: (EventMap)[K]) => void): this;
|
2020-04-26 22:22:39 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
onAux(type: string, message: string, handler: (event: Event) => void): EventDispatcher {
|
|
|
|
const arrowHandler = event => {
|
|
|
|
if (event) {
|
2021-04-29 23:37:44 +02:00
|
|
|
logger.info(message, event);
|
2020-04-26 22:22:39 +02:00
|
|
|
} else {
|
2021-04-29 23:37:44 +02:00
|
|
|
logger.info(message);
|
2020-04-26 22:22:39 +02:00
|
|
|
}
|
|
|
|
handler(event);
|
|
|
|
};
|
|
|
|
this.userHandlerArrowHandler.set(handler, arrowHandler);
|
|
|
|
this.ee.on(type, arrowHandler);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
onceAux(type: string, message: string, handler: (event: Event) => void): EventDispatcher {
|
|
|
|
const arrowHandler = event => {
|
|
|
|
if (event) {
|
2021-04-29 23:37:44 +02:00
|
|
|
logger.info(message, event);
|
2020-04-26 22:22:39 +02:00
|
|
|
} else {
|
2021-04-29 23:37:44 +02:00
|
|
|
logger.info(message);
|
2020-04-26 22:22:39 +02:00
|
|
|
}
|
|
|
|
handler(event);
|
|
|
|
// Remove handler from map after first and only execution
|
|
|
|
this.userHandlerArrowHandler.delete(handler);
|
|
|
|
};
|
|
|
|
this.userHandlerArrowHandler.set(handler, arrowHandler);
|
|
|
|
this.ee.once(type, arrowHandler);
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2022-01-10 12:46:39 +01:00
|
|
|
/**
|
|
|
|
* @hidden
|
|
|
|
*/
|
|
|
|
offAux(type: string, handler?: (event: Event) => void): EventDispatcher {
|
|
|
|
if (!handler) {
|
|
|
|
this.ee.removeAllListeners(type);
|
|
|
|
} else {
|
|
|
|
// Must remove internal arrow function handler paired with user handler
|
|
|
|
const arrowHandler = this.userHandlerArrowHandler.get(handler);
|
|
|
|
if (!!arrowHandler) {
|
|
|
|
this.ee.off(type, arrowHandler);
|
|
|
|
}
|
|
|
|
this.userHandlerArrowHandler.delete(handler);
|
|
|
|
}
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
2020-04-26 22:22:39 +02:00
|
|
|
}
|