Sign up (with export icon)

ContextWatchdog

Api-class icon class

A watchdog for the Context class.

See the Watchdog feature guide to learn the rationale behind it and how to use it.

Type parameters

Properties

  • Chevron-right icon

    _item : unknown

  • Chevron-right icon

    context : null | Context
    readonly

    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.

  • Chevron-right icon

    crashes : Array<object>
    readonlyinherited

    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,
  • Chevron-right icon

    state : WatchdogState
    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 to initializing or crashedPermanently 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 calling watchdog.destroy().
  • Chevron-right icon

    _creator : ( config: ContextConfig ) => Promise<TContext>
    protected

  • Chevron-right icon

    _destructor : ( context: Context ) => Promise<unknown>
    protected

  • Chevron-right icon

    _watchdogs : Map<string, EditorWatchdog<Editor>>
    protected

    A map of internal watchdogs for added items.

  • Chevron-right icon

    _actionQueues : ActionQueues
    Lock icon private

    An action queue, which is used to handle async functions queuing.

  • Chevron-right icon

    _context : null | TContext
    Lock icon private

    The current context instance.

  • Chevron-right icon

    _contextConfig : ContextConfig | undefined
    Lock icon private

    The configuration for the Context.

  • Chevron-right icon

    _contextProps : Set<unknown>
    Lock icon private

    Context properties (nodes/references) that are gathered during the initial context creation and are used to distinguish the origin of an error.

  • Chevron-right icon

    _watchdogConfig : WatchdogConfig
    Lock icon privatereadonly

    The watchdog configuration.

Methods

  • Chevron-right icon

    constructor( Context = { Context.create }, watchdogConfig )

    The context watchdog class constructor.

    const watchdog = new ContextWatchdog( Context );
    
    await watchdog.create( contextConfiguration );
    
    await watchdog.add( item );
    
    Copy code

    See the Watchdog feature guide to learn more how to use this feature.

    Type parameters

    TContext : extends [object Object] = Context

    Parameters

    Context : object

    The Context class.

    Properties
    Context.create : Promise<TContext>
    watchdogConfig : WatchdogConfig

    The watchdog configuration.

    Defaults to {}

  • Chevron-right icon

    add( itemConfigurationOrItemConfigurations ) → Promise<unknown>

    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 )
    } ] );
    
    Copy code

    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 )
    ] );
    
    Copy code

    Then an instance can be retrieved using the getItem method:

    const editor1 = watchdog.getItem( 'editor1' );
    
    Copy code

    Note 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>
  • Chevron-right icon

    create( contextConfig ) → Promise<unknown>

    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: []
    } );
    
    Copy code

    Parameters

    contextConfig : ContextConfig

    The context configuration. See Context.

    Defaults to {}

    Returns

    Promise<unknown>
  • Chevron-right icon

    destroy() → Promise<unknown>

    Destroys the context watchdog and all added items. Once the context watchdog is destroyed, new items cannot be added.

    await watchdog.destroy();
    
    Copy code

    Returns

    Promise<unknown>
  • Chevron-right icon

    getItem( itemId ) → unknown

    Returns an item instance with the given itemId.

    const editor1 = watchdog.getItem( 'editor1' );
    
    Copy code

    Parameters

    itemId : string

    The item ID.

    Returns

    unknown

    The item instance or undefined if an item with a given ID has not been found.

  • Chevron-right icon

    Gets the state of the given item. See state for a list of available states.

    const editor1State = watchdog.getItemState( 'editor1' );
    
    Copy code

    Parameters

    itemId : string

    Item ID.

    Returns

    WatchdogState

    The state of the item.

  • Chevron-right icon

    off( eventName, callback ) → void
    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 : keyof WatchdogEventMap

    The event name.

    callback : unknown

    A callback which will be removed from event listeners.

    Returns

    void
  • Chevron-right icon

    on( eventName, callback ) → void
    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.

    Type parameters

    K : extends keyof WatchdogEventMap

    Parameters

    eventName : K

    The event name.

    callback : WatchdogEventCallback<K>

    A callback which will be added to event listeners.

    Returns

    void
  • Chevron-right icon

    remove( itemIdOrItemIds ) → Promise<unknown>

    Removes and destroys item(s) with given ID(s).

    await watchdog.remove( 'editor1' );
    
    Copy code

    Or

    await watchdog.remove( [ 'editor1', 'editor2' ] );
    
    Copy code

    Parameters

    itemIdOrItemIds : ArrayOrItem<string>

    Item ID or an array of item IDs.

    Returns

    Promise<unknown>
  • Chevron-right icon

    setCreator( creator ) → void

    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 ) );
    
    Copy code

    Parameters

    creator : ( config: ContextConfig ) => Promise<TContext>

    Returns

    void
  • Chevron-right icon

    setDestructor( destructor ) → void

    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.
    		} );
    } );
    
    Copy code

    Parameters

    destructor : ( context: Context ) => Promise<unknown>

    Returns

    void
  • Chevron-right icon

    _isErrorComingFromThisItem( error ) → boolean
    internal

    Checks whether an error comes from the context instance and not from the item instances.

    Parameters

    error : CKEditorError

    Returns

    boolean
  • Chevron-right icon

    _fire( eventName, args ) → void
    protectedinherited

    Fires an event with a given event name and arguments.

    Note that this method differs from the CKEditor 5's default EventEmitterMixin implementation.

    Type parameters

    K : extends keyof WatchdogEventMap

    Parameters

    eventName : K
    args : WatchdogEventArgs<K>

    Returns

    void
  • Chevron-right icon

    _getWatchdog( itemId ) → Watchdog
    protected

    Returns the watchdog for a given item ID.

    Parameters

    itemId : string

    Item ID.

    Returns

    Watchdog
  • Chevron-right icon

    _restart() → Promise<unknown>
    protected

    Restarts the context watchdog.

    Returns

    Promise<unknown>
  • Chevron-right icon

    _startErrorHandling() → void
    protectedinherited

    Starts error handling by attaching global error handlers.

    Returns

    void
  • Chevron-right icon

    _stopErrorHandling() → void
    protectedinherited

    Stops error handling by detaching global error handlers.

    Returns

    void
  • Chevron-right icon

    _create() → Promise<unknown>
    Lock icon private

    Initializes the context watchdog.

    Returns

    Promise<unknown>
  • Chevron-right icon

    _destroy() → Promise<unknown>
    Lock icon private

    Destroys the context instance and all added items.

    Returns

    Promise<unknown>

Events

  • Chevron-right icon

    error( eventInfo, <anonymous> )
    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.' );
    } );
    
    Copy code

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    <anonymous> : WatchdogErrorEventData
  • Chevron-right icon

    itemError( eventInfo, <anonymous> )

    Fired 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 code

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    <anonymous> : ContextWatchdogItemErrorEventData
  • Chevron-right icon

    itemRestart( eventInfo, <anonymous> )

    Fired after an item has been restarted.

    	watchdog.on( 'itemRestart', ( evt, { itemId } ) => {
    		console.log( 'An item with with the '${ itemId }' ID has been restarted.' );
    	} );
    
    Copy code

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

    <anonymous> : ContextWatchdogItemRestartEventData
  • Chevron-right icon

    restart( eventInfo )

    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.' );
    } );
    
    Copy code

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.

  • Chevron-right icon

    stateChange( eventInfo )
    inherited

    Fired when the watchdog state changed.

    Parameters

    eventInfo : EventInfo

    An object containing information about the fired event.