function createProgressiveLiveSnapshot({fetchSnapshot}) { let value = null; let flight = null; const hasIdentity = snapshot => { const user = snapshot?.context?.user || {}; return Boolean(String(user.login || '').trim()); }; const requireUsable = (snapshot, options) => { if (!options?.requireIdentity || hasIdentity(snapshot)) return snapshot; if (value === snapshot) value = null; throw new Error('Authenticated account identity is unavailable.'); }; const acquire = (options = {}) => { if (value) return Promise.resolve().then(() => requireUsable(value, options)); if (flight) return flight.then(snapshot => requireUsable(snapshot, options)); flight = Promise.resolve().then(() => fetchSnapshot()).then(snapshot => { value = snapshot; flight = null; return snapshot; }, error => { flight = null; throw error; }); return flight.then(snapshot => requireUsable(snapshot, options)); }; return { acquire, snapshot:() => value, pending:() => flight, identity() { const user = value?.context?.user || {}; const login = String(user.login || '').trim(); return {login, accountKey:login && user.id ? String(user.id) + ':' + login : login}; }, }; } if (typeof window !== 'undefined') { window.stackchainProgressiveLiveSnapshot = createProgressiveLiveSnapshot({ fetchSnapshot:async () => { const response = await fetch('api/v1/live', {headers:{Accept:'application/json'}}); if (!response.ok) throw new Error('HTTP ' + response.status); return response.json(); }, }); } if (typeof module !== 'undefined' && module.exports) module.exports = createProgressiveLiveSnapshot;