diff --git a/src/ckeditor.tsx b/src/ckeditor.tsx index 482ea5f..635c8cd 100644 --- a/src/ckeditor.tsx +++ b/src/ckeditor.tsx @@ -22,7 +22,7 @@ import type { import type { EditorSemaphoreMountResult } from './lifecycle/LifeCycleEditorSemaphore'; import { uid } from './utils/uid'; -import { ContextWatchdogContext, isContextWatchdogValueWithStatus } from './ckeditorcontext'; +import { ContextWatchdogContext, isContextWatchdogValueWithStatus, isContextWatchdogReadyToUse } from './ckeditorcontext'; import { LifeCycleElementSemaphore } from './lifecycle/LifeCycleElementSemaphore'; const REACT_INTEGRATION_READ_ONLY_LOCK_ID = 'Lock from React integration (@ckeditor/ckeditor5-react)'; @@ -225,7 +225,7 @@ export default class CKEditor extends React.Component { // There is small delay where React did not update the context yet but watchdog is already destroyed. // However editor should be created again in such case, after receiving new context. - if ( isContextWatchdogValueWithStatus( 'initialized', this.context ) && this.context.watchdog.state !== 'destroyed' ) { + if ( isContextWatchdogReadyToUse( this.context ) ) { return new EditorWatchdogAdapter( this.context.watchdog ); } diff --git a/src/ckeditorcontext.tsx b/src/ckeditorcontext.tsx index 71ef08e..8b0d526 100644 --- a/src/ckeditorcontext.tsx +++ b/src/ckeditorcontext.tsx @@ -173,6 +173,16 @@ export const isContextWatchdogValueWithStatus = => isContextWatchdogValue( obj ) && obj.status === status; +/** + * Checks if the provided object is a fully initialized context watchdog value. It prevents race conditions between + * watchdog state that is not fully synchronized with the context state. For example, the watchdog state can be 'destroyed' + * while the context is still being initialized because context setState is pending. + */ +export const isContextWatchdogReadyToUse = ( obj: any ): obj is ExtractContextWatchdogValueByStatus<'initialized'> => ( + isContextWatchdogValueWithStatus( 'initialized', obj ) && + obj.watchdog.state === 'ready' +); + /** * Represents the value of the ContextWatchdog in the CKEditor context. */