A music player that connects to your cloud/distributed storage.
at main 81 lines 1.4 kB view raw
1import type { App } from "./elm/types" 2import { db } from "../common" 3import { reportError } from "./common" 4import { transformUrl } from "../urls" 5 6 7// 🏔️ 8 9 10let app: App 11 12 13 14// 🚀 15 16 17export function init(a: App) { 18 app = a 19 20 app.ports.downloadTracks.subscribe(downloadTracks) 21 app.ports.removeTracksFromCache.subscribe(removeTracksFromCache) 22 app.ports.storeTracksInCache.subscribe(storeTracksInCache) 23} 24 25 26 27// PORTS 28 29 30function downloadTracks(group) { 31 self.postMessage({ 32 action: "DOWNLOAD_TRACKS", 33 data: group 34 }) 35} 36 37 38function removeTracksFromCache(trackIds) { 39 trackIds.reduce( 40 (acc, id) => acc.then(_ => db("tracks").removeItem(id)), 41 Promise.resolve() 42 43 ).catch( 44 _ => reportError 45 (app, { tag: "REMOVE_TRACKS_FROM_CACHE" }) 46 ("Failed to remove tracks from cache") 47 48 ) 49} 50 51 52function storeTracksInCache(list) { 53 list.reduce( 54 (acc, item) => { 55 return acc 56 .then(_ => transformUrl(item.url, app)) 57 .then(fetch) 58 .then(r => r.blob()) 59 .then(b => db("tracks").setItem(item.trackId, b)) 60 }, 61 Promise.resolve() 62 63 ).then( 64 _ => self.postMessage({ 65 tag: "STORE_TRACKS_IN_CACHE", 66 data: list.map(l => l.trackId), 67 error: null 68 }) 69 70 ).catch( 71 err => { 72 console.error(err) 73 self.postMessage({ 74 tag: "STORE_TRACKS_IN_CACHE", 75 data: list.map(l => l.trackId), 76 error: err.message || err 77 }) 78 } 79 80 ) 81}