* Initializes the context watchdog. * * @returns Watchdog instance.
()
| 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 | } ); |
no test coverage detected
searching dependent graphs…