ContextWatchdog
A watchdog for the Context class.
See the Watchdog feature guide to learn the rationale behind it and how to use it.
Type parameters
TContext : extends Context
Properties
_item : unknownmodule:watchdog/contextwatchdog~ContextWatchdog#_itemThe watched item.
module:watchdog/contextwatchdog~ContextWatchdog#contextThe 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#contextproperty.crashes : Array<object>readonlyinheritedmodule:watchdog/contextwatchdog~ContextWatchdog#crashesAn 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 : WatchdogStateinheritedmodule:watchdog/contextwatchdog~ContextWatchdog#stateSpecifies 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 toinitializingorcrashedPermanentlydepending 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().
_creator : ( config: ContextConfig ) => Promise<TContext>protectedmodule:watchdog/contextwatchdog~ContextWatchdog#_creator_destructor : ( context: Context ) => Promise<unknown>protectedmodule:watchdog/contextwatchdog~ContextWatchdog#_destructor_watchdogs : Map<string, EditorWatchdog<Editor>>protectedmodule:watchdog/contextwatchdog~ContextWatchdog#_watchdogsA map of internal watchdogs for added items.
_actionQueues : ActionQueuesprivatemodule:watchdog/contextwatchdog~ContextWatchdog#_actionQueuesAn action queue, which is used to handle async functions queuing.
_context : null | TContextprivatemodule:watchdog/contextwatchdog~ContextWatchdog#_contextThe current context instance.
_contextConfig : ContextConfig | undefinedprivatemodule:watchdog/contextwatchdog~ContextWatchdog#_contextConfigThe configuration for the
Context._contextProps : Set<unknown>privatemodule:watchdog/contextwatchdog~ContextWatchdog#_contextPropsContext properties (nodes/references) that are gathered during the initial context creation and are used to distinguish the origin of an error.
_watchdogConfig : WatchdogConfigprivatereadonlymodule:watchdog/contextwatchdog~ContextWatchdog#_watchdogConfigThe watchdog configuration.
Methods
constructor( Context = { Context.create }, watchdogConfig )module:watchdog/contextwatchdog~ContextWatchdog#constructorThe context watchdog class constructor.
const watchdog = new ContextWatchdog( Context ); await watchdog.create( contextConfiguration ); await watchdog.add( item );Copy codeSee the Watchdog feature guide to learn more how to use this feature.
Type parameters
TContext : extends Context
Parameters
Context : objectThe
Contextclass.PropertiesContext.create : Promise<TContext>
watchdogConfig : WatchdogConfigThe watchdog configuration.
Defaults to
{}
add( itemConfigurationOrItemConfigurations ) → Promise<unknown>module:watchdog/contextwatchdog~ContextWatchdog#addAdds items to the watchdog. Once created, instances of these items will be available using the
getItemmethod.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 ) } ] );Copy codeOr 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 ) ] );Copy codeThen an instance can be retrieved using the
getItemmethod:const editor1 = watchdog.getItem( 'editor1' );Copy codeNote that this method can be called multiple times, but for performance reasons it is better to pass all items together.
Parameters
itemConfigurationOrItemConfigurations : ArrayOrItem<ContextWatchdogItemConfiguration>An item configuration object or an array of item configurations.
Returns
Promise<unknown>
create( contextConfig ) → Promise<unknown>module:watchdog/contextwatchdog~ContextWatchdog#createInitializes 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: [] } );Copy codeParameters
contextConfig : ContextConfigThe context configuration. See
Context.Defaults to
{}
Returns
Promise<unknown>
destroy() → Promise<unknown>module:watchdog/contextwatchdog~ContextWatchdog#destroyDestroys the context watchdog and all added items. Once the context watchdog is destroyed, new items cannot be added.
await watchdog.destroy();Copy codeReturns
Promise<unknown>
getItem( itemId ) → unknownmodule:watchdog/contextwatchdog~ContextWatchdog#getItemReturns an item instance with the given
itemId.const editor1 = watchdog.getItem( 'editor1' );Copy codeParameters
itemId : stringThe item ID.
Returns
unknownThe item instance or
undefinedif an item with a given ID has not been found.
getItemState( itemId ) → WatchdogStatemodule:watchdog/contextwatchdog~ContextWatchdog#getItemStateGets the state of the given item. See
statefor a list of available states.const editor1State = watchdog.getItemState( 'editor1' );Copy codeParameters
itemId : stringItem ID.
Returns
WatchdogStateThe state of the item.
off( eventName, callback ) → voidinheritedmodule:watchdog/contextwatchdog~ContextWatchdog#offStops listening to the specified event name by removing the callback from event listeners.
Note that this method differs from the CKEditor 5's default
EventEmitterMixinimplementation.Parameters
eventName : keyof WatchdogEventMapThe event name.
callback : unknownA callback which will be removed from event listeners.
Returns
void
on( eventName, callback ) → voidinheritedmodule:watchdog/contextwatchdog~ContextWatchdog#onStarts 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
EventEmitterMixinimplementation.Type parameters
K : extends keyof WatchdogEventMap
Parameters
eventName : KThe event name.
callback : WatchdogEventCallback<K>A callback which will be added to event listeners.
Returns
void
remove( itemIdOrItemIds ) → Promise<unknown>module:watchdog/contextwatchdog~ContextWatchdog#removeRemoves and destroys item(s) with given ID(s).
await watchdog.remove( 'editor1' );Copy codeOr
await watchdog.remove( [ 'editor1', 'editor2' ] );Copy codeParameters
itemIdOrItemIds : ArrayOrItem<string>Item ID or an array of item IDs.
Returns
Promise<unknown>
setCreator( creator ) → voidmodule:watchdog/contextwatchdog~ContextWatchdog#setCreatorSets 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 ) );Copy codeParameters
creator : ( config: ContextConfig ) => Promise<TContext>
Returns
void
setDestructor( destructor ) → voidmodule:watchdog/contextwatchdog~ContextWatchdog#setDestructorSets 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. } ); } );Copy codeParameters
destructor : ( context: Context ) => Promise<unknown>
Returns
void
_isErrorComingFromThisItem( error ) → booleaninternalmodule:watchdog/contextwatchdog~ContextWatchdog#_isErrorComingFromThisItemChecks whether an error comes from the context instance and not from the item instances.
Parameters
error : CKEditorError
Returns
boolean
_fire( eventName, args ) → voidprotectedinheritedmodule:watchdog/contextwatchdog~ContextWatchdog#_fireFires an event with a given event name and arguments.
Note that this method differs from the CKEditor 5's default
EventEmitterMixinimplementation.Type parameters
K : extends keyof WatchdogEventMap
Parameters
eventName : Kargs : WatchdogEventArgs<K>
Returns
void
_getWatchdog( itemId ) → Watchdogprotectedmodule:watchdog/contextwatchdog~ContextWatchdog#_getWatchdogReturns the watchdog for a given item ID.
Parameters
itemId : stringItem ID.
Returns
_restart() → Promise<unknown>protectedmodule:watchdog/contextwatchdog~ContextWatchdog#_restart_startErrorHandling() → voidprotectedinheritedmodule:watchdog/contextwatchdog~ContextWatchdog#_startErrorHandling_stopErrorHandling() → voidprotectedinheritedmodule:watchdog/contextwatchdog~ContextWatchdog#_stopErrorHandling_create() → Promise<unknown>privatemodule:watchdog/contextwatchdog~ContextWatchdog#_create_destroy() → Promise<unknown>privatemodule:watchdog/contextwatchdog~ContextWatchdog#_destroyDestroys the context instance and all added items.
Returns
Promise<unknown>
Events
error( eventInfo, <anonymous> )inheritedmodule:watchdog/contextwatchdog~ContextWatchdog#event:errorFired when a new
CKEditorErrorerror connected to the watchdog instance occurs and the watchdog will react to it.watchdog.on( 'error', ( evt, { error, causesRestart } ) => { console.log( 'An error occurred.' ); } );Copy codeParameters
eventInfo : EventInfoAn object containing information about the fired event.
<anonymous> : WatchdogErrorEventData
itemError( eventInfo, <anonymous> )module:watchdog/contextwatchdog~ContextWatchdog#event:itemErrorFired when a new error occurred in one of the added items.
watchdog.on( 'itemError', ( evt, { error, itemId } ) => { console.log( `An error occurred in an item with the '${ itemId }' ID.` ); } );Copy codeParameters
eventInfo : EventInfoAn object containing information about the fired event.
<anonymous> : ContextWatchdogItemErrorEventData
itemRestart( eventInfo, <anonymous> )module:watchdog/contextwatchdog~ContextWatchdog#event:itemRestartFired after an item has been restarted.
watchdog.on( 'itemRestart', ( evt, { itemId } ) => { console.log( 'An item with with the '${ itemId }' ID has been restarted.' ); } );Copy codeParameters
eventInfo : EventInfoAn object containing information about the fired event.
<anonymous> : ContextWatchdogItemRestartEventData
restart( eventInfo )module:watchdog/contextwatchdog~ContextWatchdog#event:restartFired after the watchdog restarts the context and the added items because of a crash.
watchdog.on( 'restart', () => { console.log( 'The context has been restarted.' ); } );Copy codeParameters
eventInfo : EventInfoAn object containing information about the fired event.
stateChange( eventInfo )inheritedmodule:watchdog/contextwatchdog~ContextWatchdog#event:stateChangeFired when the watchdog state changed.
Parameters
eventInfo : EventInfoAn object containing information about the fired event.