'use strict'; // Save podcast episodes on this device, so they play with no signal. They live in the browser's own storage (a cache the service worker answers from), // so they are not synced between devices, and the browser may clear them if the phone runs low on space. const OFFLINE = 'hub-offline'; const offUrl = id => '/api/podcast/podcasts/' + encodeURIComponent(id) + '/file'; const offHas = async id => { try { return !!(await (await caches.open(OFFLINE)).match(offUrl(id))); } catch { return false; } }; async function offSave(id, onDone) { if (!('caches' in window)) { toast('This browser cannot save for offline'); return; } toast('Saving…'); try { const r = await fetch(offUrl(id), { credentials: 'same-origin' }); if (!r.ok) throw new Error('Could not download (' + r.status + ')'); await (await caches.open(OFFLINE)).put(offUrl(id), r); toast('Saved on this device'); if (navigator.storage && navigator.storage.persist) navigator.storage.persist().catch(() => {}); } catch (e) { try { await (await caches.open(OFFLINE)).delete(offUrl(id)); } catch {} toast(e.message || 'Could not save'); } if (onDone) onDone(); } async function offRemove(id, onDone) { try { await (await caches.open(OFFLINE)).delete(offUrl(id)); toast('Removed from this device'); } catch {} if (onDone) onDone(); }