import { __awaiter } from "tslib";
/**
 * Resolve `promise`, or **reject** if it doesn't settle within `ms`. Rejecting
 * (rather than resolving to a fallback) lets restore tell a genuine timeout apart
 * from a clean "nothing stored" result, so it can surface a notice for the former
 * and stay silent for the latter. Because the loser of the race is abandoned, a
 * late-resolving `load()` is discarded and can never clobber a now-live session;
 * a late *rejection* is swallowed so it doesn't surface as an unhandled rejection.
 *
 * @internal
 */
export function withTimeout(promise, ms) {
    return __awaiter(this, void 0, void 0, function* () {
        let timer;
        promise.catch(() => { });
        try {
            return yield Promise.race([
                promise,
                new Promise((_resolve, reject) => {
                    timer = setTimeout(() => reject(new Error(`withTimeout: exceeded ${ms}ms`)), ms);
                }),
            ]);
        }
        finally {
            clearTimeout(timer);
        }
    });
}
