Use named types for improve readability
authorAlexander Ebert <ebert@woltlab.com>
Fri, 16 Oct 2020 15:02:52 +0000 (17:02 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Wed, 28 Oct 2020 11:28:41 +0000 (12:28 +0100)
wcfsetup/install/files/js/WoltLabSuite/Core/Event/Handler.js
wcfsetup/install/files/ts/WoltLabSuite/Core/Event/Handler.ts

index ad223ec054c79cd7d0d2f05471e2cd86f64ae829..87be2313de64a61c304b6be972c31469614e2a24 100644 (file)
@@ -98,9 +98,6 @@ define(["require", "exports", "../Core", "../Devtools"], function (require, expo
     /**
      * Removes all listeners registered for an identifier and ending with a special suffix.
      * This is commonly used to unbound event handlers for the editor.
-     *
-     * @param       {string}        identifier      event identifier
-     * @param       {string}        suffix          action suffix
      */
     function removeAllBySuffix(identifier, suffix) {
         const actions = _listeners.get(identifier);
index 4b21420f93f8284497ad69979b87f61e1ca83732..f958343a13f71d73a9179d51577069b2731d31a8 100644 (file)
 import * as Core from '../Core';
 import Devtools from '../Devtools';
 
-const _listeners = new Map<string, Map<string, Map<string, Callback>>>();
+type Identifier = string;
+type Action = string;
+type Uuid = string;
+const _listeners = new Map<Identifier, Map<Action, Map<Uuid, Callback>>>();
 
 /**
  * Registers an event listener.
  */
-export function add(identifier: string, action: string, callback: Callback): string {
+export function add(identifier: Identifier, action: Action, callback: Callback): Uuid {
   if (typeof callback !== 'function') {
     throw new TypeError(`Expected a valid callback for '${action}'@'${identifier}'.`);
   }
 
   let actions = _listeners.get(identifier);
   if (actions === undefined) {
-    actions = new Map<string, Map<string, Callback>>();
+    actions = new Map<Action, Map<Uuid, Callback>>();
     _listeners.set(identifier, actions);
   }
 
   let callbacks = actions.get(action);
   if (callbacks === undefined) {
-    callbacks = new Map<string, Callback>();
+    callbacks = new Map<Uuid, Callback>();
     actions.set(action, callbacks);
   }
 
@@ -42,7 +45,7 @@ export function add(identifier: string, action: string, callback: Callback): str
 /**
  * Fires an event and notifies all listeners.
  */
-export function fire(identifier: string, action: string, data?: object): void {
+export function fire(identifier: Identifier, action: Action, data?: object): void {
   Devtools._internal_.eventLog(identifier, action);
 
   data = data || {};
@@ -55,7 +58,7 @@ export function fire(identifier: string, action: string, data?: object): void {
 /**
  * Removes an event listener, requires the uuid returned by add().
  */
-export function remove(identifier: string, action: string, uuid: string): void {
+export function remove(identifier: Identifier, action: Action, uuid: Uuid): void {
   _listeners.get(identifier)
     ?.get(action)
     ?.delete(uuid);
@@ -65,7 +68,7 @@ export function remove(identifier: string, action: string, uuid: string): void {
  * Removes all event listeners for given action. Omitting the second parameter will
  * remove all listeners for this identifier.
  */
-export function removeAll(identifier: string, action?: string): void {
+export function removeAll(identifier: Identifier, action?: Action): void {
   if (typeof action !== 'string') action = undefined;
 
   const actions = _listeners.get(identifier);
@@ -83,11 +86,8 @@ export function removeAll(identifier: string, action?: string): void {
 /**
  * Removes all listeners registered for an identifier and ending with a special suffix.
  * This is commonly used to unbound event handlers for the editor.
- *
- * @param       {string}        identifier      event identifier
- * @param       {string}        suffix          action suffix
  */
-export function removeAllBySuffix(identifier: string, suffix: string): void {
+export function removeAllBySuffix(identifier: Identifier, suffix: string): void {
   const actions = _listeners.get(identifier);
   if (actions === undefined) {
     return;