ContextWatchdog (watchdog)
@ckeditor/ckeditor5-watchdog/src/contextwatchdog
A watchdog for the Context
class.
See the Watchdog feature guide to learn the rationale behind it and how to use it.
Filtering
Properties
-
The context instance. Keep in mind that this property might be changed when the context watchdog restarts, so do not keep this instance internally. Always operate on the
ContextWatchdog#context
property. -
crashes : Array.<Object>
module:watchdog/contextwatchdog~ContextWatchdog#crashes
readonly inherited
An array of crashes saved as an object with the following properties:
message
:String
,stack
:String
,date
:Number
,filename
:String | undefined
,lineno
:Number | undefined
,colno
:Number | undefined
,
-
state : 'initializing' | 'ready' | 'crashed' | 'crashedPermanently' | 'destroyed'
module:watchdog/contextwatchdog~ContextWatchdog#state
inherited
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 the user can interact with the item.crashed
– A state when an error occurs. It quickly changes toinitializing
orcrashedPermanently
depending on how many and how frequent errors have been caught recently.crashedPermanently
– A state when the watchdog stops reacting to errors and keeps the item it is watching crashed,destroyed
– A state when the item is manually destroyed by the user after callingwatchdog.destroy()
.
-
_crashNumberLimit : Number
module:watchdog/contextwatchdog~ContextWatchdog#_crashNumberLimit
protected inherited
Related:
-
_creator : function
module:watchdog/contextwatchdog~ContextWatchdog#_creator
protected inherited
The creation method.
Related:
-
_destructor : function
module:watchdog/contextwatchdog~ContextWatchdog#_destructor
protected inherited
The destruction method.
Related:
-
_item : Object | undefined
module:watchdog/contextwatchdog~ContextWatchdog#_item
protected inherited
The watched item.
-
_minimumNonErrorTimePeriod : Number
module:watchdog/contextwatchdog~ContextWatchdog#_minimumNonErrorTimePeriod
protected inherited
Related:
-
_now
module:watchdog/contextwatchdog~ContextWatchdog#_now
protected inherited
Returns the result of the
Date.now()
call. It can be overridden in tests to mock time as some popular approaches likesinon.useFakeTimers()
do not work well with error handling. -
_watchdogs : Map.<string, EditorWatchdog>
module:watchdog/contextwatchdog~ContextWatchdog#_watchdogs
protected
A map of internal watchdogs for added items.
-
_actionQueues : ActionQueues
module:watchdog/contextwatchdog~ContextWatchdog#_actionQueues
private
An action queue, which is used to handle async functions queuing.
-
_config : Object | undefined
module:watchdog/contextwatchdog~ContextWatchdog#_config
private
The context configuration.
-
The current context instance.
-
_contextConfig : Object
module:watchdog/contextwatchdog~ContextWatchdog#_contextConfig
private
The configuration for the
Context
. -
_contextProps : Set.<*>
module:watchdog/contextwatchdog~ContextWatchdog#_contextProps
private
Context properties (nodes/references) that are gathered during the initial context creation and are used to distinguish the origin of an error.
-
_listeners : Object.<String, Function>>
module:watchdog/contextwatchdog~ContextWatchdog#_listeners
private inherited
A dictionary of event emitter listeners.
-
_watchdogConfig : WatchdogConfig
module:watchdog/contextwatchdog~ContextWatchdog#_watchdogConfig
private
The watchdog configuration.
Methods
-
constructor( Context, [ watchdogConfig ] )
module:watchdog/contextwatchdog~ContextWatchdog#constructor
The context watchdog class constructor.
const watchdog = new ContextWatchdog( Context ); await watchdog.create( contextConfiguration ); await watchdog.add( item );
See the Watchdog feature guide to learn more how to use this feature.
Parameters
Context : function
The
Context
class.[ watchdogConfig ] : WatchdogConfig
The watchdog configuration.
-
add( itemConfigurationOrItemConfigurations ) → Promise
module:watchdog/contextwatchdog~ContextWatchdog#add
Adds items to the watchdog. Once created, instances of these items will be available using the
getItem
method.Items can be passed together as an array of objects:
await watchdog.add( [ { id: 'editor1', type: 'editor', sourceElementOrData: document.querySelector( '#editor' ), config: { plugins: [ Essentials, Paragraph, Bold, Italic ], toolbar: [ 'bold', 'italic', 'alignment' ] }, creator: ( element, config ) => ClassicEditor.create( element, config ) } ] );
Or one by one as objects:
await watchdog.add( { id: 'editor1', type: 'editor', sourceElementOrData: document.querySelector( '#editor' ), config: { plugins: [ Essentials, Paragraph, Bold, Italic ], toolbar: [ 'bold', 'italic', 'alignment' ] }, creator: ( element, config ) => ClassicEditor.create( element, config ) ] );
Then an instance can be retrieved using the
getItem
method:const editor1 = watchdog.getItem( 'editor1' );
Note that this method can be called multiple times, but for performance reasons it is better to pass all items together.
Parameters
itemConfigurationOrItemConfigurations : WatchdogItemConfiguration | Array.<WatchdogItemConfiguration>
An item configuration object or an array of item configurations.
Returns
Promise
-
create( [ contextConfig ] ) → Promise
module:watchdog/contextwatchdog~ContextWatchdog#create
Initializes the context watchdog. Once it is created, the watchdog takes care about recreating the context and the provided items, and starts the error handling mechanism.
await watchdog.create( { plugins: [] } );
Parameters
[ contextConfig ] : Object
The context configuration. See
Context
.
Returns
Promise
-
destroy()
module:watchdog/contextwatchdog~ContextWatchdog#destroy
inherited
Destroys the watchdog and releases the resources.
-
getItem( itemId ) → *
module:watchdog/contextwatchdog~ContextWatchdog#getItem
Returns an item instance with the given
itemId
.const editor1 = watchdog.getItem( 'editor1' );
Parameters
itemId : String
The item ID.
Returns
*
The item instance or
undefined
if an item with a given ID has not been found.
-
getItemState( itemId ) → 'initializing' | 'ready' | 'crashed' | 'crashedPermanently' | 'destroyed'
module:watchdog/contextwatchdog~ContextWatchdog#getItemState
Gets the state of the given item. See
state
for a list of available states.const editor1State = watchdog.getItemState( 'editor1' );
Parameters
itemId : String
Item ID.
Returns
'initializing' | 'ready' | 'crashed' | 'crashedPermanently' | 'destroyed'
The state of the item.
-
off( eventName, callback )
module:watchdog/contextwatchdog~ContextWatchdog#off
inherited
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.Parameters
eventName : String
The event name.
callback : function
A callback which will be removed from event listeners.
-
on( eventName, callback )
module:watchdog/contextwatchdog~ContextWatchdog#on
inherited
Starts listening to a specific event name by registering a callback that will be executed whenever an event with a given name fires.
Note that this method differs from the CKEditor 5's default
EventEmitterMixin
implementation.Parameters
eventName : String
The event name.
callback : function
A callback which will be added to event listeners.
-
remove( itemIdOrItemIds ) → Promise
module:watchdog/contextwatchdog~ContextWatchdog#remove
Removes and destroys item(s) with given ID(s).
await watchdog.remove( 'editor1' );
Or
await watchdog.remove( [ 'editor1', 'editor2' ] );
Parameters
itemIdOrItemIds : Array.<String> | String
Item ID or an array of item IDs.
Returns
Promise
-
setCreator( creator )
module:watchdog/contextwatchdog~ContextWatchdog#setCreator
inherited
Sets the function that is responsible for the context creation. It expects a function that should return a promise (or
undefined
).watchdog.setCreator( config => Context.create( config ) );
Parameters
creator : function
-
setDestructor( destructor )
module:watchdog/contextwatchdog~ContextWatchdog#setDestructor
inherited
Sets the function that is responsible for the context destruction. Overrides the default destruction function, which destroys only the context instance. It expects a function that should return a promise (or
undefined
).watchdog.setDestructor( context => { // Do something before the context is destroyed. return context .destroy() .then( () => { // Do something after the context is destroyed. } ); } );
Parameters
destructor : function
-
_fire( eventName, args )
module:watchdog/contextwatchdog~ContextWatchdog#_fire
protected inherited
Fires an event with a given event name and arguments.
Note that this method differs from the CKEditor 5's default
EventEmitterMixin
implementation.Parameters
eventName : String
The event name.
args : *
Event arguments.
-
_getWatchdog( itemId ) → Watchdog
module:watchdog/contextwatchdog~ContextWatchdog#_getWatchdog
protected
Returns the watchdog for a given item ID.
-
_isErrorComingFromThisItem( error ) → Boolean
module:watchdog/contextwatchdog~ContextWatchdog#_isErrorComingFromThisItem
protected
Checks whether an error comes from the context instance and not from the item instances.
Parameters
error : Error
Returns
Boolean
-
_restart() → Promise
module:watchdog/contextwatchdog~ContextWatchdog#_restart
protected
Restarts the context watchdog.
Returns
Promise
-
_startErrorHandling()
module:watchdog/contextwatchdog~ContextWatchdog#_startErrorHandling
protected inherited
Starts error handling by attaching global error handlers.
-
_stopErrorHandling()
module:watchdog/contextwatchdog~ContextWatchdog#_stopErrorHandling
protected inherited
Stops error handling by detaching global error handlers.
-
_boundErrorHandler()
module:watchdog/contextwatchdog~ContextWatchdog#_boundErrorHandler
private inherited
Checks if the event error comes from the underlying item and restarts the item.
-
_create() → Promise
module:watchdog/contextwatchdog~ContextWatchdog#_create
private
Returns
Promise
-
_destroy() → Promise
module:watchdog/contextwatchdog~ContextWatchdog#_destroy
private
Destroys the context instance and all added items.
Returns
Promise
-
_handleError( error, evt )
module:watchdog/contextwatchdog~ContextWatchdog#_handleError
private inherited
Checks if an error comes from the watched item and restarts it. It reacts to
CKEditorError
errors only.Parameters
error : Error
Error.
evt : ErrorEvent | PromiseRejectionEvent
An error event.
Fires
-
_shouldReactToError( error )
module:watchdog/contextwatchdog~ContextWatchdog#_shouldReactToError
private inherited
Checks whether an error should be handled by the watchdog.
Parameters
error : Error
An error that was caught by the error handling process.
-
_shouldRestart()
module:watchdog/contextwatchdog~ContextWatchdog#_shouldRestart
private inherited
Checks if the watchdog should restart the underlying item.
Events
-
error( eventInfo )
module:watchdog/contextwatchdog~ContextWatchdog#event:error
inherited
Fired when a new
CKEditorError
error connected to the watchdog instance occurs and the watchdog will react to it.watchdog.on( 'error', ( evt, { error, causesRestart } ) => { console.log( 'An error occurred.' ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
-
itemError( eventInfo )
module:watchdog/contextwatchdog~ContextWatchdog#event:itemError
Fired when a new error occurred in one of the added items.
watchdog.on( 'itemError', ( evt, { error, itemId, causesRestart } ) => { console.log( `An error occurred in an item with the '${ itemId }' ID.` ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
-
itemRestart( eventInfo )
module:watchdog/contextwatchdog~ContextWatchdog#event:itemRestart
Fired after an item has been restarted.
watchdog.on( 'itemRestart', ( evt, { itemId } ) => { console.log( 'An item with with the '${ itemId }' ID has been restarted.' ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
-
restart( eventInfo )
module:watchdog/contextwatchdog~ContextWatchdog#event:restart
Fired after the watchdog restarts the context and the added items because of a crash.
watchdog.on( 'restart', () => { console.log( 'The context has been restarted.' ); } );
Parameters
eventInfo : EventInfo
An object containing information about the fired event.
Every day, we work hard to keep our documentation complete. Have you spotted an outdated information? Is something missing? Please report it via our issue tracker.