UNPKG

@aws-amplify/auth

Version:

Auth category of aws-amplify

66 lines (64 loc) • 2.89 kB
'use strict'; // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 Object.defineProperty(exports, "__esModule", { value: true }); exports.resolveAndClearInflightPromises = exports.armInflightDeadline = exports.addInflightPromise = void 0; const inflightPromises = []; // Module-level singleton backstop timer for the inflight OAuth blocking // deadline. A single timer (not one per waiter) is sufficient because all // parked waiters share one release (`resolveAndClearInflightPromises` drains // them all), and every park re-arms it with the current deadline. let deadlineTimer; const addInflightPromise = (resolver) => { inflightPromises.push(resolver); }; exports.addInflightPromise = addInflightPromise; /** * Arms (or re-arms) the backstop timer that releases parked token consumers * once the inflight OAuth blocking deadline passes. * * On fire it re-reads the deadline instead of trusting the armed one: a fresh * `signInWithRedirect` may have renewed it (re-arm for the new deadline), or * the flow may have been released through another path already (the re-check * returns `undefined` and draining is a no-op). The release is purely local — * it never mutates shared OAuth state, so another tab's still-running flow is * left intact. * * Note: browsers throttle timers in background tabs, which may delay (never * prevent) this release; the cross-tab storage listener remains the fast path. */ const armInflightDeadline = (deadline, recheckDeadline) => { if (deadlineTimer !== undefined) { clearTimeout(deadlineTimer); } deadlineTimer = setTimeout(() => { deadlineTimer = undefined; recheckDeadline() .then(renewedDeadline => { if (renewedDeadline !== undefined) { (0, exports.armInflightDeadline)(renewedDeadline, recheckDeadline); return; } (0, exports.resolveAndClearInflightPromises)(); }) // Fail open: an error while re-checking (e.g. auth config torn down) // must release the waiters rather than leave them parked forever. .catch(() => { (0, exports.resolveAndClearInflightPromises)(); }); }, Math.max(0, deadline - Date.now())); }; exports.armInflightDeadline = armInflightDeadline; const resolveAndClearInflightPromises = () => { // The waiters this timer guarded are being released through this very call // (whatever triggered it) — clear it so no stale timer outlives its waiters. if (deadlineTimer !== undefined) { clearTimeout(deadlineTimer); deadlineTimer = undefined; } while (inflightPromises.length) { inflightPromises.pop()?.(); } }; exports.resolveAndClearInflightPromises = resolveAndClearInflightPromises; //# sourceMappingURL=inflightPromise.js.map