feat(modal): add cancellable shared runtime
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
0647c501ba
commit
d79b063bf9
12 files changed
+999
-392
No files matched your search
@@ -0,0 +1,150 @@
|
||||
/**
|
||||
* User-scoped in-memory modal cache with short TTLs and request deduplication.
|
||||
* Private modal payloads are never persisted to browser storage.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
(function (window, document) {
|
||||
"use strict";
|
||||
|
||||
var DEFAULT_TTL = 15000;
|
||||
var entries = Object.create(null);
|
||||
var flights = Object.create(null);
|
||||
|
||||
function scope() {
|
||||
var config = window.aksrtData || window.aksrt || {};
|
||||
var userId = parseInt(config.userId || 0, 10) || 0;
|
||||
return (config.isLoggedIn && userId > 0 ? "user:" + userId : "guest") + ":v1";
|
||||
}
|
||||
|
||||
function token(key) {
|
||||
return scope() + "::" + String(key || "");
|
||||
}
|
||||
|
||||
function read(key) {
|
||||
var id = token(key);
|
||||
var entry = entries[id];
|
||||
if (!entry) return { hit: false, value: undefined };
|
||||
if (entry.expiresAt <= Date.now()) {
|
||||
delete entries[id];
|
||||
return { hit: false, value: undefined };
|
||||
}
|
||||
return { hit: true, value: entry.value };
|
||||
}
|
||||
|
||||
function tagsOf(value) {
|
||||
return Array.isArray(value) ? value.slice() : (value ? [value] : []);
|
||||
}
|
||||
|
||||
function set(key, value, options) {
|
||||
options = options || {};
|
||||
var ttl = Number(options.ttl || DEFAULT_TTL);
|
||||
entries[token(key)] = {
|
||||
value: value,
|
||||
expiresAt: Date.now() + Math.max(0, ttl),
|
||||
tags: tagsOf(options.tags),
|
||||
};
|
||||
return value;
|
||||
}
|
||||
|
||||
function matchesTags(entry, wanted) {
|
||||
if (!wanted.length) return true;
|
||||
return wanted.some(function (tag) { return entry.tags.indexOf(tag) >= 0; });
|
||||
}
|
||||
|
||||
function invalidate(tags) {
|
||||
var wanted = Array.isArray(tags) ? tags : (tags ? [tags] : []);
|
||||
var removed = 0;
|
||||
Object.keys(entries).forEach(function (id) {
|
||||
if (!wanted.length || matchesTags(entries[id], wanted)) {
|
||||
delete entries[id];
|
||||
removed += 1;
|
||||
}
|
||||
});
|
||||
Object.keys(flights).forEach(function (id) {
|
||||
var flight = flights[id];
|
||||
if (!wanted.length || matchesTags(flight, wanted)) {
|
||||
flight.cancelled = true;
|
||||
delete flights[id];
|
||||
if (typeof flight.cancel === "function") flight.cancel();
|
||||
}
|
||||
});
|
||||
return removed;
|
||||
}
|
||||
|
||||
function clear() {
|
||||
entries = Object.create(null);
|
||||
Object.keys(flights).forEach(function (id) {
|
||||
var flight = flights[id];
|
||||
flight.cancelled = true;
|
||||
if (typeof flight.cancel === "function") flight.cancel();
|
||||
});
|
||||
flights = Object.create(null);
|
||||
}
|
||||
|
||||
function acquire(key, loader, options) {
|
||||
options = options || {};
|
||||
var id = token(key);
|
||||
var cached = options.skipCache ? { hit: false } : read(key);
|
||||
|
||||
if (cached.hit) {
|
||||
return {
|
||||
cached: true,
|
||||
promise: Promise.resolve(cached.value),
|
||||
release: function () {},
|
||||
};
|
||||
}
|
||||
|
||||
var flight = flights[id];
|
||||
if (!flight) {
|
||||
flight = {
|
||||
refs: 0,
|
||||
settled: false,
|
||||
cancelled: false,
|
||||
tags: tagsOf(options.tags),
|
||||
cancel: options.cancel,
|
||||
};
|
||||
flight.promise = Promise.resolve().then(loader).then(function (value) {
|
||||
flight.settled = true;
|
||||
if (!flight.cancelled && options.store !== false) set(key, value, options);
|
||||
if (flights[id] === flight) delete flights[id];
|
||||
return value;
|
||||
}, function (error) {
|
||||
flight.settled = true;
|
||||
if (flights[id] === flight) delete flights[id];
|
||||
throw error;
|
||||
});
|
||||
flights[id] = flight;
|
||||
}
|
||||
|
||||
flight.refs += 1;
|
||||
var released = false;
|
||||
return {
|
||||
cached: false,
|
||||
promise: flight.promise,
|
||||
release: function () {
|
||||
if (released) return;
|
||||
released = true;
|
||||
flight.refs = Math.max(0, flight.refs - 1);
|
||||
if (flight.refs === 0 && !flight.settled) {
|
||||
flight.cancelled = true;
|
||||
if (flights[id] === flight) delete flights[id];
|
||||
if (typeof flight.cancel === "function") flight.cancel();
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
["aksrt:auth:logout", "aksrt:account-switch", "aksrt:logout", "aksrt:user:logout", "aksrt:auth:changed", "wp_logout"].forEach(function (eventName) {
|
||||
document.addEventListener(eventName, clear);
|
||||
});
|
||||
|
||||
window.AksrtModalCache = {
|
||||
get: function (key) { return read(key); },
|
||||
set: set,
|
||||
acquire: acquire,
|
||||
invalidate: invalidate,
|
||||
clear: clear,
|
||||
scope: scope,
|
||||
};
|
||||
})(window, document);
|
||||
Reference in new issue
Block a user