fix(modal): close rollout and async lifecycle gaps
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
var MAX_DELAY = 15000;
|
||||
var createFlights = Object.create(null);
|
||||
var polls = Object.create(null);
|
||||
var lastScope = "";
|
||||
var SUCCESS_STATUSES = {
|
||||
paid: true,
|
||||
delivered: true,
|
||||
@@ -32,14 +33,16 @@
|
||||
function userScope() {
|
||||
var config = window.aksrtData || window.aksrt || {};
|
||||
var userId = parseInt(config.userId || config.user_id || 0, 10) || 0;
|
||||
if (userId > 0) return "user:" + userId;
|
||||
return config.isLoggedIn ? "auth" : "guest";
|
||||
var current = userId > 0 ? "user:" + userId : (config.isLoggedIn ? "auth" : "guest");
|
||||
if (lastScope && lastScope !== current) clearFlights();
|
||||
lastScope = current;
|
||||
return current;
|
||||
}
|
||||
|
||||
function clearFlights() {
|
||||
createFlights = Object.create(null);
|
||||
Object.keys(polls).forEach(function (orderNumber) {
|
||||
var active = polls[orderNumber];
|
||||
Object.keys(polls).forEach(function (pollKey) {
|
||||
var active = polls[pollKey];
|
||||
if (active && active.handle && typeof active.handle.abort === "function") active.handle.abort();
|
||||
});
|
||||
polls = Object.create(null);
|
||||
@@ -121,6 +124,7 @@
|
||||
|
||||
function exchange(options) {
|
||||
options = options || {};
|
||||
userScope();
|
||||
var packageId = parseInt(options.packageId || options.package_id || 0, 10);
|
||||
if (!packageId) {
|
||||
return Promise.resolve({ ok: false, status: 400, json: { message: "兑换套餐无效" } });
|
||||
@@ -153,7 +157,9 @@
|
||||
abort: function () {},
|
||||
};
|
||||
}
|
||||
if (polls[orderNumber]) return polls[orderNumber].handle;
|
||||
var pollScope = userScope();
|
||||
var pollKey = pollScope + ":" + orderNumber;
|
||||
if (polls[pollKey]) return polls[pollKey].handle;
|
||||
|
||||
var maxAttempts = Math.max(1, Number(options.maxAttempts || DEFAULT_MAX_ATTEMPTS));
|
||||
var maxElapsed = Math.max(1000, Number(options.maxElapsed || DEFAULT_MAX_ELAPSED));
|
||||
@@ -190,7 +196,7 @@
|
||||
state.controller = null;
|
||||
}
|
||||
if (state.onVisibility) document.removeEventListener("visibilitychange", state.onVisibility);
|
||||
if (polls[orderNumber] && polls[orderNumber].handle === state.handle) delete polls[orderNumber];
|
||||
if (polls[pollKey] && polls[pollKey].handle === state.handle) delete polls[pollKey];
|
||||
state.resolve(result);
|
||||
}
|
||||
|
||||
@@ -226,6 +232,8 @@
|
||||
function run() {
|
||||
state.timer = null;
|
||||
if (state.done || state.aborted) return;
|
||||
// Detect an account transition even when no auth event reached this page.
|
||||
if (userScope() !== pollScope || state.done || state.aborted) return;
|
||||
if (document.hidden) {
|
||||
state.waitingForVisible = true;
|
||||
return;
|
||||
@@ -289,7 +297,7 @@
|
||||
finish({ terminal: false, aborted: true, status: "aborted" });
|
||||
},
|
||||
};
|
||||
polls[orderNumber] = state;
|
||||
polls[pollKey] = state;
|
||||
schedule(0);
|
||||
return state.handle;
|
||||
}
|
||||
@@ -349,7 +357,8 @@
|
||||
poll: poll,
|
||||
start: start,
|
||||
abort: function (orderNumber) {
|
||||
var active = polls[String(orderNumber || "")];
|
||||
var key = userScope() + ":" + String(orderNumber || "");
|
||||
var active = polls[key];
|
||||
if (active && active.handle) active.handle.abort();
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user