Save slots: tests for migration, two windows, take-over, stale writes, delete, import/export, puzzles
This commit is contained in:
parent
da8150b7c4
commit
cd46128c09
3 changed files with 487 additions and 10 deletions
|
|
@ -27,6 +27,9 @@ function load(opts = {}) {
|
|||
window.__TUBES_TEST__ = true;
|
||||
// Seed before the game script runs, so nothing random happens at load time either.
|
||||
if (opts.seed !== false) { let r = opts.seed || 12345; window.Math.random = () => { r = (r * 16807) % 2147483647; return r / 2147483647; }; window.__reseed = (n) => { r = n; }; }
|
||||
// opts.browser (from browser()): windows of one browser share localStorage, its storage events, a broadcast
|
||||
// channel and Web Locks, like real tabs; opts.tab is one tab's sessionStorage, which survives a reload.
|
||||
if (opts.browser) opts.browser.attach(window, opts.tab || new Map(), opts);
|
||||
for (const [k, v] of Object.entries(opts.storage || {})) window.localStorage.setItem(k, v);
|
||||
if (opts.navigatorLanguage) Object.defineProperty(window.navigator, 'language', { value: opts.navigatorLanguage });
|
||||
// A non-English game: jsdom does not fetch the dictionary script, so register it up front.
|
||||
|
|
@ -62,4 +65,44 @@ function seed(ev, n) {
|
|||
ev(`if(window.__reseed)__reseed(${n});else Math.random=(()=>{let s=${n};return()=>{s=(s*16807)%2147483647;return s/2147483647;};})();`);
|
||||
}
|
||||
|
||||
module.exports = { load, seed };
|
||||
// A fake browser for several windows at once (see opts.browser). Storage events, broadcast messages and lock
|
||||
// hand-overs arrive asynchronously, as in browsers: await settle() or flush() before checking them.
|
||||
function browser({ locks = true, channel = true } = {}) {
|
||||
const ls = new Map(), wins = new Set(), held = new Map(), log = [];
|
||||
const later = (f) => setTimeout(f, 0);
|
||||
const store = (map, win, shared) => {
|
||||
const fire = (key, oldValue, newValue) => { if (shared) for (const w of wins) if (w !== win && !w.closed) later(() => { try { w.dispatchEvent(new w.StorageEvent('storage', { key, oldValue, newValue })); } catch (e) {} }); };
|
||||
return {
|
||||
getItem: (k) => { if (shared) log.push(['get', String(k)]); return map.has(String(k)) ? map.get(String(k)) : null; },
|
||||
setItem: (k, v) => { if (win.closed) return; k = String(k); v = String(v); if (shared && b.quota && k.startsWith('sot-save-v1:') && v.length > b.quota) { const e = new win.Error('full'); e.name = 'QuotaExceededError'; throw e; } const o = map.has(k) ? map.get(k) : null; map.set(k, v); if (shared) { log.push(['set', k]); fire(k, o, v); } },
|
||||
removeItem: (k) => { k = String(k); if (!map.has(k)) return; const o = map.get(k); map.delete(k); if (shared) fire(k, o, null); },
|
||||
key: (i) => [...map.keys()][i] ?? null,
|
||||
get length() { return map.size; },
|
||||
clear: () => map.clear(),
|
||||
};
|
||||
};
|
||||
const b = { ls, wins, log, quota: 0,
|
||||
attach(win, tab, opts) {
|
||||
wins.add(win);
|
||||
Object.defineProperty(win, 'localStorage', { configurable: true, get: () => store(ls, win, true) });
|
||||
Object.defineProperty(win, 'sessionStorage', { configurable: true, get: () => store(tab, win, false) });
|
||||
if (channel) win.BroadcastChannel = class { constructor(name) { this.name = name; (b.chans = b.chans || []).push({ c: this, win }); }
|
||||
postMessage(data) { const msg = JSON.parse(JSON.stringify(data)); for (const o of b.chans) if (o.c !== this && o.c.name === this.name && !o.win.closed && o.c.onmessage) later(() => o.c.onmessage({ data: msg })); }
|
||||
close() {} };
|
||||
if (locks) Object.defineProperty(win.navigator, 'locks', { configurable: true, value: {
|
||||
request(name, o, cb) {
|
||||
const cur = held.get(name);
|
||||
if (cur && !cur.win.closed && !o.steal) { if (o.ifAvailable) return win.Promise.resolve().then(() => cb(null)); return win.Promise.reject(new win.Error('busy')); }
|
||||
if (cur) { held.delete(name); cur.reject(new win.DOMException('stolen', 'AbortError')); }
|
||||
return new win.Promise((res, rej) => { const me = { win, reject: rej }; held.set(name, me);
|
||||
win.Promise.resolve().then(() => cb({ name })).then((v) => { if (held.get(name) === me) held.delete(name); res(v); }, rej); });
|
||||
} } });
|
||||
},
|
||||
// A tab closing: its locks go with it.
|
||||
close(win) { win.closed = true; for (const [k, v] of held) if (v.win === win) held.delete(k); wins.delete(win); },
|
||||
};
|
||||
return b;
|
||||
}
|
||||
const flush = (n = 3) => new Promise((r) => { const go = (i) => (i ? setTimeout(() => go(i - 1), 2) : r()); go(n); });
|
||||
|
||||
module.exports = { load, seed, browser, flush };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue