2022-06-06 14:17:51 +00:00
|
|
|
import { IEmitUserInteraction } from '../../../containers/UIKit/interfaces';
|
2018-10-23 21:39:48 +00:00
|
|
|
import log from './log';
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
type TEventEmitterEmmitArgs =
|
|
|
|
| { rid: string }
|
2022-02-25 18:59:39 +00:00
|
|
|
| { server: string }
|
2022-01-12 12:54:04 +00:00
|
|
|
| { message: string }
|
|
|
|
| { method: string }
|
|
|
|
| { invalid: boolean }
|
|
|
|
| { force: boolean }
|
|
|
|
| { hasBiometry: boolean }
|
2022-08-19 21:14:37 +00:00
|
|
|
| { visible: boolean; onCancel?: null | Function }
|
2022-01-12 12:54:04 +00:00
|
|
|
| { cancel: () => void }
|
2022-03-16 19:07:49 +00:00
|
|
|
| { submit: (param: string) => void }
|
2024-02-20 20:17:46 +00:00
|
|
|
| IEmitUserInteraction;
|
2022-01-12 12:54:04 +00:00
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
class EventEmitter {
|
2022-01-12 12:54:04 +00:00
|
|
|
private events: { [key: string]: any };
|
|
|
|
|
2018-10-23 21:39:48 +00:00
|
|
|
constructor() {
|
|
|
|
this.events = {};
|
|
|
|
}
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
addEventListener(event: string, listener: Function) {
|
2018-10-23 21:39:48 +00:00
|
|
|
if (typeof this.events[event] !== 'object') {
|
|
|
|
this.events[event] = [];
|
|
|
|
}
|
|
|
|
this.events[event].push(listener);
|
|
|
|
return listener;
|
|
|
|
}
|
|
|
|
|
2022-01-12 12:54:04 +00:00
|
|
|
removeListener(event: string, listener: Function) {
|
2018-10-23 21:39:48 +00:00
|
|
|
if (typeof this.events[event] === 'object') {
|
|
|
|
const idx = this.events[event].indexOf(listener);
|
|
|
|
if (idx > -1) {
|
|
|
|
this.events[event].splice(idx, 1);
|
|
|
|
}
|
|
|
|
if (this.events[event].length === 0) {
|
|
|
|
delete this.events[event];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 17:49:09 +00:00
|
|
|
/**
|
|
|
|
* @deprecated use lib/methods/helpers/emitter.ts
|
|
|
|
*/
|
2022-01-12 12:54:04 +00:00
|
|
|
emit(event: string, ...args: TEventEmitterEmmitArgs[]) {
|
2018-10-23 21:39:48 +00:00
|
|
|
if (typeof this.events[event] === 'object') {
|
2022-01-12 12:54:04 +00:00
|
|
|
this.events[event].forEach((listener: Function) => {
|
2018-10-23 21:39:48 +00:00
|
|
|
try {
|
|
|
|
listener.apply(this, args);
|
|
|
|
} catch (e) {
|
2019-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const events = new EventEmitter();
|
|
|
|
export default events;
|