Experiment to rebuild Diffuse using web applets.
0

Configure Feed

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

1import * as IDB from "idb-keyval"; 2 3import { expose, jsonDecode, jsonEncode } 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 return jsonDecode<Track[]>(encoded); 23} 24 25async function putTracks(tracks: Track[]) { 26 const data = jsonEncode(tracks); 27 await put({ name: "tracks.json", data }); 28} 29 30//////////////////////////////////////////// 31// 🛠️ 32//////////////////////////////////////////// 33 34async function get({ name }: { name: string }) { 35 const handle: FileSystemDirectoryHandle | null = (await IDB.get(IDB_DEVICE_KEY)) ?? null; 36 if (!handle) throw new Error("Storage not configured properly, handle not found."); 37 38 try { 39 const fileHandle = await handle.getFileHandle(name); 40 const file = await fileHandle.getFile(); 41 const data = await file.bytes(); 42 return data; 43 } catch (err) { 44 return undefined; 45 } 46} 47 48async function put({ data, name }: { data: Uint8Array; name: string }) { 49 const handle: FileSystemDirectoryHandle | null = (await IDB.get(IDB_DEVICE_KEY)) ?? null; 50 if (!handle) throw new Error("Storage not configured properly, handle not found."); 51 const fileHandle = await handle.getFileHandle(name, { create: true }); 52 const stream = await fileHandle.createWritable(); 53 await stream.write(data); 54 await stream.close(); 55}