|
|
@@ -9,9 +9,6 @@
|
|
|
|
|
|
/* globals window */
|
|
|
|
|
|
-import mix from '@ckeditor/ckeditor5-utils/src/mix';
|
|
|
-import ObservableMixin from '@ckeditor/ckeditor5-utils/src/observablemixin';
|
|
|
-
|
|
|
/**
|
|
|
* An abstract watchdog class that handles most of the error handling process and the state of the underlying component.
|
|
|
*
|
|
|
@@ -42,7 +39,7 @@ export default class Watchdog {
|
|
|
this.crashes = [];
|
|
|
|
|
|
/**
|
|
|
- * Specifies the state of the watchdog item handled by the watchdog. The state can be one of the following values:
|
|
|
+ * Specifies the state of the item watched by the watchdog. The state can be one of the following values:
|
|
|
*
|
|
|
* * `initializing` - before the first initialization, and after crashes, before the item is ready,
|
|
|
* * `ready` - a state when a user can interact with the item,
|
|
|
@@ -52,10 +49,9 @@ export default class Watchdog {
|
|
|
* * `destroyed` - a state when the item is manually destroyed by the user after calling `watchdog.destroy()`
|
|
|
*
|
|
|
* @public
|
|
|
- * @observable
|
|
|
* @member {'initializing'|'ready'|'crashed'|'crashedPermanently'|'destroyed'} #state
|
|
|
*/
|
|
|
- this.set( 'state', 'initializing' );
|
|
|
+ this.state = 'initializing';
|
|
|
|
|
|
/**
|
|
|
* @protected
|
|
|
@@ -96,13 +92,6 @@ export default class Watchdog {
|
|
|
}
|
|
|
};
|
|
|
|
|
|
- if ( !this._restart ) {
|
|
|
- throw new Error(
|
|
|
- 'The Watchdog class was split into the abstract `Watchdog` class and the `EditorWatchdog` class. ' +
|
|
|
- 'Please, use `EditorWatchdog` if you have used the `Watchdog` class previously.'
|
|
|
- );
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* The creation method.
|
|
|
*
|
|
|
@@ -120,7 +109,7 @@ export default class Watchdog {
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
- * The handled watchdog item.
|
|
|
+ * The watched item.
|
|
|
*
|
|
|
* @abstract
|
|
|
* @protected
|
|
|
@@ -128,7 +117,7 @@ export default class Watchdog {
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
- * The method responsible for the watchdog item restarting.
|
|
|
+ * The method responsible for restarting the watched item.
|
|
|
*
|
|
|
* @abstract
|
|
|
* @protected
|
|
|
@@ -136,7 +125,7 @@ export default class Watchdog {
|
|
|
*/
|
|
|
|
|
|
/**
|
|
|
- * Traverses the error context and the handled watchdog item to find out whether the error should
|
|
|
+ * Traverses the error context and the watched item to find out whether the error should
|
|
|
* be handled by the given item.
|
|
|
*
|
|
|
* @abstract
|
|
|
@@ -144,10 +133,25 @@ export default class Watchdog {
|
|
|
* @method #_isErrorComingFromThisItem
|
|
|
* @param {module:utils/ckeditorerror~CKEditorError} error
|
|
|
*/
|
|
|
+
|
|
|
+ /**
|
|
|
+ * A dictionary of event emitter listeners.
|
|
|
+ *
|
|
|
+ * @private
|
|
|
+ * @type {Object.<String,Array.<Function>>}
|
|
|
+ */
|
|
|
+ this._listeners = {};
|
|
|
+
|
|
|
+ if ( !this._restart ) {
|
|
|
+ throw new Error(
|
|
|
+ 'The Watchdog class was split into the abstract `Watchdog` class and the `EditorWatchdog` class. ' +
|
|
|
+ 'Please, use `EditorWatchdog` if you have used the `Watchdog` class previously.'
|
|
|
+ );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the function that is responsible for the watchdog item creation.
|
|
|
+ * Sets the function that is responsible for creating watchded items.
|
|
|
*
|
|
|
* @param {Function} creator A callback responsible for creating an item. Returns a promise
|
|
|
* that is resolved when the item is created.
|
|
|
@@ -157,7 +161,7 @@ export default class Watchdog {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Sets the function that is responsible for the watchdog item destruction.
|
|
|
+ * Sets the function that is responsible for destructing watched items.
|
|
|
*
|
|
|
* @param {Function} destructor A callback that takes the item and returns the promise
|
|
|
* to the destroying process.
|
|
|
@@ -167,11 +171,59 @@ export default class Watchdog {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Destroys the watchdog and release the resources.
|
|
|
+ * Destroys the watchdog and releases the resources.
|
|
|
*/
|
|
|
destroy() {
|
|
|
this._stopErrorHandling();
|
|
|
- this.stopListening();
|
|
|
+
|
|
|
+ this._listeners = {};
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Starts listening to the specific event name by registering a callback that will be executed
|
|
|
+ * whenever an event with given name fires.
|
|
|
+ *
|
|
|
+ * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
|
|
|
+ *
|
|
|
+ * @param {String} eventName Event name.
|
|
|
+ * @param {Function} callback A callback which will be added to event listeners.
|
|
|
+ */
|
|
|
+ on( eventName, callback ) {
|
|
|
+ if ( !this._listeners[ eventName ] ) {
|
|
|
+ this._listeners[ eventName ] = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ this._listeners[ eventName ].push( 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 Event name.
|
|
|
+ * @param {Function} callback A callback which will be removed from event listeners.
|
|
|
+ */
|
|
|
+ off( eventName, callback ) {
|
|
|
+ this._listeners[ eventName ] = this._listeners[ eventName ]
|
|
|
+ .filter( cb => cb !== callback );
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Fires an event with given event name and arguments.
|
|
|
+ *
|
|
|
+ * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
|
|
|
+ *
|
|
|
+ * @protected
|
|
|
+ * @param {String} eventName Event name.
|
|
|
+ * @param {...any} args Event arguments.
|
|
|
+ */
|
|
|
+ _fire( eventName, ...args ) {
|
|
|
+ const callbacks = this._listeners[ eventName ] || [];
|
|
|
+
|
|
|
+ for ( const callback of callbacks ) {
|
|
|
+ callback.apply( this, [ null, ...args ] );
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
@@ -195,7 +247,7 @@ export default class Watchdog {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Checks if the error comes from the watchdog item and restarts it.
|
|
|
+ * Checks if the error comes from the watched item and restarts it.
|
|
|
* It reacts to {@link module:utils/ckeditorerror~CKEditorError `CKEditorError` errors} only.
|
|
|
*
|
|
|
* @private
|
|
|
@@ -223,12 +275,14 @@ export default class Watchdog {
|
|
|
const causesRestart = this._shouldRestart();
|
|
|
|
|
|
this.state = 'crashed';
|
|
|
- this.fire( 'error', { error, causesRestart } );
|
|
|
+ this._fire( 'stateChange' );
|
|
|
+ this._fire( 'error', { error, causesRestart } );
|
|
|
|
|
|
if ( causesRestart ) {
|
|
|
this._restart();
|
|
|
} else {
|
|
|
this.state = 'crashedPermanently';
|
|
|
+ this._fire( 'stateChange' );
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -245,7 +299,7 @@ export default class Watchdog {
|
|
|
error.is( 'CKEditorError' ) &&
|
|
|
error.context !== undefined &&
|
|
|
|
|
|
- // In some cases the watchdog item should not be restarted - e.g. during the item initialization.
|
|
|
+ // In some cases the watched item should not be restarted - e.g. during the item initialization.
|
|
|
// That's why the `null` was introduced as a correct error context which does cause restarting.
|
|
|
error.context !== null &&
|
|
|
|
|
|
@@ -284,19 +338,17 @@ export default class Watchdog {
|
|
|
*/
|
|
|
}
|
|
|
|
|
|
-mix( Watchdog, ObservableMixin );
|
|
|
-
|
|
|
/**
|
|
|
* The watchdog plugin configuration.
|
|
|
*
|
|
|
* @typedef {Object} WatchdogConfig
|
|
|
*
|
|
|
- * @property {Number} [crashNumberLimit=3] A threshold specifying the number of watchdog item crashes
|
|
|
+ * @property {Number} [crashNumberLimit=3] A threshold specifying the number of watched item crashes
|
|
|
* when the watchdog stops restarting the item in case of errors.
|
|
|
* After this limit is reached and the time between last errors is shorter than `minimumNonErrorTimePeriod`
|
|
|
* the watchdog changes its state to `crashedPermanently` and it stops restarting the item. This prevents an infinite restart loop.
|
|
|
*
|
|
|
- * @property {Number} [minimumNonErrorTimePeriod=5000] An average amount of milliseconds between last watchdog item errors
|
|
|
+ * @property {Number} [minimumNonErrorTimePeriod=5000] An average amount of milliseconds between last watched item errors
|
|
|
* (defaults to 5000). When the period of time between errors is lower than that and the `crashNumberLimit` is also reached
|
|
|
* the watchdog changes its state to `crashedPermanently` and it stops restarting the item. This prevents an infinite restart loop.
|
|
|
*
|