timing/Scheduler.js
/**
		 * Schedules an action to execute in the future, returning a Promise.
		 *
		 * @public
		 * @param {Function} actionToSchedule - The action to execute.
		 * @param {number} millisecondDelay - Milliseconds before the action can be started.
		 * @param {string=} actionDescription - A description of the action, used for logging purposes.
		 * @returns {Promise}
		 */
Schedules an action to execute in the future, returning a Promise.
@public
@param {Function} actionToSchedule - The action to execute.
@param {number} millisecondDelay - Milliseconds before the action can be started.
@param {string=} actionDescription - A description of the action, used for logging purposes.
@returns {Promise}
Schedules an action to execute in the future returning a Promise . @public @param { Function } actionToSchedule - The action to execute . @param { number } millisecondDelay - Milliseconds before the action can be started . @param { string = } actionDescription - A description of the action used for logging purposes . @returns { Promise }
schedule(actionToSchedule, millisecondDelay, actionDescription) {
			return Promise.resolve()
				.then(() => {
					assert.argumentIsRequired(actionToSchedule, 'actionToSchedule', Function);
					assert.argumentIsRequired(millisecondDelay, 'millisecondDelay', Number);
					assert.argumentIsOptional(actionDescription, 'actionDescription', String);
					if (this.getIsDisposed()) {
						throw new Error('The Scheduler has been disposed.');
					}
					let token;
					const schedulePromise = promise.build((resolveCallback, rejectCallback) => {
						const wrappedAction = () => {
							const disposable = this._timeoutBindings[token];
							if (disposable) {
								disposable.dispose();
							}
							try {
								resolveCallback(actionToSchedule());
							} catch (e) {
								rejectCallback(e);
							}
						};
						token = setTimeout(wrappedAction, millisecondDelay);
						this._timeoutBindings[token] = Disposable.fromAction(() => {
							clearTimeout(token);
							delete this._timeoutBindings[token];
						});
					});
					return schedulePromise;
				});
		}
schedule ( actionToSchedule , millisecondDelay , actionDescription ) { return Promise . resolve ( ) . then ( ( ) => { assert . argumentIsRequired ( actionToSchedule , 'actionToSchedule' , Function ) ; assert . argumentIsRequired ( millisecondDelay , 'millisecondDelay' , Number ) ; assert . argumentIsOptional ( actionDescription , 'actionDescription' , String ) ; if ( this . getIsDisposed ( ) ) { throw new Error ( 'The Scheduler has been disposed.' ) ; } let token ; const schedulePromise = promise . build ( ( resolveCallback , rejectCallback ) => { const wrappedAction = ( ) => { const disposable = this . _timeoutBindings [ token ] ; if ( disposable ) { disposable . dispose ( ) ; } try { resolveCallback ( actionToSchedule ( ) ) ; } catch ( e ) { rejectCallback ( e ) ; } } ; token = setTimeout ( wrappedAction , millisecondDelay ) ; this . _timeoutBindings [ token ] = Disposable . fromAction ( ( ) => { clearTimeout ( token ) ; delete this . _timeoutBindings [ token ] ; } ) ; } ) ; return schedulePromise ; } ) ; }
Schedules an action to execute in the future, returning a Promise.
Schedules an action to execute in the future returning a Promise .
// 2021/05/18, BRI. Invoking dispose cases the clearTimeout function to run. // Running clearTimeout should not be necessary because the timer has elapsed // and the callback is being invoked. However, failing to call clearTimeout in // a Node.js environment (after version 10) leads to a memory leak. Notice that // this function has a reference to the Scheduler instance (via closure). In my // view, this is breaking change between versions 10 and 12 of Node.js. I have // been unable to locate any documentation regarding this change; however, a changes // to did occur (which becomes obvious when inspecting the data structure returned by // the setTimeout function).