@@ -5,6 +5,7 @@ import { once } from 'node:events';
55
66import { AlreadyReportedError } from '@rushstack/node-core-library' ;
77
8+ import type { OperationRequestRunCallback } from './Operation' ;
89import { OperationStatus } from './OperationStatus' ;
910import type {
1011 IAfterExecuteEventMessage ,
@@ -30,8 +31,11 @@ export interface IWatchLoopOptions {
3031 onBeforeExecute : ( ) => void ;
3132 /**
3233 * Logging callback when a run is requested (and hasn't already been).
34+ *
35+ * @param requestor - The name of the operation requesting a rerun.
36+ * @param detail - Optional detail about why the rerun is requested, e.g. the name of a changed file.
3337 */
34- onRequestRun : ( requestor ?: string ) => void ;
38+ onRequestRun : OperationRequestRunCallback ;
3539 /**
3640 * Logging callback when a run is aborted.
3741 */
@@ -45,7 +49,7 @@ export interface IWatchLoopOptions {
4549 */
4650export interface IWatchLoopState {
4751 get abortSignal ( ) : AbortSignal ;
48- requestRun : ( requestor ?: string ) => void ;
52+ requestRun : OperationRequestRunCallback ;
4953}
5054
5155/**
@@ -59,8 +63,8 @@ export class WatchLoop implements IWatchLoopState {
5963 private _abortController : AbortController ;
6064 private _isRunning : boolean ;
6165 private _runRequested : boolean ;
62- private _requestRunPromise : Promise < string | undefined > ;
63- private _resolveRequestRun ! : ( requestor ?: string ) => void ;
66+ private _requestRunPromise : Promise < [ string , string ? ] > ;
67+ private _resolveRequestRun ! : ( value : [ string , string ? ] ) => void ;
6468
6569 public constructor ( options : IWatchLoopOptions ) {
6670 this . _options = options ;
@@ -69,7 +73,7 @@ export class WatchLoop implements IWatchLoopState {
6973 this . _isRunning = false ;
7074 // Always start as true, so that any requests prior to first run are silenced.
7175 this . _runRequested = true ;
72- this . _requestRunPromise = new Promise < string | undefined > ( ( resolve ) => {
76+ this . _requestRunPromise = new Promise < [ string , string ? ] > ( ( resolve ) => {
7377 this . _resolveRequestRun = resolve ;
7478 } ) ;
7579 }
@@ -146,7 +150,7 @@ export class WatchLoop implements IWatchLoopState {
146150 }
147151 }
148152
149- function requestRunFromHost ( requestor ?: string ) : void {
153+ function requestRunFromHost ( requestor : string , detail ?: string ) : void {
150154 if ( runRequestedFromHost ) {
151155 return ;
152156 }
@@ -155,7 +159,8 @@ export class WatchLoop implements IWatchLoopState {
155159
156160 const requestRunMessage : IRequestRunEventMessage = {
157161 event : 'requestRun' ,
158- requestor
162+ requestor,
163+ detail
159164 } ;
160165
161166 tryMessageHost ( requestRunMessage ) ;
@@ -192,8 +197,12 @@ export class WatchLoop implements IWatchLoopState {
192197 try {
193198 status = await this . runUntilStableAsync ( abortController . signal ) ;
194199 // ESLINT: "Promises must be awaited, end with a call to .catch, end with a call to .then ..."
195- // eslint-disable-next-line @typescript-eslint/no-floating-promises
196- this . _requestRunPromise . finally ( requestRunFromHost ) ;
200+ this . _requestRunPromise . then (
201+ ( [ requestor , detail ] ) => requestRunFromHost ( requestor , detail ) ,
202+ ( error : Error ) => {
203+ // Unreachable code. The promise will never be rejected.
204+ }
205+ ) ;
197206 } catch ( err ) {
198207 status = OperationStatus . Failure ;
199208 return reject ( err ) ;
@@ -224,16 +233,16 @@ export class WatchLoop implements IWatchLoopState {
224233 /**
225234 * Requests that a new run occur.
226235 */
227- public requestRun : ( requestor ? : string ) => void = ( requestor ?: string ) => {
236+ public requestRun : OperationRequestRunCallback = ( requestor : string , detail ?: string ) => {
228237 if ( ! this . _runRequested ) {
229- this . _options . onRequestRun ( requestor ) ;
238+ this . _options . onRequestRun ( requestor , detail ) ;
230239 this . _runRequested = true ;
231240 if ( this . _isRunning ) {
232241 this . _options . onAbort ( ) ;
233242 this . _abortCurrent ( ) ;
234243 }
235244 }
236- this . _resolveRequestRun ( requestor ) ;
245+ this . _resolveRequestRun ( [ requestor , detail ] ) ;
237246 } ;
238247
239248 /**
@@ -260,7 +269,7 @@ export class WatchLoop implements IWatchLoopState {
260269
261270 if ( this . _runRequested ) {
262271 this . _runRequested = false ;
263- this . _requestRunPromise = new Promise < string | undefined > ( ( resolve ) => {
272+ this . _requestRunPromise = new Promise < [ string , string ? ] > ( ( resolve ) => {
264273 this . _resolveRequestRun = resolve ;
265274 } ) ;
266275 }
0 commit comments