MCPcopy Create free account
hub / github.com/ckeditor/ckeditor5-react / initializeContextWatchdog

Function initializeContextWatchdog

src/context/ckeditorcontext.tsx:108–168  ·  view source on GitHub ↗

* Initializes the context watchdog. * * @returns Watchdog instance.

()

Source from the content-addressed store, hash-verified

106 * @returns Watchdog instance.
107 */
108 function initializeContextWatchdog() {
109 // The prevWatchdogInitializationID variable is used to keep track of the previous initialization ID.
110 // It is used to ensure that the state update is performed only if the current initialization ID matches the previous one.
111 // This helps to avoid race conditions and ensures that the correct context watchdog is associated with the component.
112 const watchdogInitializationID = regenerateInitializationID()!;
113 const contextWatchdog = new ContextWatchdogConstructor( context!, watchdogConfig );
114
115 // Handle error event from context watchdog.
116 contextWatchdog.on( 'error', ( _, errorEvent ) => {
117 /* istanbul ignore else -- @preserve */
118 if ( canUpdateState( watchdogInitializationID ) ) {
119 onError( errorEvent.error, {
120 phase: 'runtime',
121 willContextRestart: errorEvent.causesRestart
122 } );
123 }
124 } );
125
126 // Handle state change event from context watchdog.
127 contextWatchdog.on( 'stateChange', () => {
128 if ( onReady && contextWatchdog.state === 'ready' && canUpdateState( watchdogInitializationID ) ) {
129 onReady(
130 contextWatchdog.context! as TContext,
131 contextWatchdog
132 );
133 }
134 } );
135
136 // Create the context watchdog and initialize it with the provided config.
137 contextWatchdog
138 .create( config )
139 .then( () => {
140 // Check if the state update is still valid and update the current context watchdog.
141 if ( canUpdateState( watchdogInitializationID ) ) {
142 setCurrentContextWatchdog( {
143 status: 'initialized',
144 watchdog: contextWatchdog
145 } );
146 } else {
147 // Destroy the context watchdog if the state update is no longer valid.
148 contextWatchdog.destroy();
149 }
150 } )
151 .catch( error => {
152 // Update the current context watchdog with the error status.
153 if ( canUpdateState( watchdogInitializationID ) ) {
154 // Handle error during context watchdog initialization.
155 onError( error, {
156 phase: 'initialization',
157 willContextRestart: false
158 } );
159
160 setCurrentContextWatchdog( {
161 status: 'error',
162 error
163 } );
164 }
165 } );

Callers 1

CKEditorContextFunction · 0.85

Calls 5

canUpdateStateFunction · 0.85
onMethod · 0.80
createMethod · 0.45
destroyMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…