From 82776dcbd5634d67de9b9ab8bd10649c69c3f877 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 16 Oct 2020 16:08:30 +0200 Subject: [PATCH] Convert `Dom/Change/Listener` to TypeScript --- .../WoltLabSuite/Core/Dom/Change/Listener.js | 87 +++++++++---------- .../ts/WoltLabSuite/Core/CallbackList.ts | 2 +- .../WoltLabSuite/Core/Dom/Change/Listener.ts | 44 ++++++++++ 3 files changed, 86 insertions(+), 47 deletions(-) create mode 100644 wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Change/Listener.ts diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Change/Listener.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Change/Listener.js index ef87db6ae6..15771affc8 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Change/Listener.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Dom/Change/Listener.js @@ -1,51 +1,46 @@ /** * Allows to be informed when the DOM may have changed and * new elements that are relevant to you may have been added. - * - * @author Tim Duesterhus - * @copyright 2001-2019 WoltLab GmbH - * @license GNU Lesser General Public License - * @module Dom/ChangeListener (alias) - * @module WoltLabSuite/Core/Dom/Change/Listener + * + * @author Tim Duesterhus + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @module Dom/ChangeListener (alias) + * @module WoltLabSuite/Core/Dom/Change/Listener */ -define(['CallbackList'], function(CallbackList) { - "use strict"; - - var _callbackList = new CallbackList(); - var _hot = false; - - /** - * @exports WoltLabSuite/Core/Dom/Change/Listener - */ - return { - /** - * @see WoltLabSuite/Core/CallbackList#add - */ - add: _callbackList.add.bind(_callbackList), - - /** - * @see WoltLabSuite/Core/CallbackList#remove - */ - remove: _callbackList.remove.bind(_callbackList), - - /** - * Triggers the execution of all the listeners. - * Use this function when you added new elements to the DOM that might - * be relevant to others. - * While this function is in progress further calls to it will be ignored. - */ - trigger: function() { - if (_hot) return; - - try { - _hot = true; - _callbackList.forEach(null, function(callback) { - callback(); - }); - } - finally { - _hot = false; - } - } - }; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +define(["require", "exports", "../../CallbackList"], function (require, exports, CallbackList_1) { + "use strict"; + CallbackList_1 = __importDefault(CallbackList_1); + const _callbackList = new CallbackList_1.default(); + let _hot = false; + return { + /** + * @see CallbackList.add + */ + add: _callbackList.add.bind(_callbackList), + /** + * @see CallbackList.remove + */ + remove: _callbackList.remove.bind(_callbackList), + /** + * Triggers the execution of all the listeners. + * Use this function when you added new elements to the DOM that might + * be relevant to others. + * While this function is in progress further calls to it will be ignored. + */ + trigger() { + if (_hot) + return; + try { + _hot = true; + _callbackList.forEach(null, callback => callback()); + } + finally { + _hot = false; + } + }, + }; }); diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/CallbackList.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/CallbackList.ts index 94a43a785c..d888ecd374 100644 --- a/wcfsetup/install/files/ts/WoltLabSuite/Core/CallbackList.ts +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/CallbackList.ts @@ -47,6 +47,6 @@ class CallbackList { } } -type Callback = () => void; +type Callback = (...args: any[]) => void; export = CallbackList; diff --git a/wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Change/Listener.ts b/wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Change/Listener.ts new file mode 100644 index 0000000000..230fbb4216 --- /dev/null +++ b/wcfsetup/install/files/ts/WoltLabSuite/Core/Dom/Change/Listener.ts @@ -0,0 +1,44 @@ +/** + * Allows to be informed when the DOM may have changed and + * new elements that are relevant to you may have been added. + * + * @author Tim Duesterhus + * @copyright 2001-2019 WoltLab GmbH + * @license GNU Lesser General Public License + * @module Dom/ChangeListener (alias) + * @module WoltLabSuite/Core/Dom/Change/Listener + */ + +import CallbackList from '../../CallbackList'; + +const _callbackList = new CallbackList(); +let _hot = false; + +export = { + /** + * @see CallbackList.add + */ + add: _callbackList.add.bind(_callbackList), + + /** + * @see CallbackList.remove + */ + remove: _callbackList.remove.bind(_callbackList), + + /** + * Triggers the execution of all the listeners. + * Use this function when you added new elements to the DOM that might + * be relevant to others. + * While this function is in progress further calls to it will be ignored. + */ + trigger(): void { + if (_hot) return; + + try { + _hot = true; + _callbackList.forEach(null, callback => callback()); + } finally { + _hot = false; + } + }, +} -- 2.20.1