|
|
@@ -8,12 +8,17 @@
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
+ * A private class that mimics the {@link module:utils/emittermixin~EmitterMixin} and partially implements
|
|
|
+ * the {@link module:utils/emittermixin~Emitter} interface. Though, there are huge differences between both APIs
|
|
|
+ * as `SimpleEventEmitter` implements only the `on()`, `off()` and `_fire()` methods, and does not provide others.
|
|
|
+ * It also passes `null` as the first argument to event listeners instead of the `EventSource` instance.
|
|
|
+ *
|
|
|
* @private
|
|
|
*/
|
|
|
export default class SimpleEventEmitter {
|
|
|
constructor() {
|
|
|
/**
|
|
|
- * Simple event emitter listeners
|
|
|
+ * A dictionary of event emitter listeners.
|
|
|
*
|
|
|
* @private
|
|
|
* @type {Object.<String,Array.<Function>>}
|
|
|
@@ -23,12 +28,12 @@ export default class SimpleEventEmitter {
|
|
|
|
|
|
/**
|
|
|
* Starts listening to the specific event name by registering a callback that will be executed
|
|
|
- * when the event with the given name will be fired.
|
|
|
+ * whenever an event with given name fires.
|
|
|
*
|
|
|
* Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
|
|
|
*
|
|
|
- * @param {String} eventName
|
|
|
- * @param {Function} callback
|
|
|
+ * @param {String} eventName Event name.
|
|
|
+ * @param {Function} callback A callback which will be added to event listeners.
|
|
|
*/
|
|
|
on( eventName, callback ) {
|
|
|
if ( !this._listeners[ eventName ] ) {
|
|
|
@@ -39,31 +44,28 @@ export default class SimpleEventEmitter {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Stops listening to the specified event name by removing the callback.
|
|
|
+ * Stops listening to the specified event name by removing the callback from event listeners.
|
|
|
*
|
|
|
* Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
|
|
|
*
|
|
|
- * @param {String} eventName
|
|
|
- * @param {Function} callback
|
|
|
+ * @param {String} eventName Event name.
|
|
|
+ * @param {Function} callback A callback which will be removed from event listeners.
|
|
|
*/
|
|
|
off( eventName, callback ) {
|
|
|
- if ( !this._listeners[ eventName ] ) {
|
|
|
- return;
|
|
|
- }
|
|
|
-
|
|
|
this._listeners[ eventName ] = this._listeners[ eventName ]
|
|
|
.filter( cb => cb !== callback );
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Fires an event with the given event name and arguments.
|
|
|
+ * Fires an event with given event name and arguments.
|
|
|
*
|
|
|
* Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
|
|
|
*
|
|
|
- * @param {String} eventName
|
|
|
- * @param {...any} args
|
|
|
+ * @protected
|
|
|
+ * @param {String} eventName Event name.
|
|
|
+ * @param {...any} args Event arguments.
|
|
|
*/
|
|
|
- fire( eventName, ...args ) {
|
|
|
+ _fire( eventName, ...args ) {
|
|
|
const callbacks = this._listeners[ eventName ] || [];
|
|
|
|
|
|
for ( const callback of callbacks ) {
|