Commit 15e38a13 authored by Alejandro Celaya's avatar Alejandro Celaya Committed by Alejandro Celaya

Migrate emitter module to TS

parent 21ae258e
/* eslint-disable @typescript-eslint/ban-types */
/*
* Disable @typescript-eslint/ban-types for the whole file, as changing the
* event's callback type away from `Function` has multiple implications that
* should be addressed separately
*/
import { TinyEmitter } from 'tiny-emitter'; import { TinyEmitter } from 'tiny-emitter';
/** @typedef {import('../../types/annotator').Destroyable} Destroyable */ import type { Destroyable } from '../../types/annotator';
/** /**
* Emitter is a communication class that implements the publisher/subscriber * Emitter is a communication class that implements the publisher/subscriber
* pattern. It allows sending and listening events through a shared EventBus. * pattern. It allows sending and listening events through a shared EventBus.
* The different elements of the application can communicate with each other * The different elements of the application can communicate with each other
* without being tightly coupled. * without being tightly coupled.
*
* @implements {Destroyable}
*/ */
export class Emitter { export class Emitter implements Destroyable {
/** private _emitter: TinyEmitter;
* @param {TinyEmitter} emitter private _subscriptions: [event: string, callback: Function][];
*/
constructor(emitter) {
this._emitter = emitter;
/** @type {[event: string, callback: Function][]} */ constructor(emitter: TinyEmitter) {
this._emitter = emitter;
this._subscriptions = []; this._subscriptions = [];
} }
/** /**
* Fire an event. * Fire an event.
*
* @param {string} event
* @param {unknown[]} args
*/ */
publish(event, ...args) { publish(event: string, ...args: unknown[]) {
this._emitter.emit(event, ...args); this._emitter.emit(event, ...args);
} }
/** /**
* Register an event listener. * Register an event listener.
*
* @param {string} event
* @param {Function} callback
*/ */
subscribe(event, callback) { subscribe(event: string, callback: Function) {
this._emitter.on(event, callback); this._emitter.on(event, callback);
this._subscriptions.push([event, callback]); this._subscriptions.push([event, callback]);
} }
/** /**
* Remove an event listener. * Remove an event listener.
*
* @param {string} event
* @param {Function} callback
*/ */
unsubscribe(event, callback) { unsubscribe(event: string, callback: Function) {
this._emitter.off(event, callback); this._emitter.off(event, callback);
this._subscriptions = this._subscriptions.filter( this._subscriptions = this._subscriptions.filter(
([subEvent, subCallback]) => ([subEvent, subCallback]) =>
...@@ -60,7 +54,7 @@ export class Emitter { ...@@ -60,7 +54,7 @@ export class Emitter {
* Remove all event listeners. * Remove all event listeners.
*/ */
destroy() { destroy() {
for (let [event, callback] of this._subscriptions) { for (const [event, callback] of this._subscriptions) {
this._emitter.off(event, callback); this._emitter.off(event, callback);
} }
this._subscriptions = []; this._subscriptions = [];
...@@ -68,6 +62,8 @@ export class Emitter { ...@@ -68,6 +62,8 @@ export class Emitter {
} }
export class EventBus { export class EventBus {
private _emitter: TinyEmitter;
constructor() { constructor() {
this._emitter = new TinyEmitter(); this._emitter = new TinyEmitter();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment