Experiment to rebuild Diffuse using web applets.
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at main 56 lines 1.7 kB view raw
1import * as IDB from "idb-keyval"; 2 3import { expose, jsonDecode, jsonEncode, transfer } from "@scripts/common"; 4import type { Track } from "@applets/core/types"; 5import { IDB_DEVICE_KEY, IDB_PREFIX } from "./constants"; 6 7//////////////////////////////////////////// 8// ACTIONS 9//////////////////////////////////////////// 10const actions = expose({ 11 getTracks, 12 putTracks, 13}); 14 15export type Actions = typeof actions; 16 17// Actions 18 19async function getTracks() { 20 const encoded = await get({ name: "tracks.json" }); 21 if (!encoded) return []; 22 const tracks = jsonDecode<Track[]>(encoded); 23 return transfer(tracks); 24} 25 26async function putTracks(tracks: Track[]) { 27 const data = jsonEncode(tracks); 28 await put({ name: "tracks.json", data }); 29} 30 31//////////////////////////////////////////// 32// 🛠️ 33//////////////////////////////////////////// 34 35async function get({ name }: { name: string }) { 36 const handle: FileSystemDirectoryHandle | null = (await IDB.get(IDB_DEVICE_KEY)) ?? null; 37 if (!handle) throw new Error("Storage not configured properly, handle not found."); 38 39 try { 40 const fileHandle = await handle.getFileHandle(name); 41 const file = await fileHandle.getFile(); 42 const data = await file.bytes(); 43 return data; 44 } catch (err) { 45 return undefined; 46 } 47} 48 49async function put({ data, name }: { data: Uint8Array; name: string }) { 50 const handle: FileSystemDirectoryHandle | null = (await IDB.get(IDB_DEVICE_KEY)) ?? null; 51 if (!handle) throw new Error("Storage not configured properly, handle not found."); 52 const fileHandle = await handle.getFileHandle(name, { create: true }); 53 const stream = await fileHandle.createWritable(); 54 await stream.write(data); 55 await stream.close(); 56}