2021-08-31 14:44:08 -07:00
|
|
|
/**
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-04-07 12:55:44 -08:00
|
|
|
import { monotonicTime } from './';
|
2021-08-31 14:44:08 -07:00
|
|
|
|
2023-07-18 17:03:26 -07:00
|
|
|
export async function raceAgainstDeadline<T>(cb: () => Promise<T>, deadline: number): Promise<{ result: T, timedOut: false } | { timedOut: true }> {
|
2024-03-11 15:43:50 -07:00
|
|
|
let timer: NodeJS.Timeout | undefined;
|
|
|
|
return Promise.race([
|
|
|
|
cb().then(result => {
|
|
|
|
return { result, timedOut: false };
|
|
|
|
}),
|
|
|
|
new Promise<{ timedOut: true }>(resolve => {
|
|
|
|
const kMaxDeadline = 2147483647; // 2^31-1
|
|
|
|
const timeout = (deadline || kMaxDeadline) - monotonicTime();
|
|
|
|
timer = setTimeout(() => resolve({ timedOut: true }), timeout);
|
|
|
|
}),
|
|
|
|
]).finally(() => {
|
|
|
|
clearTimeout(timer);
|
|
|
|
});
|
2021-08-31 14:44:08 -07:00
|
|
|
}
|
2023-01-05 11:14:37 -08:00
|
|
|
|
2023-07-18 17:03:26 -07:00
|
|
|
export async function pollAgainstDeadline<T>(callback: () => Promise<{ continuePolling: boolean, result: T }>, deadline: number, pollIntervals: number[] = [100, 250, 500, 1000]): Promise<{ result?: T, timedOut: boolean }> {
|
2023-01-05 11:14:37 -08:00
|
|
|
const lastPollInterval = pollIntervals.pop() ?? 1000;
|
|
|
|
let lastResult: T|undefined;
|
|
|
|
const wrappedCallback = () => Promise.resolve().then(callback);
|
|
|
|
while (true) {
|
2023-07-18 17:03:26 -07:00
|
|
|
const time = monotonicTime();
|
|
|
|
if (deadline && time >= deadline)
|
2023-01-05 11:14:37 -08:00
|
|
|
break;
|
2023-07-18 17:03:26 -07:00
|
|
|
const received = await raceAgainstDeadline(wrappedCallback, deadline);
|
2023-01-05 11:14:37 -08:00
|
|
|
if (received.timedOut)
|
|
|
|
break;
|
2023-02-19 11:18:07 -08:00
|
|
|
lastResult = (received as any).result.result;
|
|
|
|
if (!(received as any).result.continuePolling)
|
|
|
|
return { result: lastResult, timedOut: false };
|
2023-01-23 17:57:37 -08:00
|
|
|
const interval = pollIntervals!.shift() ?? lastPollInterval;
|
2023-07-18 17:03:26 -07:00
|
|
|
if (deadline && deadline <= monotonicTime() + interval)
|
2023-01-23 17:57:37 -08:00
|
|
|
break;
|
2023-01-20 15:47:24 -08:00
|
|
|
await new Promise(x => setTimeout(x, interval));
|
2023-01-05 11:14:37 -08:00
|
|
|
}
|
|
|
|
return { timedOut: true, result: lastResult };
|
|
|
|
}
|