2019-11-12 11:45:28 +00:00
|
|
|
/*
|
2020-04-22 11:27:39 +00:00
|
|
|
Copyright 2019, 2020 The Matrix.org Foundation C.I.C.
|
2019-11-12 11:45:28 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Returns a promise which resolves with a given value after the given number of ms
|
2020-04-22 11:27:39 +00:00
|
|
|
export function sleep<T>(ms: number, value: T): Promise<T> {
|
|
|
|
return new Promise((resolve => { setTimeout(resolve, ms, value); }));
|
|
|
|
}
|
2019-11-12 11:45:28 +00:00
|
|
|
|
2019-11-14 09:37:26 +00:00
|
|
|
// Returns a promise which resolves when the input promise resolves with its value
|
|
|
|
// or when the timeout of ms is reached with the value of given timeoutValue
|
2020-04-22 11:27:39 +00:00
|
|
|
export async function timeout<T>(promise: Promise<T>, timeoutValue: T, ms: number): Promise<T> {
|
|
|
|
const timeoutPromise = new Promise<T>((resolve) => {
|
2019-11-14 09:37:26 +00:00
|
|
|
const timeoutId = setTimeout(resolve, ms, timeoutValue);
|
|
|
|
promise.then(() => {
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return Promise.race([promise, timeoutPromise]);
|
|
|
|
}
|
|
|
|
|
2020-04-22 11:27:39 +00:00
|
|
|
export interface IDeferred<T> {
|
2020-04-22 12:08:33 +00:00
|
|
|
resolve: (value: T) => void;
|
2020-04-22 11:27:39 +00:00
|
|
|
reject: (any) => void;
|
|
|
|
promise: Promise<T>;
|
|
|
|
}
|
|
|
|
|
2019-11-12 11:45:28 +00:00
|
|
|
// Returns a Deferred
|
2020-04-22 11:27:39 +00:00
|
|
|
export function defer<T>(): IDeferred<T> {
|
2019-11-12 11:45:28 +00:00
|
|
|
let resolve;
|
|
|
|
let reject;
|
|
|
|
|
2020-04-22 11:27:39 +00:00
|
|
|
const promise = new Promise<T>((_resolve, _reject) => {
|
2019-11-12 11:45:28 +00:00
|
|
|
resolve = _resolve;
|
|
|
|
reject = _reject;
|
|
|
|
});
|
|
|
|
|
|
|
|
return {resolve, reject, promise};
|
|
|
|
}
|
2019-11-14 13:52:17 +00:00
|
|
|
|
|
|
|
// Promise.allSettled polyfill until browser support is stable in Firefox
|
2020-04-22 11:27:39 +00:00
|
|
|
export function allSettled<T>(promises: Promise<T>[]): Promise<Array<ISettledFulfilled<T> | ISettledRejected>> {
|
2019-11-14 13:52:17 +00:00
|
|
|
if (Promise.allSettled) {
|
2020-04-22 11:27:39 +00:00
|
|
|
return Promise.allSettled<T>(promises);
|
2019-11-14 13:52:17 +00:00
|
|
|
}
|
|
|
|
|
2020-04-22 11:27:39 +00:00
|
|
|
// @ts-ignore - typescript isn't smart enough to see the disjoint here
|
2019-11-14 13:52:17 +00:00
|
|
|
return Promise.all(promises.map((promise) => {
|
|
|
|
return promise.then(value => ({
|
|
|
|
status: "fulfilled",
|
|
|
|
value,
|
|
|
|
})).catch(reason => ({
|
|
|
|
status: "rejected",
|
|
|
|
reason,
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|