@ckeditor/ckeditor5-watchdog 40.0.0 → 40.2.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/src/watchdog.d.ts CHANGED
@@ -1,229 +1,229 @@
1
- /**
2
- * @license Copyright (c) 2003-2023, 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
- /**
6
- * @module watchdog/watchdog
7
- */
8
- import type { CKEditorError } from 'ckeditor5/src/utils';
9
- import type { EditorWatchdogRestartEvent } from './editorwatchdog';
10
- import type { ContextWatchdogItemErrorEvent, ContextWatchdogItemRestartEvent } from './contextwatchdog';
11
- /**
12
- * An abstract watchdog class that handles most of the error handling process and the state of the underlying component.
13
- *
14
- * See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and how to use it.
15
- *
16
- * @internal
17
- */
18
- export default abstract class Watchdog {
19
- /**
20
- * An array of crashes saved as an object with the following properties:
21
- *
22
- * * `message`: `String`,
23
- * * `stack`: `String`,
24
- * * `date`: `Number`,
25
- * * `filename`: `String | undefined`,
26
- * * `lineno`: `Number | undefined`,
27
- * * `colno`: `Number | undefined`,
28
- */
29
- readonly crashes: Array<{
30
- message: string;
31
- stack?: string;
32
- date: number;
33
- filename?: string;
34
- lineno?: number;
35
- colno?: number;
36
- }>;
37
- /**
38
- * Specifies the state of the item watched by the watchdog. The state can be one of the following values:
39
- *
40
- * * `initializing` &ndash; Before the first initialization, and after crashes, before the item is ready.
41
- * * `ready` &ndash; A state when the user can interact with the item.
42
- * * `crashed` &ndash; A state when an error occurs. It quickly changes to `initializing` or `crashedPermanently`
43
- * depending on how many and how frequent errors have been caught recently.
44
- * * `crashedPermanently` &ndash; A state when the watchdog stops reacting to errors and keeps the item it is watching crashed,
45
- * * `destroyed` &ndash; A state when the item is manually destroyed by the user after calling `watchdog.destroy()`.
46
- */
47
- state: WatchdogState;
48
- /**
49
- * @see module:watchdog/watchdog~WatchdogConfig
50
- */
51
- private _crashNumberLimit;
52
- /**
53
- * Returns the result of the `Date.now()` call. It can be overridden in tests to mock time as some popular
54
- * approaches like `sinon.useFakeTimers()` do not work well with error handling.
55
- */
56
- private _now;
57
- /**
58
- * @see module:watchdog/watchdog~WatchdogConfig
59
- */
60
- private _minimumNonErrorTimePeriod;
61
- /**
62
- * Checks if the event error comes from the underlying item and restarts the item.
63
- */
64
- private _boundErrorHandler;
65
- /**
66
- * The method responsible for restarting the watched item.
67
- */
68
- protected abstract _restart(): Promise<unknown>;
69
- /**
70
- * Traverses the error context and the watched item to find out whether the error should
71
- * be handled by the given item.
72
- *
73
- * @internal
74
- */
75
- abstract _isErrorComingFromThisItem(error: CKEditorError): boolean;
76
- /**
77
- * The watched item.
78
- *
79
- * @internal
80
- */
81
- abstract get _item(): unknown;
82
- /**
83
- * A dictionary of event emitter listeners.
84
- */
85
- private _listeners;
86
- /**
87
- * @param {module:watchdog/watchdog~WatchdogConfig} config The watchdog plugin configuration.
88
- */
89
- constructor(config: WatchdogConfig);
90
- /**
91
- * Destroys the watchdog and releases the resources.
92
- */
93
- destroy(): void;
94
- /**
95
- * Starts listening to a specific event name by registering a callback that will be executed
96
- * whenever an event with a given name fires.
97
- *
98
- * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
99
- *
100
- * @param eventName The event name.
101
- * @param callback A callback which will be added to event listeners.
102
- */
103
- on<K extends keyof EventMap>(eventName: K, callback: EventCallback<K>): void;
104
- /**
105
- * Stops listening to the specified event name by removing the callback from event listeners.
106
- *
107
- * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
108
- *
109
- * @param eventName The event name.
110
- * @param callback A callback which will be removed from event listeners.
111
- */
112
- off(eventName: keyof EventMap, callback: unknown): void;
113
- /**
114
- * Fires an event with a given event name and arguments.
115
- *
116
- * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
117
- */
118
- protected _fire<K extends keyof EventMap>(eventName: K, ...args: EventArgs<K>): void;
119
- /**
120
- * Starts error handling by attaching global error handlers.
121
- */
122
- protected _startErrorHandling(): void;
123
- /**
124
- * Stops error handling by detaching global error handlers.
125
- */
126
- protected _stopErrorHandling(): void;
127
- /**
128
- * Checks if an error comes from the watched item and restarts it.
129
- * It reacts to {@link module:utils/ckeditorerror~CKEditorError `CKEditorError` errors} only.
130
- *
131
- * @fires error
132
- * @param error Error.
133
- * @param evt An error event.
134
- */
135
- private _handleError;
136
- /**
137
- * Checks whether an error should be handled by the watchdog.
138
- *
139
- * @param error An error that was caught by the error handling process.
140
- */
141
- private _shouldReactToError;
142
- /**
143
- * Checks if the watchdog should restart the underlying item.
144
- */
145
- private _shouldRestart;
146
- }
147
- /**
148
- * Fired when a new {@link module:utils/ckeditorerror~CKEditorError `CKEditorError`} error connected to the watchdog instance occurs
149
- * and the watchdog will react to it.
150
- *
151
- * ```ts
152
- * watchdog.on( 'error', ( evt, { error, causesRestart } ) => {
153
- * console.log( 'An error occurred.' );
154
- * } );
155
- * ```
156
- *
157
- * @eventName ~Watchdog#error
158
- */
159
- export type WatchdogErrorEvent = {
160
- name: 'error';
161
- args: [WatchdogErrorEventData];
162
- };
163
- /**
164
- * The `error` event data.
165
- */
166
- export type WatchdogErrorEventData = {
167
- error: Error;
168
- causesRestart: boolean;
169
- };
170
- /**
171
- * Fired when the watchdog state changed.
172
- *
173
- * @eventName ~Watchdog#stateChange
174
- */
175
- export type WatchdogStateChangeEvent = {
176
- name: 'stateChange';
177
- args: [];
178
- };
179
- /**
180
- * The map of watchdog events.
181
- */
182
- export interface EventMap {
183
- stateChange: WatchdogStateChangeEvent;
184
- error: WatchdogErrorEvent;
185
- restart: EditorWatchdogRestartEvent;
186
- itemError: ContextWatchdogItemErrorEvent;
187
- itemRestart: ContextWatchdogItemRestartEvent;
188
- }
189
- /**
190
- * Utility type that gets the arguments type for the given event.
191
- */
192
- export type EventArgs<K extends keyof EventMap> = EventMap[K]['args'];
193
- /**
194
- * Utility type that gets the callback type for the given event.
195
- */
196
- export type EventCallback<K extends keyof EventMap> = (evt: null, ...args: EventArgs<K>) => void;
197
- /**
198
- * The watchdog plugin configuration.
199
- */
200
- export interface WatchdogConfig {
201
- /**
202
- * A threshold specifying the number of watched item crashes
203
- * when the watchdog stops restarting the item in case of errors.
204
- * After this limit is reached and the time between the last errors is shorter than `minimumNonErrorTimePeriod`,
205
- * the watchdog changes its state to `crashedPermanently` and it stops restarting the item. This prevents an infinite restart loop.
206
- *
207
- * @default 3
208
- */
209
- crashNumberLimit?: number;
210
- /**
211
- * An average number of milliseconds between the last watched item errors
212
- * (defaults to 5000). When the period of time between errors is lower than that and the `crashNumberLimit` is also reached,
213
- * the watchdog changes its state to `crashedPermanently` and it stops restarting the item. This prevents an infinite restart loop.
214
- *
215
- * @default 5000
216
- */
217
- minimumNonErrorTimePeriod?: number;
218
- /**
219
- * A minimum number of milliseconds between saving the editor data internally (defaults to 5000).
220
- * Note that for large documents this might impact the editor performance.
221
- *
222
- * @default 5000
223
- */
224
- saveInterval?: number;
225
- }
226
- /**
227
- * Specifies the state of the item watched by the watchdog.
228
- */
229
- export type WatchdogState = 'initializing' | 'ready' | 'crashed' | 'crashedPermanently' | 'destroyed';
1
+ /**
2
+ * @license Copyright (c) 2003-2023, 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
+ /**
6
+ * @module watchdog/watchdog
7
+ */
8
+ import type { CKEditorError } from 'ckeditor5/src/utils';
9
+ import type { EditorWatchdogRestartEvent } from './editorwatchdog';
10
+ import type { ContextWatchdogItemErrorEvent, ContextWatchdogItemRestartEvent } from './contextwatchdog';
11
+ /**
12
+ * An abstract watchdog class that handles most of the error handling process and the state of the underlying component.
13
+ *
14
+ * See the {@glink features/watchdog Watchdog feature guide} to learn the rationale behind it and how to use it.
15
+ *
16
+ * @internal
17
+ */
18
+ export default abstract class Watchdog {
19
+ /**
20
+ * An array of crashes saved as an object with the following properties:
21
+ *
22
+ * * `message`: `String`,
23
+ * * `stack`: `String`,
24
+ * * `date`: `Number`,
25
+ * * `filename`: `String | undefined`,
26
+ * * `lineno`: `Number | undefined`,
27
+ * * `colno`: `Number | undefined`,
28
+ */
29
+ readonly crashes: Array<{
30
+ message: string;
31
+ stack?: string;
32
+ date: number;
33
+ filename?: string;
34
+ lineno?: number;
35
+ colno?: number;
36
+ }>;
37
+ /**
38
+ * Specifies the state of the item watched by the watchdog. The state can be one of the following values:
39
+ *
40
+ * * `initializing` &ndash; Before the first initialization, and after crashes, before the item is ready.
41
+ * * `ready` &ndash; A state when the user can interact with the item.
42
+ * * `crashed` &ndash; A state when an error occurs. It quickly changes to `initializing` or `crashedPermanently`
43
+ * depending on how many and how frequent errors have been caught recently.
44
+ * * `crashedPermanently` &ndash; A state when the watchdog stops reacting to errors and keeps the item it is watching crashed,
45
+ * * `destroyed` &ndash; A state when the item is manually destroyed by the user after calling `watchdog.destroy()`.
46
+ */
47
+ state: WatchdogState;
48
+ /**
49
+ * @see module:watchdog/watchdog~WatchdogConfig
50
+ */
51
+ private _crashNumberLimit;
52
+ /**
53
+ * Returns the result of the `Date.now()` call. It can be overridden in tests to mock time as some popular
54
+ * approaches like `sinon.useFakeTimers()` do not work well with error handling.
55
+ */
56
+ private _now;
57
+ /**
58
+ * @see module:watchdog/watchdog~WatchdogConfig
59
+ */
60
+ private _minimumNonErrorTimePeriod;
61
+ /**
62
+ * Checks if the event error comes from the underlying item and restarts the item.
63
+ */
64
+ private _boundErrorHandler;
65
+ /**
66
+ * The method responsible for restarting the watched item.
67
+ */
68
+ protected abstract _restart(): Promise<unknown>;
69
+ /**
70
+ * Traverses the error context and the watched item to find out whether the error should
71
+ * be handled by the given item.
72
+ *
73
+ * @internal
74
+ */
75
+ abstract _isErrorComingFromThisItem(error: CKEditorError): boolean;
76
+ /**
77
+ * The watched item.
78
+ *
79
+ * @internal
80
+ */
81
+ abstract get _item(): unknown;
82
+ /**
83
+ * A dictionary of event emitter listeners.
84
+ */
85
+ private _listeners;
86
+ /**
87
+ * @param {module:watchdog/watchdog~WatchdogConfig} config The watchdog plugin configuration.
88
+ */
89
+ constructor(config: WatchdogConfig);
90
+ /**
91
+ * Destroys the watchdog and releases the resources.
92
+ */
93
+ destroy(): void;
94
+ /**
95
+ * Starts listening to a specific event name by registering a callback that will be executed
96
+ * whenever an event with a given name fires.
97
+ *
98
+ * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
99
+ *
100
+ * @param eventName The event name.
101
+ * @param callback A callback which will be added to event listeners.
102
+ */
103
+ on<K extends keyof EventMap>(eventName: K, callback: EventCallback<K>): void;
104
+ /**
105
+ * Stops listening to the specified event name by removing the callback from event listeners.
106
+ *
107
+ * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
108
+ *
109
+ * @param eventName The event name.
110
+ * @param callback A callback which will be removed from event listeners.
111
+ */
112
+ off(eventName: keyof EventMap, callback: unknown): void;
113
+ /**
114
+ * Fires an event with a given event name and arguments.
115
+ *
116
+ * Note that this method differs from the CKEditor 5's default `EventEmitterMixin` implementation.
117
+ */
118
+ protected _fire<K extends keyof EventMap>(eventName: K, ...args: EventArgs<K>): void;
119
+ /**
120
+ * Starts error handling by attaching global error handlers.
121
+ */
122
+ protected _startErrorHandling(): void;
123
+ /**
124
+ * Stops error handling by detaching global error handlers.
125
+ */
126
+ protected _stopErrorHandling(): void;
127
+ /**
128
+ * Checks if an error comes from the watched item and restarts it.
129
+ * It reacts to {@link module:utils/ckeditorerror~CKEditorError `CKEditorError` errors} only.
130
+ *
131
+ * @fires error
132
+ * @param error Error.
133
+ * @param evt An error event.
134
+ */
135
+ private _handleError;
136
+ /**
137
+ * Checks whether an error should be handled by the watchdog.
138
+ *
139
+ * @param error An error that was caught by the error handling process.
140
+ */
141
+ private _shouldReactToError;
142
+ /**
143
+ * Checks if the watchdog should restart the underlying item.
144
+ */
145
+ private _shouldRestart;
146
+ }
147
+ /**
148
+ * Fired when a new {@link module:utils/ckeditorerror~CKEditorError `CKEditorError`} error connected to the watchdog instance occurs
149
+ * and the watchdog will react to it.
150
+ *
151
+ * ```ts
152
+ * watchdog.on( 'error', ( evt, { error, causesRestart } ) => {
153
+ * console.log( 'An error occurred.' );
154
+ * } );
155
+ * ```
156
+ *
157
+ * @eventName ~Watchdog#error
158
+ */
159
+ export type WatchdogErrorEvent = {
160
+ name: 'error';
161
+ args: [WatchdogErrorEventData];
162
+ };
163
+ /**
164
+ * The `error` event data.
165
+ */
166
+ export type WatchdogErrorEventData = {
167
+ error: Error;
168
+ causesRestart: boolean;
169
+ };
170
+ /**
171
+ * Fired when the watchdog state changed.
172
+ *
173
+ * @eventName ~Watchdog#stateChange
174
+ */
175
+ export type WatchdogStateChangeEvent = {
176
+ name: 'stateChange';
177
+ args: [];
178
+ };
179
+ /**
180
+ * The map of watchdog events.
181
+ */
182
+ export interface EventMap {
183
+ stateChange: WatchdogStateChangeEvent;
184
+ error: WatchdogErrorEvent;
185
+ restart: EditorWatchdogRestartEvent;
186
+ itemError: ContextWatchdogItemErrorEvent;
187
+ itemRestart: ContextWatchdogItemRestartEvent;
188
+ }
189
+ /**
190
+ * Utility type that gets the arguments type for the given event.
191
+ */
192
+ export type EventArgs<K extends keyof EventMap> = EventMap[K]['args'];
193
+ /**
194
+ * Utility type that gets the callback type for the given event.
195
+ */
196
+ export type EventCallback<K extends keyof EventMap> = (evt: null, ...args: EventArgs<K>) => void;
197
+ /**
198
+ * The watchdog plugin configuration.
199
+ */
200
+ export interface WatchdogConfig {
201
+ /**
202
+ * A threshold specifying the number of watched item crashes
203
+ * when the watchdog stops restarting the item in case of errors.
204
+ * After this limit is reached and the time between the last errors is shorter than `minimumNonErrorTimePeriod`,
205
+ * the watchdog changes its state to `crashedPermanently` and it stops restarting the item. This prevents an infinite restart loop.
206
+ *
207
+ * @default 3
208
+ */
209
+ crashNumberLimit?: number;
210
+ /**
211
+ * An average number of milliseconds between the last watched item errors
212
+ * (defaults to 5000). When the period of time between errors is lower than that and the `crashNumberLimit` is also reached,
213
+ * the watchdog changes its state to `crashedPermanently` and it stops restarting the item. This prevents an infinite restart loop.
214
+ *
215
+ * @default 5000
216
+ */
217
+ minimumNonErrorTimePeriod?: number;
218
+ /**
219
+ * A minimum number of milliseconds between saving the editor data internally (defaults to 5000).
220
+ * Note that for large documents this might impact the editor performance.
221
+ *
222
+ * @default 5000
223
+ */
224
+ saveInterval?: number;
225
+ }
226
+ /**
227
+ * Specifies the state of the item watched by the watchdog.
228
+ */
229
+ export type WatchdogState = 'initializing' | 'ready' | 'crashed' | 'crashedPermanently' | 'destroyed';