@ckeditor/ckeditor5-watchdog 41.3.0 → 41.4.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index-content.css +4 -0
- package/dist/index-editor.css +4 -0
- package/dist/index.css +4 -0
- package/dist/index.js +1031 -0
- package/dist/index.js.map +1 -0
- package/dist/types/augmentation.d.ts +19 -0
- package/dist/types/contextwatchdog.d.ts +337 -0
- package/dist/types/editorwatchdog.d.ts +195 -0
- package/dist/types/index.d.ts +15 -0
- package/dist/types/utils/areconnectedthroughproperties.d.ts +12 -0
- package/dist/types/utils/getsubnodes.d.ts +12 -0
- package/dist/types/watchdog.d.ts +233 -0
- package/package.json +2 -1
package/dist/index.js
ADDED
|
@@ -0,0 +1,1031 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
import { isElement, cloneDeepWith, throttle } from 'lodash-es';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
9
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
10
|
+
*/ /**
|
|
11
|
+
* An abstract watchdog class that handles most of the error handling process and the state of the underlying component.
|
|
12
|
+
*
|
|
13
|
+
* See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and how to use it.
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/ class Watchdog {
|
|
17
|
+
/**
|
|
18
|
+
* Destroys the watchdog and releases the resources.
|
|
19
|
+
*/ destroy() {
|
|
20
|
+
this._stopErrorHandling();
|
|
21
|
+
this._listeners = {};
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Starts listening to a specific event name by registering a callback that will be executed
|
|
25
|
+
* whenever an event with a given name fires.
|
|
26
|
+
*
|
|
27
|
+
* Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
|
|
28
|
+
*
|
|
29
|
+
* @param eventName The event name.
|
|
30
|
+
* @param callback A callback which will be added to event listeners.
|
|
31
|
+
*/ on(eventName, callback) {
|
|
32
|
+
if (!this._listeners[eventName]) {
|
|
33
|
+
this._listeners[eventName] = [];
|
|
34
|
+
}
|
|
35
|
+
this._listeners[eventName].push(callback);
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Stops listening to the specified event name by removing the callback from event listeners.
|
|
39
|
+
*
|
|
40
|
+
* Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
|
|
41
|
+
*
|
|
42
|
+
* @param eventName The event name.
|
|
43
|
+
* @param callback A callback which will be removed from event listeners.
|
|
44
|
+
*/ off(eventName, callback) {
|
|
45
|
+
this._listeners[eventName] = this._listeners[eventName].filter((cb)=>cb !== callback);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Fires an event with a given event name and arguments.
|
|
49
|
+
*
|
|
50
|
+
* Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
|
|
51
|
+
*/ _fire(eventName, ...args) {
|
|
52
|
+
const callbacks = this._listeners[eventName] || [];
|
|
53
|
+
for (const callback of callbacks){
|
|
54
|
+
callback.apply(this, [
|
|
55
|
+
null,
|
|
56
|
+
...args
|
|
57
|
+
]);
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Starts error handling by attaching global error handlers.
|
|
62
|
+
*/ _startErrorHandling() {
|
|
63
|
+
window.addEventListener('error', this._boundErrorHandler);
|
|
64
|
+
window.addEventListener('unhandledrejection', this._boundErrorHandler);
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Stops error handling by detaching global error handlers.
|
|
68
|
+
*/ _stopErrorHandling() {
|
|
69
|
+
window.removeEventListener('error', this._boundErrorHandler);
|
|
70
|
+
window.removeEventListener('unhandledrejection', this._boundErrorHandler);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Checks if an error comes from the watched item and restarts it.
|
|
74
|
+
* It reacts to {@link module:utils/ckeditorerror~CKEditorError `CKEditorError` errors} only.
|
|
75
|
+
*
|
|
76
|
+
* @fires error
|
|
77
|
+
* @param error Error.
|
|
78
|
+
* @param evt An error event.
|
|
79
|
+
*/ _handleError(error, evt) {
|
|
80
|
+
// @if CK_DEBUG // const err = error as CKEditorError;
|
|
81
|
+
// @if CK_DEBUG // if ( err.is && err.is( 'CKEditorError' ) && err.context === undefined ) {
|
|
82
|
+
// @if CK_DEBUG // console.warn( 'The error is missing its context and Watchdog cannot restart the proper item.' );
|
|
83
|
+
// @if CK_DEBUG // }
|
|
84
|
+
if (this._shouldReactToError(error)) {
|
|
85
|
+
this.crashes.push({
|
|
86
|
+
message: error.message,
|
|
87
|
+
stack: error.stack,
|
|
88
|
+
// `evt.filename`, `evt.lineno` and `evt.colno` are available only in ErrorEvent events
|
|
89
|
+
filename: evt instanceof ErrorEvent ? evt.filename : undefined,
|
|
90
|
+
lineno: evt instanceof ErrorEvent ? evt.lineno : undefined,
|
|
91
|
+
colno: evt instanceof ErrorEvent ? evt.colno : undefined,
|
|
92
|
+
date: this._now()
|
|
93
|
+
});
|
|
94
|
+
const causesRestart = this._shouldRestart();
|
|
95
|
+
this.state = 'crashed';
|
|
96
|
+
this._fire('stateChange');
|
|
97
|
+
this._fire('error', {
|
|
98
|
+
error,
|
|
99
|
+
causesRestart
|
|
100
|
+
});
|
|
101
|
+
if (causesRestart) {
|
|
102
|
+
this._restart();
|
|
103
|
+
} else {
|
|
104
|
+
this.state = 'crashedPermanently';
|
|
105
|
+
this._fire('stateChange');
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Checks whether an error should be handled by the watchdog.
|
|
111
|
+
*
|
|
112
|
+
* @param error An error that was caught by the error handling process.
|
|
113
|
+
*/ _shouldReactToError(error) {
|
|
114
|
+
return error.is && error.is('CKEditorError') && error.context !== undefined && // In some cases the watched item should not be restarted - e.g. during the item initialization.
|
|
115
|
+
// That's why the `null` was introduced as a correct error context which does cause restarting.
|
|
116
|
+
error.context !== null && // Do not react to errors if the watchdog is in states other than `ready`.
|
|
117
|
+
this.state === 'ready' && this._isErrorComingFromThisItem(error);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Checks if the watchdog should restart the underlying item.
|
|
121
|
+
*/ _shouldRestart() {
|
|
122
|
+
if (this.crashes.length <= this._crashNumberLimit) {
|
|
123
|
+
return true;
|
|
124
|
+
}
|
|
125
|
+
const lastErrorTime = this.crashes[this.crashes.length - 1].date;
|
|
126
|
+
const firstMeaningfulErrorTime = this.crashes[this.crashes.length - 1 - this._crashNumberLimit].date;
|
|
127
|
+
const averageNonErrorTimePeriod = (lastErrorTime - firstMeaningfulErrorTime) / this._crashNumberLimit;
|
|
128
|
+
return averageNonErrorTimePeriod > this._minimumNonErrorTimePeriod;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* @param {module:watchdog/watchdog~WatchdogConfig} config The watchdog plugin configuration.
|
|
132
|
+
*/ constructor(config){
|
|
133
|
+
/**
|
|
134
|
+
* An array of crashes saved as an object with the following properties:
|
|
135
|
+
*
|
|
136
|
+
* * `message`: `String`,
|
|
137
|
+
* * `stack`: `String`,
|
|
138
|
+
* * `date`: `Number`,
|
|
139
|
+
* * `filename`: `String | undefined`,
|
|
140
|
+
* * `lineno`: `Number | undefined`,
|
|
141
|
+
* * `colno`: `Number | undefined`,
|
|
142
|
+
*/ this.crashes = [];
|
|
143
|
+
/**
|
|
144
|
+
* Specifies the state of the item watched by the watchdog. The state can be one of the following values:
|
|
145
|
+
*
|
|
146
|
+
* * `initializing` – Before the first initialization, and after crashes, before the item is ready.
|
|
147
|
+
* * `ready` – A state when the user can interact with the item.
|
|
148
|
+
* * `crashed` – A state when an error occurs. It quickly changes to `initializing` or `crashedPermanently`
|
|
149
|
+
* depending on how many and how frequent errors have been caught recently.
|
|
150
|
+
* * `crashedPermanently` – A state when the watchdog stops reacting to errors and keeps the item it is watching crashed,
|
|
151
|
+
* * `destroyed` – A state when the item is manually destroyed by the user after calling `watchdog.destroy()`.
|
|
152
|
+
*/ this.state = 'initializing';
|
|
153
|
+
/**
|
|
154
|
+
* Returns the result of the `Date.now()` call. It can be overridden in tests to mock time as some popular
|
|
155
|
+
* approaches like `sinon.useFakeTimers()` do not work well with error handling.
|
|
156
|
+
*/ this._now = Date.now;
|
|
157
|
+
this.crashes = [];
|
|
158
|
+
this._crashNumberLimit = typeof config.crashNumberLimit === 'number' ? config.crashNumberLimit : 3;
|
|
159
|
+
this._minimumNonErrorTimePeriod = typeof config.minimumNonErrorTimePeriod === 'number' ? config.minimumNonErrorTimePeriod : 5000;
|
|
160
|
+
this._boundErrorHandler = (evt)=>{
|
|
161
|
+
// `evt.error` is exposed by EventError while `evt.reason` is available in PromiseRejectionEvent.
|
|
162
|
+
const error = 'error' in evt ? evt.error : evt.reason;
|
|
163
|
+
// Note that `evt.reason` might be everything that is in the promise rejection.
|
|
164
|
+
// Similarly everything that is thrown lands in `evt.error`.
|
|
165
|
+
if (error instanceof Error) {
|
|
166
|
+
this._handleError(error, evt);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
this._listeners = {};
|
|
170
|
+
if (!this._restart) {
|
|
171
|
+
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.');
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
|
|
178
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
179
|
+
*/ /**
|
|
180
|
+
* @module watchdog/utils/getsubnodes
|
|
181
|
+
*/ /* globals EventTarget, Event */ function getSubNodes(head, excludedProperties = new Set()) {
|
|
182
|
+
const nodes = [
|
|
183
|
+
head
|
|
184
|
+
];
|
|
185
|
+
// @if CK_DEBUG_WATCHDOG // const prevNodeMap = new Map();
|
|
186
|
+
// Nodes are stored to prevent infinite looping.
|
|
187
|
+
const subNodes = new Set();
|
|
188
|
+
let nodeIndex = 0;
|
|
189
|
+
while(nodes.length > nodeIndex){
|
|
190
|
+
// Incrementing the iterator is much faster than changing size of the array with Array.prototype.shift().
|
|
191
|
+
const node = nodes[nodeIndex++];
|
|
192
|
+
if (subNodes.has(node) || !shouldNodeBeIncluded(node) || excludedProperties.has(node)) {
|
|
193
|
+
continue;
|
|
194
|
+
}
|
|
195
|
+
subNodes.add(node);
|
|
196
|
+
// Handle arrays, maps, sets, custom collections that implements `[ Symbol.iterator ]()`, etc.
|
|
197
|
+
if (Symbol.iterator in node) {
|
|
198
|
+
// The custom editor iterators might cause some problems if the editor is crashed.
|
|
199
|
+
try {
|
|
200
|
+
for (const n of node){
|
|
201
|
+
nodes.push(n);
|
|
202
|
+
// @if CK_DEBUG_WATCHDOG // if ( !prevNodeMap.has( n ) ) {
|
|
203
|
+
// @if CK_DEBUG_WATCHDOG // prevNodeMap.set( n, node );
|
|
204
|
+
// @if CK_DEBUG_WATCHDOG // }
|
|
205
|
+
}
|
|
206
|
+
} catch (err) {
|
|
207
|
+
// Do not log errors for broken structures
|
|
208
|
+
// since we are in the error handling process already.
|
|
209
|
+
// eslint-disable-line no-empty
|
|
210
|
+
}
|
|
211
|
+
} else {
|
|
212
|
+
for(const key in node){
|
|
213
|
+
// We share a reference via the protobuf library within the editors,
|
|
214
|
+
// hence the shared value should be skipped. Although, it's not a perfect
|
|
215
|
+
// solution since new places like that might occur in the future.
|
|
216
|
+
if (key === 'defaultValue') {
|
|
217
|
+
continue;
|
|
218
|
+
}
|
|
219
|
+
nodes.push(node[key]);
|
|
220
|
+
// @if CK_DEBUG_WATCHDOG // if ( !prevNodeMap.has( node[ key ] ) ) {
|
|
221
|
+
// @if CK_DEBUG_WATCHDOG // prevNodeMap.set( node[ key ], node );
|
|
222
|
+
// @if CK_DEBUG_WATCHDOG // }
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
// @if CK_DEBUG_WATCHDOG // return { subNodes, prevNodeMap } as any;
|
|
227
|
+
return subNodes;
|
|
228
|
+
}
|
|
229
|
+
function shouldNodeBeIncluded(node) {
|
|
230
|
+
const type = Object.prototype.toString.call(node);
|
|
231
|
+
const typeOfNode = typeof node;
|
|
232
|
+
return !(typeOfNode === 'number' || typeOfNode === 'boolean' || typeOfNode === 'string' || typeOfNode === 'symbol' || typeOfNode === 'function' || type === '[object Date]' || type === '[object RegExp]' || type === '[object Module]' || node === undefined || node === null || // This flag is meant to exclude singletons shared across editor instances. So when an error is thrown in one editor,
|
|
233
|
+
// the other editors connected through the reference to the same singleton are not restarted. This is a temporary workaround
|
|
234
|
+
// until a better solution is found.
|
|
235
|
+
// More in https://github.com/ckeditor/ckeditor5/issues/12292.
|
|
236
|
+
node._watchdogExcluded || // Skip native DOM objects, e.g. Window, nodes, events, etc.
|
|
237
|
+
node instanceof EventTarget || node instanceof Event);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Traverses both structures to find out whether there is a reference that is shared between both structures.
|
|
242
|
+
*/ function areConnectedThroughProperties(target1, target2, excludedNodes = new Set()) {
|
|
243
|
+
if (target1 === target2 && isObject(target1)) {
|
|
244
|
+
return true;
|
|
245
|
+
}
|
|
246
|
+
// @if CK_DEBUG_WATCHDOG // return checkConnectionBetweenProps( target1, target2, excludedNodes );
|
|
247
|
+
const subNodes1 = getSubNodes(target1, excludedNodes);
|
|
248
|
+
const subNodes2 = getSubNodes(target2, excludedNodes);
|
|
249
|
+
for (const node of subNodes1){
|
|
250
|
+
if (subNodes2.has(node)) {
|
|
251
|
+
return true;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
return false;
|
|
255
|
+
}
|
|
256
|
+
function isObject(structure) {
|
|
257
|
+
return typeof structure === 'object' && structure !== null;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
class EditorWatchdog extends Watchdog {
|
|
261
|
+
/**
|
|
262
|
+
* The current editor instance.
|
|
263
|
+
*/ get editor() {
|
|
264
|
+
return this._editor;
|
|
265
|
+
}
|
|
266
|
+
/**
|
|
267
|
+
* @internal
|
|
268
|
+
*/ get _item() {
|
|
269
|
+
return this._editor;
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Sets the function that is responsible for the editor creation.
|
|
273
|
+
* It expects a function that should return a promise.
|
|
274
|
+
*
|
|
275
|
+
* ```ts
|
|
276
|
+
* watchdog.setCreator( ( element, config ) => ClassicEditor.create( element, config ) );
|
|
277
|
+
* ```
|
|
278
|
+
*/ setCreator(creator) {
|
|
279
|
+
this._creator = creator;
|
|
280
|
+
}
|
|
281
|
+
/**
|
|
282
|
+
* Sets the function that is responsible for the editor destruction.
|
|
283
|
+
* Overrides the default destruction function, which destroys only the editor instance.
|
|
284
|
+
* It expects a function that should return a promise or `undefined`.
|
|
285
|
+
*
|
|
286
|
+
* ```ts
|
|
287
|
+
* watchdog.setDestructor( editor => {
|
|
288
|
+
* // Do something before the editor is destroyed.
|
|
289
|
+
*
|
|
290
|
+
* return editor
|
|
291
|
+
* .destroy()
|
|
292
|
+
* .then( () => {
|
|
293
|
+
* // Do something after the editor is destroyed.
|
|
294
|
+
* } );
|
|
295
|
+
* } );
|
|
296
|
+
* ```
|
|
297
|
+
*/ setDestructor(destructor) {
|
|
298
|
+
this._destructor = destructor;
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* Restarts the editor instance. This method is called whenever an editor error occurs. It fires the `restart` event and changes
|
|
302
|
+
* the state to `initializing`.
|
|
303
|
+
*
|
|
304
|
+
* @fires restart
|
|
305
|
+
*/ _restart() {
|
|
306
|
+
return Promise.resolve().then(()=>{
|
|
307
|
+
this.state = 'initializing';
|
|
308
|
+
this._fire('stateChange');
|
|
309
|
+
return this._destroy();
|
|
310
|
+
}).catch((err)=>{
|
|
311
|
+
console.error('An error happened during the editor destroying.', err);
|
|
312
|
+
}).then(()=>{
|
|
313
|
+
// Pre-process some data from the original editor config.
|
|
314
|
+
// Our goal here is to make sure that the restarted editor will be reinitialized with correct set of roots.
|
|
315
|
+
// We are not interested in any data set in config or in `.create()` first parameter. It will be replaced anyway.
|
|
316
|
+
// But we need to set them correctly to make sure that proper roots are created.
|
|
317
|
+
//
|
|
318
|
+
// Since a different set of roots will be created, `lazyRoots` and `rootsAttributes` properties must be managed too.
|
|
319
|
+
// Keys are root names, values are ''. Used when the editor was initialized by setting the first parameter to document data.
|
|
320
|
+
const existingRoots = {};
|
|
321
|
+
// Keeps lazy roots. They may be different when compared to initial config if some of the roots were loaded.
|
|
322
|
+
const lazyRoots = [];
|
|
323
|
+
// Roots attributes from the old config. Will be referred when setting new attributes.
|
|
324
|
+
const oldRootsAttributes = this._config.rootsAttributes || {};
|
|
325
|
+
// New attributes to be set. Is filled only for roots that still exist in the document.
|
|
326
|
+
const rootsAttributes = {};
|
|
327
|
+
// Traverse through the roots saved when the editor crashed and set up the discussed values.
|
|
328
|
+
for (const [rootName, rootData] of Object.entries(this._data.roots)){
|
|
329
|
+
if (rootData.isLoaded) {
|
|
330
|
+
existingRoots[rootName] = '';
|
|
331
|
+
rootsAttributes[rootName] = oldRootsAttributes[rootName] || {};
|
|
332
|
+
} else {
|
|
333
|
+
lazyRoots.push(rootName);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
const updatedConfig = {
|
|
337
|
+
...this._config,
|
|
338
|
+
extraPlugins: this._config.extraPlugins || [],
|
|
339
|
+
lazyRoots,
|
|
340
|
+
rootsAttributes,
|
|
341
|
+
_watchdogInitialData: this._data
|
|
342
|
+
};
|
|
343
|
+
// Delete `initialData` as it is not needed. Data will be set by the watchdog based on `_watchdogInitialData`.
|
|
344
|
+
// First parameter of the editor `.create()` will be used to set up initial roots.
|
|
345
|
+
delete updatedConfig.initialData;
|
|
346
|
+
updatedConfig.extraPlugins.push(EditorWatchdogInitPlugin);
|
|
347
|
+
if (this._initUsingData) {
|
|
348
|
+
return this.create(existingRoots, updatedConfig, updatedConfig.context);
|
|
349
|
+
} else {
|
|
350
|
+
// Set correct editables to make sure that proper roots are created and linked with DOM elements.
|
|
351
|
+
// No need to set initial data, as it would be discarded anyway.
|
|
352
|
+
//
|
|
353
|
+
// If one element was initially set in `elementOrData`, then use that original element to restart the editor.
|
|
354
|
+
// This is for compatibility purposes with single-root editor types.
|
|
355
|
+
if (isElement(this._elementOrData)) {
|
|
356
|
+
return this.create(this._elementOrData, updatedConfig, updatedConfig.context);
|
|
357
|
+
} else {
|
|
358
|
+
return this.create(this._editables, updatedConfig, updatedConfig.context);
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}).then(()=>{
|
|
362
|
+
this._fire('restart');
|
|
363
|
+
});
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Creates the editor instance and keeps it running, using the defined creator and destructor.
|
|
367
|
+
*
|
|
368
|
+
* @param elementOrData The editor source element or the editor data.
|
|
369
|
+
* @param config The editor configuration.
|
|
370
|
+
* @param context A context for the editor.
|
|
371
|
+
*/ create(elementOrData = this._elementOrData, config = this._config, context) {
|
|
372
|
+
this._lifecyclePromise = Promise.resolve(this._lifecyclePromise).then(()=>{
|
|
373
|
+
super._startErrorHandling();
|
|
374
|
+
this._elementOrData = elementOrData;
|
|
375
|
+
// Use document data in the first parameter of the editor `.create()` call only if it was used like this originally.
|
|
376
|
+
// Use document data if a string or object with strings was passed.
|
|
377
|
+
this._initUsingData = typeof elementOrData == 'string' || Object.keys(elementOrData).length > 0 && typeof Object.values(elementOrData)[0] == 'string';
|
|
378
|
+
// Clone configuration because it might be shared within multiple watchdog instances. Otherwise,
|
|
379
|
+
// when an error occurs in one of these editors, the watchdog will restart all of them.
|
|
380
|
+
this._config = this._cloneEditorConfiguration(config) || {};
|
|
381
|
+
this._config.context = context;
|
|
382
|
+
return this._creator(elementOrData, this._config);
|
|
383
|
+
}).then((editor)=>{
|
|
384
|
+
this._editor = editor;
|
|
385
|
+
editor.model.document.on('change:data', this._throttledSave);
|
|
386
|
+
this._lastDocumentVersion = editor.model.document.version;
|
|
387
|
+
this._data = this._getData();
|
|
388
|
+
if (!this._initUsingData) {
|
|
389
|
+
this._editables = this._getEditables();
|
|
390
|
+
}
|
|
391
|
+
this.state = 'ready';
|
|
392
|
+
this._fire('stateChange');
|
|
393
|
+
}).finally(()=>{
|
|
394
|
+
this._lifecyclePromise = null;
|
|
395
|
+
});
|
|
396
|
+
return this._lifecyclePromise;
|
|
397
|
+
}
|
|
398
|
+
/**
|
|
399
|
+
* Destroys the watchdog and the current editor instance. It fires the callback
|
|
400
|
+
* registered in {@link #setDestructor `setDestructor()`} and uses it to destroy the editor instance.
|
|
401
|
+
* It also sets the state to `destroyed`.
|
|
402
|
+
*/ destroy() {
|
|
403
|
+
this._lifecyclePromise = Promise.resolve(this._lifecyclePromise).then(()=>{
|
|
404
|
+
this.state = 'destroyed';
|
|
405
|
+
this._fire('stateChange');
|
|
406
|
+
super.destroy();
|
|
407
|
+
return this._destroy();
|
|
408
|
+
}).finally(()=>{
|
|
409
|
+
this._lifecyclePromise = null;
|
|
410
|
+
});
|
|
411
|
+
return this._lifecyclePromise;
|
|
412
|
+
}
|
|
413
|
+
_destroy() {
|
|
414
|
+
return Promise.resolve().then(()=>{
|
|
415
|
+
this._stopErrorHandling();
|
|
416
|
+
this._throttledSave.cancel();
|
|
417
|
+
const editor = this._editor;
|
|
418
|
+
this._editor = null;
|
|
419
|
+
// Remove the `change:data` listener before destroying the editor.
|
|
420
|
+
// Incorrectly written plugins may trigger firing `change:data` events during the editor destruction phase
|
|
421
|
+
// causing the watchdog to call `editor.getData()` when some parts of editor are already destroyed.
|
|
422
|
+
editor.model.document.off('change:data', this._throttledSave);
|
|
423
|
+
return this._destructor(editor);
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
/**
|
|
427
|
+
* Saves the editor data, so it can be restored after the crash even if the data cannot be fetched at
|
|
428
|
+
* the moment of the crash.
|
|
429
|
+
*/ _save() {
|
|
430
|
+
const version = this._editor.model.document.version;
|
|
431
|
+
try {
|
|
432
|
+
this._data = this._getData();
|
|
433
|
+
if (!this._initUsingData) {
|
|
434
|
+
this._editables = this._getEditables();
|
|
435
|
+
}
|
|
436
|
+
this._lastDocumentVersion = version;
|
|
437
|
+
} catch (err) {
|
|
438
|
+
console.error(err, 'An error happened during restoring editor data. ' + 'Editor will be restored from the previously saved data.');
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* @internal
|
|
443
|
+
*/ _setExcludedProperties(props) {
|
|
444
|
+
this._excludedProps = props;
|
|
445
|
+
}
|
|
446
|
+
/**
|
|
447
|
+
* Gets all data that is required to reinitialize editor instance.
|
|
448
|
+
*/ _getData() {
|
|
449
|
+
const editor = this._editor;
|
|
450
|
+
const roots = editor.model.document.roots.filter((root)=>root.isAttached() && root.rootName != '$graveyard');
|
|
451
|
+
const { plugins } = editor;
|
|
452
|
+
// `as any` to avoid linking from external private repo.
|
|
453
|
+
const commentsRepository = plugins.has('CommentsRepository') && plugins.get('CommentsRepository');
|
|
454
|
+
const trackChanges = plugins.has('TrackChanges') && plugins.get('TrackChanges');
|
|
455
|
+
const data = {
|
|
456
|
+
roots: {},
|
|
457
|
+
markers: {},
|
|
458
|
+
commentThreads: JSON.stringify([]),
|
|
459
|
+
suggestions: JSON.stringify([])
|
|
460
|
+
};
|
|
461
|
+
roots.forEach((root)=>{
|
|
462
|
+
data.roots[root.rootName] = {
|
|
463
|
+
content: JSON.stringify(Array.from(root.getChildren())),
|
|
464
|
+
attributes: JSON.stringify(Array.from(root.getAttributes())),
|
|
465
|
+
isLoaded: root._isLoaded
|
|
466
|
+
};
|
|
467
|
+
});
|
|
468
|
+
for (const marker of editor.model.markers){
|
|
469
|
+
if (!marker._affectsData) {
|
|
470
|
+
continue;
|
|
471
|
+
}
|
|
472
|
+
data.markers[marker.name] = {
|
|
473
|
+
rangeJSON: marker.getRange().toJSON(),
|
|
474
|
+
usingOperation: marker._managedUsingOperations,
|
|
475
|
+
affectsData: marker._affectsData
|
|
476
|
+
};
|
|
477
|
+
}
|
|
478
|
+
if (commentsRepository) {
|
|
479
|
+
data.commentThreads = JSON.stringify(commentsRepository.getCommentThreads({
|
|
480
|
+
toJSON: true,
|
|
481
|
+
skipNotAttached: true
|
|
482
|
+
}));
|
|
483
|
+
}
|
|
484
|
+
if (trackChanges) {
|
|
485
|
+
data.suggestions = JSON.stringify(trackChanges.getSuggestions({
|
|
486
|
+
toJSON: true,
|
|
487
|
+
skipNotAttached: true
|
|
488
|
+
}));
|
|
489
|
+
}
|
|
490
|
+
return data;
|
|
491
|
+
}
|
|
492
|
+
/**
|
|
493
|
+
* For each attached model root, returns its HTML editable element (if available).
|
|
494
|
+
*/ _getEditables() {
|
|
495
|
+
const editables = {};
|
|
496
|
+
for (const rootName of this.editor.model.document.getRootNames()){
|
|
497
|
+
const editable = this.editor.ui.getEditableElement(rootName);
|
|
498
|
+
if (editable) {
|
|
499
|
+
editables[rootName] = editable;
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
return editables;
|
|
503
|
+
}
|
|
504
|
+
/**
|
|
505
|
+
* Traverses the error context and the current editor to find out whether these structures are connected
|
|
506
|
+
* to each other via properties.
|
|
507
|
+
*
|
|
508
|
+
* @internal
|
|
509
|
+
*/ _isErrorComingFromThisItem(error) {
|
|
510
|
+
return areConnectedThroughProperties(this._editor, error.context, this._excludedProps);
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Clones the editor configuration.
|
|
514
|
+
*/ _cloneEditorConfiguration(config) {
|
|
515
|
+
return cloneDeepWith(config, (value, key)=>{
|
|
516
|
+
// Leave DOM references.
|
|
517
|
+
if (isElement(value)) {
|
|
518
|
+
return value;
|
|
519
|
+
}
|
|
520
|
+
if (key === 'context') {
|
|
521
|
+
return value;
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* @param Editor The editor class.
|
|
527
|
+
* @param watchdogConfig The watchdog plugin configuration.
|
|
528
|
+
*/ constructor(Editor, watchdogConfig = {}){
|
|
529
|
+
super(watchdogConfig);
|
|
530
|
+
/**
|
|
531
|
+
* The current editor instance.
|
|
532
|
+
*/ this._editor = null;
|
|
533
|
+
/**
|
|
534
|
+
* A promise associated with the life cycle of the editor (creation or destruction processes).
|
|
535
|
+
*
|
|
536
|
+
* It is used to prevent the initialization of the editor if the previous instance has not been destroyed yet,
|
|
537
|
+
* and conversely, to prevent the destruction of the editor if it has not been initialized.
|
|
538
|
+
*/ this._lifecyclePromise = null;
|
|
539
|
+
/**
|
|
540
|
+
* Specifies whether the editor was initialized using document data (`true`) or HTML elements (`false`).
|
|
541
|
+
*/ this._initUsingData = true;
|
|
542
|
+
/**
|
|
543
|
+
* The latest record of the editor editable elements. Used to restart the editor.
|
|
544
|
+
*/ this._editables = {};
|
|
545
|
+
// this._editorClass = Editor;
|
|
546
|
+
this._throttledSave = throttle(this._save.bind(this), typeof watchdogConfig.saveInterval === 'number' ? watchdogConfig.saveInterval : 5000);
|
|
547
|
+
// Set default creator and destructor functions:
|
|
548
|
+
if (Editor) {
|
|
549
|
+
this._creator = (elementOrData, config)=>Editor.create(elementOrData, config);
|
|
550
|
+
}
|
|
551
|
+
this._destructor = (editor)=>editor.destroy();
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
/**
|
|
555
|
+
* Internal plugin that is used to stop the default editor initialization and restoring the editor state
|
|
556
|
+
* based on the `editor.config._watchdogInitialData` data.
|
|
557
|
+
*/ class EditorWatchdogInitPlugin {
|
|
558
|
+
/**
|
|
559
|
+
* @inheritDoc
|
|
560
|
+
*/ init() {
|
|
561
|
+
// Stops the default editor initialization and use the saved data to restore the editor state.
|
|
562
|
+
// Some of data could not be initialize as a config properties. It is important to keep the data
|
|
563
|
+
// in the same form as it was before the restarting.
|
|
564
|
+
this.editor.data.on('init', (evt)=>{
|
|
565
|
+
evt.stop();
|
|
566
|
+
this.editor.model.enqueueChange({
|
|
567
|
+
isUndoable: false
|
|
568
|
+
}, (writer)=>{
|
|
569
|
+
this._restoreCollaborationData();
|
|
570
|
+
this._restoreEditorData(writer);
|
|
571
|
+
});
|
|
572
|
+
this.editor.data.fire('ready');
|
|
573
|
+
// Keep priority `'high' - 1` to be sure that RTC initialization will be first.
|
|
574
|
+
}, {
|
|
575
|
+
priority: 1000 - 1
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* Creates a model node (element or text) based on provided JSON.
|
|
580
|
+
*/ _createNode(writer, jsonNode) {
|
|
581
|
+
if ('name' in jsonNode) {
|
|
582
|
+
// If child has name property, it is an Element.
|
|
583
|
+
const element = writer.createElement(jsonNode.name, jsonNode.attributes);
|
|
584
|
+
if (jsonNode.children) {
|
|
585
|
+
for (const child of jsonNode.children){
|
|
586
|
+
element._appendChild(this._createNode(writer, child));
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return element;
|
|
590
|
+
} else {
|
|
591
|
+
// Otherwise, it is a Text node.
|
|
592
|
+
return writer.createText(jsonNode.data, jsonNode.attributes);
|
|
593
|
+
}
|
|
594
|
+
}
|
|
595
|
+
/**
|
|
596
|
+
* Restores the editor by setting the document data, roots attributes and markers.
|
|
597
|
+
*/ _restoreEditorData(writer) {
|
|
598
|
+
const editor = this.editor;
|
|
599
|
+
Object.entries(this._data.roots).forEach(([rootName, { content, attributes }])=>{
|
|
600
|
+
const parsedNodes = JSON.parse(content);
|
|
601
|
+
const parsedAttributes = JSON.parse(attributes);
|
|
602
|
+
const rootElement = editor.model.document.getRoot(rootName);
|
|
603
|
+
for (const [key, value] of parsedAttributes){
|
|
604
|
+
writer.setAttribute(key, value, rootElement);
|
|
605
|
+
}
|
|
606
|
+
for (const child of parsedNodes){
|
|
607
|
+
const node = this._createNode(writer, child);
|
|
608
|
+
writer.insert(node, rootElement, 'end');
|
|
609
|
+
}
|
|
610
|
+
});
|
|
611
|
+
Object.entries(this._data.markers).forEach(([markerName, markerOptions])=>{
|
|
612
|
+
const { document } = editor.model;
|
|
613
|
+
const { rangeJSON: { start, end }, ...options } = markerOptions;
|
|
614
|
+
const root = document.getRoot(start.root);
|
|
615
|
+
const startPosition = writer.createPositionFromPath(root, start.path, start.stickiness);
|
|
616
|
+
const endPosition = writer.createPositionFromPath(root, end.path, end.stickiness);
|
|
617
|
+
const range = writer.createRange(startPosition, endPosition);
|
|
618
|
+
writer.addMarker(markerName, {
|
|
619
|
+
range,
|
|
620
|
+
...options
|
|
621
|
+
});
|
|
622
|
+
});
|
|
623
|
+
}
|
|
624
|
+
/**
|
|
625
|
+
* Restores the editor collaboration data - comment threads and suggestions.
|
|
626
|
+
*/ _restoreCollaborationData() {
|
|
627
|
+
// `as any` to avoid linking from external private repo.
|
|
628
|
+
const parsedCommentThreads = JSON.parse(this._data.commentThreads);
|
|
629
|
+
const parsedSuggestions = JSON.parse(this._data.suggestions);
|
|
630
|
+
parsedCommentThreads.forEach((commentThreadData)=>{
|
|
631
|
+
const channelId = this.editor.config.get('collaboration.channelId');
|
|
632
|
+
const commentsRepository = this.editor.plugins.get('CommentsRepository');
|
|
633
|
+
if (commentsRepository.hasCommentThread(commentThreadData.threadId)) {
|
|
634
|
+
const commentThread = commentsRepository.getCommentThread(commentThreadData.threadId);
|
|
635
|
+
commentThread.remove();
|
|
636
|
+
}
|
|
637
|
+
commentsRepository.addCommentThread({
|
|
638
|
+
channelId,
|
|
639
|
+
...commentThreadData
|
|
640
|
+
});
|
|
641
|
+
});
|
|
642
|
+
parsedSuggestions.forEach((suggestionData)=>{
|
|
643
|
+
const trackChangesEditing = this.editor.plugins.get('TrackChangesEditing');
|
|
644
|
+
if (trackChangesEditing.hasSuggestion(suggestionData.id)) {
|
|
645
|
+
const suggestion = trackChangesEditing.getSuggestion(suggestionData.id);
|
|
646
|
+
suggestion.attributes = suggestionData.attributes;
|
|
647
|
+
} else {
|
|
648
|
+
trackChangesEditing.addSuggestionData(suggestionData);
|
|
649
|
+
}
|
|
650
|
+
});
|
|
651
|
+
}
|
|
652
|
+
constructor(editor){
|
|
653
|
+
this.editor = editor;
|
|
654
|
+
this._data = editor.config.get('_watchdogInitialData');
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
const mainQueueId = Symbol('MainQueueId');
|
|
659
|
+
class ContextWatchdog extends Watchdog {
|
|
660
|
+
/**
|
|
661
|
+
* Sets the function that is responsible for the context creation.
|
|
662
|
+
* It expects a function that should return a promise (or `undefined`).
|
|
663
|
+
*
|
|
664
|
+
* ```ts
|
|
665
|
+
* watchdog.setCreator( config => Context.create( config ) );
|
|
666
|
+
* ```
|
|
667
|
+
*/ setCreator(creator) {
|
|
668
|
+
this._creator = creator;
|
|
669
|
+
}
|
|
670
|
+
/**
|
|
671
|
+
* Sets the function that is responsible for the context destruction.
|
|
672
|
+
* Overrides the default destruction function, which destroys only the context instance.
|
|
673
|
+
* It expects a function that should return a promise (or `undefined`).
|
|
674
|
+
*
|
|
675
|
+
* ```ts
|
|
676
|
+
* watchdog.setDestructor( context => {
|
|
677
|
+
* // Do something before the context is destroyed.
|
|
678
|
+
*
|
|
679
|
+
* return context
|
|
680
|
+
* .destroy()
|
|
681
|
+
* .then( () => {
|
|
682
|
+
* // Do something after the context is destroyed.
|
|
683
|
+
* } );
|
|
684
|
+
* } );
|
|
685
|
+
* ```
|
|
686
|
+
*/ setDestructor(destructor) {
|
|
687
|
+
this._destructor = destructor;
|
|
688
|
+
}
|
|
689
|
+
/**
|
|
690
|
+
* The context instance. Keep in mind that this property might be changed when the context watchdog restarts,
|
|
691
|
+
* so do not keep this instance internally. Always operate on the `ContextWatchdog#context` property.
|
|
692
|
+
*/ get context() {
|
|
693
|
+
return this._context;
|
|
694
|
+
}
|
|
695
|
+
/**
|
|
696
|
+
* Initializes the context watchdog. Once it is created, the watchdog takes care about
|
|
697
|
+
* recreating the context and the provided items, and starts the error handling mechanism.
|
|
698
|
+
*
|
|
699
|
+
* ```ts
|
|
700
|
+
* await watchdog.create( {
|
|
701
|
+
* plugins: []
|
|
702
|
+
* } );
|
|
703
|
+
* ```
|
|
704
|
+
*
|
|
705
|
+
* @param contextConfig The context configuration. See {@link module:core/context~Context}.
|
|
706
|
+
*/ create(contextConfig = {}) {
|
|
707
|
+
return this._actionQueues.enqueue(mainQueueId, ()=>{
|
|
708
|
+
this._contextConfig = contextConfig;
|
|
709
|
+
return this._create();
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
/**
|
|
713
|
+
* Returns an item instance with the given `itemId`.
|
|
714
|
+
*
|
|
715
|
+
* ```ts
|
|
716
|
+
* const editor1 = watchdog.getItem( 'editor1' );
|
|
717
|
+
* ```
|
|
718
|
+
*
|
|
719
|
+
* @param itemId The item ID.
|
|
720
|
+
* @returns The item instance or `undefined` if an item with a given ID has not been found.
|
|
721
|
+
*/ getItem(itemId) {
|
|
722
|
+
const watchdog = this._getWatchdog(itemId);
|
|
723
|
+
return watchdog._item;
|
|
724
|
+
}
|
|
725
|
+
/**
|
|
726
|
+
* Gets the state of the given item. See {@link #state} for a list of available states.
|
|
727
|
+
*
|
|
728
|
+
* ```ts
|
|
729
|
+
* const editor1State = watchdog.getItemState( 'editor1' );
|
|
730
|
+
* ```
|
|
731
|
+
*
|
|
732
|
+
* @param itemId Item ID.
|
|
733
|
+
* @returns The state of the item.
|
|
734
|
+
*/ getItemState(itemId) {
|
|
735
|
+
const watchdog = this._getWatchdog(itemId);
|
|
736
|
+
return watchdog.state;
|
|
737
|
+
}
|
|
738
|
+
/**
|
|
739
|
+
* Adds items to the watchdog. Once created, instances of these items will be available using the {@link #getItem} method.
|
|
740
|
+
*
|
|
741
|
+
* Items can be passed together as an array of objects:
|
|
742
|
+
*
|
|
743
|
+
* ```ts
|
|
744
|
+
* await watchdog.add( [ {
|
|
745
|
+
* id: 'editor1',
|
|
746
|
+
* type: 'editor',
|
|
747
|
+
* sourceElementOrData: document.querySelector( '#editor' ),
|
|
748
|
+
* config: {
|
|
749
|
+
* plugins: [ Essentials, Paragraph, Bold, Italic ],
|
|
750
|
+
* toolbar: [ 'bold', 'italic', 'alignment' ]
|
|
751
|
+
* },
|
|
752
|
+
* creator: ( element, config ) => ClassicEditor.create( element, config )
|
|
753
|
+
* } ] );
|
|
754
|
+
* ```
|
|
755
|
+
*
|
|
756
|
+
* Or one by one as objects:
|
|
757
|
+
*
|
|
758
|
+
* ```ts
|
|
759
|
+
* await watchdog.add( {
|
|
760
|
+
* id: 'editor1',
|
|
761
|
+
* type: 'editor',
|
|
762
|
+
* sourceElementOrData: document.querySelector( '#editor' ),
|
|
763
|
+
* config: {
|
|
764
|
+
* plugins: [ Essentials, Paragraph, Bold, Italic ],
|
|
765
|
+
* toolbar: [ 'bold', 'italic', 'alignment' ]
|
|
766
|
+
* },
|
|
767
|
+
* creator: ( element, config ) => ClassicEditor.create( element, config )
|
|
768
|
+
* ] );
|
|
769
|
+
* ```
|
|
770
|
+
*
|
|
771
|
+
* Then an instance can be retrieved using the {@link #getItem} method:
|
|
772
|
+
*
|
|
773
|
+
* ```ts
|
|
774
|
+
* const editor1 = watchdog.getItem( 'editor1' );
|
|
775
|
+
* ```
|
|
776
|
+
*
|
|
777
|
+
* Note that this method can be called multiple times, but for performance reasons it is better
|
|
778
|
+
* to pass all items together.
|
|
779
|
+
*
|
|
780
|
+
* @param itemConfigurationOrItemConfigurations An item configuration object or an array of item configurations.
|
|
781
|
+
*/ add(itemConfigurationOrItemConfigurations) {
|
|
782
|
+
const itemConfigurations = toArray(itemConfigurationOrItemConfigurations);
|
|
783
|
+
return Promise.all(itemConfigurations.map((item)=>{
|
|
784
|
+
return this._actionQueues.enqueue(item.id, ()=>{
|
|
785
|
+
if (this.state === 'destroyed') {
|
|
786
|
+
throw new Error('Cannot add items to destroyed watchdog.');
|
|
787
|
+
}
|
|
788
|
+
if (!this._context) {
|
|
789
|
+
throw new Error('Context was not created yet. You should call the `ContextWatchdog#create()` method first.');
|
|
790
|
+
}
|
|
791
|
+
let watchdog;
|
|
792
|
+
if (this._watchdogs.has(item.id)) {
|
|
793
|
+
throw new Error(`Item with the given id is already added: '${item.id}'.`);
|
|
794
|
+
}
|
|
795
|
+
if (item.type === 'editor') {
|
|
796
|
+
watchdog = new EditorWatchdog(null, this._watchdogConfig);
|
|
797
|
+
watchdog.setCreator(item.creator);
|
|
798
|
+
watchdog._setExcludedProperties(this._contextProps);
|
|
799
|
+
if (item.destructor) {
|
|
800
|
+
watchdog.setDestructor(item.destructor);
|
|
801
|
+
}
|
|
802
|
+
this._watchdogs.set(item.id, watchdog);
|
|
803
|
+
// Enqueue the internal watchdog errors within the main queue.
|
|
804
|
+
// And propagate the internal `error` events as `itemError` event.
|
|
805
|
+
watchdog.on('error', (evt, { error, causesRestart })=>{
|
|
806
|
+
this._fire('itemError', {
|
|
807
|
+
itemId: item.id,
|
|
808
|
+
error
|
|
809
|
+
});
|
|
810
|
+
// Do not enqueue the item restart action if the item will not restart.
|
|
811
|
+
if (!causesRestart) {
|
|
812
|
+
return;
|
|
813
|
+
}
|
|
814
|
+
this._actionQueues.enqueue(item.id, ()=>new Promise((res)=>{
|
|
815
|
+
const rethrowRestartEventOnce = ()=>{
|
|
816
|
+
watchdog.off('restart', rethrowRestartEventOnce);
|
|
817
|
+
this._fire('itemRestart', {
|
|
818
|
+
itemId: item.id
|
|
819
|
+
});
|
|
820
|
+
res();
|
|
821
|
+
};
|
|
822
|
+
watchdog.on('restart', rethrowRestartEventOnce);
|
|
823
|
+
}));
|
|
824
|
+
});
|
|
825
|
+
return watchdog.create(item.sourceElementOrData, item.config, this._context);
|
|
826
|
+
} else {
|
|
827
|
+
throw new Error(`Not supported item type: '${item.type}'.`);
|
|
828
|
+
}
|
|
829
|
+
});
|
|
830
|
+
}));
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Removes and destroys item(s) with given ID(s).
|
|
834
|
+
*
|
|
835
|
+
* ```ts
|
|
836
|
+
* await watchdog.remove( 'editor1' );
|
|
837
|
+
* ```
|
|
838
|
+
*
|
|
839
|
+
* Or
|
|
840
|
+
*
|
|
841
|
+
* ```ts
|
|
842
|
+
* await watchdog.remove( [ 'editor1', 'editor2' ] );
|
|
843
|
+
* ```
|
|
844
|
+
*
|
|
845
|
+
* @param itemIdOrItemIds Item ID or an array of item IDs.
|
|
846
|
+
*/ remove(itemIdOrItemIds) {
|
|
847
|
+
const itemIds = toArray(itemIdOrItemIds);
|
|
848
|
+
return Promise.all(itemIds.map((itemId)=>{
|
|
849
|
+
return this._actionQueues.enqueue(itemId, ()=>{
|
|
850
|
+
const watchdog = this._getWatchdog(itemId);
|
|
851
|
+
this._watchdogs.delete(itemId);
|
|
852
|
+
return watchdog.destroy();
|
|
853
|
+
});
|
|
854
|
+
}));
|
|
855
|
+
}
|
|
856
|
+
/**
|
|
857
|
+
* Destroys the context watchdog and all added items.
|
|
858
|
+
* Once the context watchdog is destroyed, new items cannot be added.
|
|
859
|
+
*
|
|
860
|
+
* ```ts
|
|
861
|
+
* await watchdog.destroy();
|
|
862
|
+
* ```
|
|
863
|
+
*/ destroy() {
|
|
864
|
+
return this._actionQueues.enqueue(mainQueueId, ()=>{
|
|
865
|
+
this.state = 'destroyed';
|
|
866
|
+
this._fire('stateChange');
|
|
867
|
+
super.destroy();
|
|
868
|
+
return this._destroy();
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
/**
|
|
872
|
+
* Restarts the context watchdog.
|
|
873
|
+
*/ _restart() {
|
|
874
|
+
return this._actionQueues.enqueue(mainQueueId, ()=>{
|
|
875
|
+
this.state = 'initializing';
|
|
876
|
+
this._fire('stateChange');
|
|
877
|
+
return this._destroy().catch((err)=>{
|
|
878
|
+
console.error('An error happened during destroying the context or items.', err);
|
|
879
|
+
}).then(()=>this._create()).then(()=>this._fire('restart'));
|
|
880
|
+
});
|
|
881
|
+
}
|
|
882
|
+
/**
|
|
883
|
+
* Initializes the context watchdog.
|
|
884
|
+
*/ _create() {
|
|
885
|
+
return Promise.resolve().then(()=>{
|
|
886
|
+
this._startErrorHandling();
|
|
887
|
+
return this._creator(this._contextConfig);
|
|
888
|
+
}).then((context)=>{
|
|
889
|
+
this._context = context;
|
|
890
|
+
this._contextProps = getSubNodes(this._context);
|
|
891
|
+
return Promise.all(Array.from(this._watchdogs.values()).map((watchdog)=>{
|
|
892
|
+
watchdog._setExcludedProperties(this._contextProps);
|
|
893
|
+
return watchdog.create(undefined, undefined, this._context);
|
|
894
|
+
}));
|
|
895
|
+
});
|
|
896
|
+
}
|
|
897
|
+
/**
|
|
898
|
+
* Destroys the context instance and all added items.
|
|
899
|
+
*/ _destroy() {
|
|
900
|
+
return Promise.resolve().then(()=>{
|
|
901
|
+
this._stopErrorHandling();
|
|
902
|
+
const context = this._context;
|
|
903
|
+
this._context = null;
|
|
904
|
+
this._contextProps = new Set();
|
|
905
|
+
return Promise.all(Array.from(this._watchdogs.values()).map((watchdog)=>watchdog.destroy()))// Context destructor destroys each editor.
|
|
906
|
+
.then(()=>this._destructor(context));
|
|
907
|
+
});
|
|
908
|
+
}
|
|
909
|
+
/**
|
|
910
|
+
* Returns the watchdog for a given item ID.
|
|
911
|
+
*
|
|
912
|
+
* @param itemId Item ID.
|
|
913
|
+
*/ _getWatchdog(itemId) {
|
|
914
|
+
const watchdog = this._watchdogs.get(itemId);
|
|
915
|
+
if (!watchdog) {
|
|
916
|
+
throw new Error(`Item with the given id was not registered: ${itemId}.`);
|
|
917
|
+
}
|
|
918
|
+
return watchdog;
|
|
919
|
+
}
|
|
920
|
+
/**
|
|
921
|
+
* Checks whether an error comes from the context instance and not from the item instances.
|
|
922
|
+
*
|
|
923
|
+
* @internal
|
|
924
|
+
*/ _isErrorComingFromThisItem(error) {
|
|
925
|
+
for (const watchdog of this._watchdogs.values()){
|
|
926
|
+
if (watchdog._isErrorComingFromThisItem(error)) {
|
|
927
|
+
return false;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
return areConnectedThroughProperties(this._context, error.context);
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* The context watchdog class constructor.
|
|
934
|
+
*
|
|
935
|
+
* ```ts
|
|
936
|
+
* const watchdog = new ContextWatchdog( Context );
|
|
937
|
+
*
|
|
938
|
+
* await watchdog.create( contextConfiguration );
|
|
939
|
+
*
|
|
940
|
+
* await watchdog.add( item );
|
|
941
|
+
* ```
|
|
942
|
+
*
|
|
943
|
+
* See the {@glink features/watchdog Watchdog feature guide} to learn more how to use this feature.
|
|
944
|
+
*
|
|
945
|
+
* @param Context The {@link module:core/context~Context} class.
|
|
946
|
+
* @param watchdogConfig The watchdog configuration.
|
|
947
|
+
*/ constructor(Context, watchdogConfig = {}){
|
|
948
|
+
super(watchdogConfig);
|
|
949
|
+
/**
|
|
950
|
+
* A map of internal watchdogs for added items.
|
|
951
|
+
*/ this._watchdogs = new Map();
|
|
952
|
+
/**
|
|
953
|
+
* The current context instance.
|
|
954
|
+
*/ this._context = null;
|
|
955
|
+
/**
|
|
956
|
+
* Context properties (nodes/references) that are gathered during the initial context creation
|
|
957
|
+
* and are used to distinguish the origin of an error.
|
|
958
|
+
*/ this._contextProps = new Set();
|
|
959
|
+
/**
|
|
960
|
+
* An action queue, which is used to handle async functions queuing.
|
|
961
|
+
*/ this._actionQueues = new ActionQueues();
|
|
962
|
+
this._watchdogConfig = watchdogConfig;
|
|
963
|
+
// Default creator and destructor.
|
|
964
|
+
this._creator = (contextConfig)=>Context.create(contextConfig);
|
|
965
|
+
this._destructor = (context)=>context.destroy();
|
|
966
|
+
this._actionQueues.onEmpty(()=>{
|
|
967
|
+
if (this.state === 'initializing') {
|
|
968
|
+
this.state = 'ready';
|
|
969
|
+
this._fire('stateChange');
|
|
970
|
+
}
|
|
971
|
+
});
|
|
972
|
+
}
|
|
973
|
+
}
|
|
974
|
+
/**
|
|
975
|
+
* Manager of action queues that allows queuing async functions.
|
|
976
|
+
*/ class ActionQueues {
|
|
977
|
+
/**
|
|
978
|
+
* Used to register callbacks that will be run when the queue becomes empty.
|
|
979
|
+
*
|
|
980
|
+
* @param onEmptyCallback A callback that will be run whenever the queue becomes empty.
|
|
981
|
+
*/ onEmpty(onEmptyCallback) {
|
|
982
|
+
this._onEmptyCallbacks.push(onEmptyCallback);
|
|
983
|
+
}
|
|
984
|
+
/**
|
|
985
|
+
* It adds asynchronous actions (functions) to the proper queue and runs them one by one.
|
|
986
|
+
*
|
|
987
|
+
* @param queueId The action queue ID.
|
|
988
|
+
* @param action A function that should be enqueued.
|
|
989
|
+
*/ enqueue(queueId, action) {
|
|
990
|
+
const isMainAction = queueId === mainQueueId;
|
|
991
|
+
this._activeActions++;
|
|
992
|
+
if (!this._queues.get(queueId)) {
|
|
993
|
+
this._queues.set(queueId, Promise.resolve());
|
|
994
|
+
}
|
|
995
|
+
// List all sources of actions that the current action needs to await for.
|
|
996
|
+
// For the main action wait for all other actions.
|
|
997
|
+
// For the item action wait only for the item queue and the main queue.
|
|
998
|
+
const awaitedActions = isMainAction ? Promise.all(this._queues.values()) : Promise.all([
|
|
999
|
+
this._queues.get(mainQueueId),
|
|
1000
|
+
this._queues.get(queueId)
|
|
1001
|
+
]);
|
|
1002
|
+
const queueWithAction = awaitedActions.then(action);
|
|
1003
|
+
// Catch all errors in the main queue to stack promises even if an error occurred in the past.
|
|
1004
|
+
const nonErrorQueue = queueWithAction.catch(()=>{});
|
|
1005
|
+
this._queues.set(queueId, nonErrorQueue);
|
|
1006
|
+
return queueWithAction.finally(()=>{
|
|
1007
|
+
this._activeActions--;
|
|
1008
|
+
if (this._queues.get(queueId) === nonErrorQueue && this._activeActions === 0) {
|
|
1009
|
+
this._onEmptyCallbacks.forEach((cb)=>cb());
|
|
1010
|
+
}
|
|
1011
|
+
});
|
|
1012
|
+
}
|
|
1013
|
+
constructor(){
|
|
1014
|
+
this._onEmptyCallbacks = [];
|
|
1015
|
+
this._queues = new Map();
|
|
1016
|
+
this._activeActions = 0;
|
|
1017
|
+
}
|
|
1018
|
+
}
|
|
1019
|
+
/**
|
|
1020
|
+
* Transforms any value to an array. If the provided value is already an array, it is returned unchanged.
|
|
1021
|
+
*
|
|
1022
|
+
* @param elementOrArray The value to transform to an array.
|
|
1023
|
+
* @returns An array created from data.
|
|
1024
|
+
*/ function toArray(elementOrArray) {
|
|
1025
|
+
return Array.isArray(elementOrArray) ? elementOrArray : [
|
|
1026
|
+
elementOrArray
|
|
1027
|
+
];
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
export { ContextWatchdog, EditorWatchdog, Watchdog };
|
|
1031
|
+
//# sourceMappingURL=index.js.map
|