Experiment to rebuild Diffuse using web applets.
0
fork

Configure Feed

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

refactor: only load input applets when needed

+44 -67
+44 -67
src/pages/configurator/input/_applet.astro
··· 37 37 <script> 38 38 import type { Track } from "@applets/core/types.d.ts"; 39 39 import { applet, register } from "@scripts/applet/common"; 40 + import type { Applet } from "@web-applets/sdk"; 40 41 41 42 //////////////////////////////////////////// 42 43 // SETUP 43 44 //////////////////////////////////////////// 44 45 const context = register(); 45 46 46 - // Applet connections 47 - const input = { 48 - nativeFs: applet("../../input/native-fs"), 49 - opensubsonic: applet("../../input/opensubsonic"), 50 - s3: applet("../../input/s3"), 47 + //////////////////////////////////////////// 48 + // CONNECTIONS 49 + ////////////////////////////////////////////// Applet connections 50 + const CONNECTIONS = { 51 + "file+local": "../../input/native-fs", 52 + opensubsonic: "../../input/opensubsonic", 53 + s3: "../../input/s3", 51 54 }; 52 55 56 + type Scheme = keyof typeof CONNECTIONS; 57 + 58 + const connections: Record<Scheme, Promise<Applet> | undefined> = { 59 + "file+local": undefined, 60 + opensubsonic: undefined, 61 + s3: undefined, 62 + }; 63 + 64 + async function connection(scheme: Scheme) { 65 + connections[scheme] = connections[scheme] ?? applet(CONNECTIONS[scheme]); 66 + return connections[scheme]; 67 + } 68 + 69 + function isSupportedScheme(scheme: string): scheme is Scheme { 70 + return !!CONNECTIONS[scheme as Scheme]; 71 + } 72 + 53 73 //////////////////////////////////////////// 54 74 // ACTIONS 55 75 //////////////////////////////////////////// 56 76 const contextualize = async (tracks: Track[]) => { 57 - const opensubsonic = await input.opensubsonic; 58 - const s3 = await input.s3; 59 - 60 77 const groups = await groupTracksPerScheme(tracks); 78 + const promises = Object.keys(groups).map(async (scheme: string) => { 79 + if (!isSupportedScheme(scheme)) return; 80 + const conn = await connection(scheme); 81 + await conn.sendAction("contextualize", groups[scheme], { timeoutDuration: 60000 * 5 }); 82 + }); 61 83 62 - await Promise.all([ 63 - opensubsonic.sendAction( 64 - "contextualize", 65 - groups[opensubsonic.manifest.input_properties.scheme], 66 - { timeoutDuration: 60000 * 5 }, 67 - ), 68 - s3.sendAction("contextualize", groups[s3.manifest.input_properties.scheme], { 69 - timeoutDuration: 60000 * 5, 70 - }), 71 - ]); 84 + await Promise.all(promises); 72 85 }; 73 86 74 87 const list = async (cachedTracks: Track[] = []) => { 75 - const [nativeFs, opensubsonic, s3] = [ 76 - await input.nativeFs, 77 - await input.opensubsonic, 78 - await input.s3, 79 - ]; 80 - 81 88 const groups = await groupTracksPerScheme(cachedTracks); 82 89 83 90 const promises = Object.entries(groups).map( 84 91 async ([scheme, cachedTracksGroup]: [string, Track[]]) => { 85 - switch (scheme) { 86 - case nativeFs.manifest.input_properties.scheme: 87 - return await nativeFs.sendAction("list", cachedTracksGroup, { 88 - timeoutDuration: 60000 * 60 * 24, 89 - }); 90 - 91 - case opensubsonic.manifest.input_properties.scheme: 92 - return await opensubsonic.sendAction("list", cachedTracksGroup, { 93 - timeoutDuration: 60000 * 60 * 24, 94 - }); 95 - 96 - case s3.manifest.input_properties.scheme: 97 - return await s3.sendAction("list", cachedTracksGroup, { 98 - timeoutDuration: 60000 * 60 * 24, 99 - }); 100 - 101 - default: 102 - return cachedTracks; 103 - } 92 + if (!isSupportedScheme(scheme)) return cachedTracksGroup; 93 + const conn = await connection(scheme); 94 + return await conn.sendAction("list", cachedTracksGroup, { 95 + timeoutDuration: 60000 * 60 * 24, 96 + }); 104 97 }, 105 98 ); 106 99 ··· 112 105 113 106 const resolve = async (args: { method: string; uri: string }) => { 114 107 const scheme = args.uri.split(":", 1)[0]; 115 - const [nativeFs, opensubsonic, s3] = [ 116 - await input.nativeFs, 117 - await input.opensubsonic, 118 - await input.s3, 119 - ]; 120 - 121 - switch (scheme) { 122 - case nativeFs.manifest.input_properties.scheme: 123 - return await nativeFs.sendAction("resolve", args); 124 - 125 - case opensubsonic.manifest.input_properties.scheme: 126 - return await opensubsonic.sendAction("resolve", args); 127 - 128 - case s3.manifest.input_properties.scheme: 129 - return await s3.sendAction("resolve", args); 130 - 131 - default: 132 - return undefined; 133 - } 108 + if (!isSupportedScheme(scheme)) return undefined; 109 + const conn = await connection(scheme); 110 + return await conn.sendAction("resolve", args); 134 111 }; 135 112 136 113 context.setActionHandler("contextualize", contextualize); ··· 146 123 const scheme = track.uri.split(":", 1)[0]; 147 124 return { ...acc, [scheme]: [...(acc[scheme] || []), track] }; 148 125 }, 149 - { 150 - [(await input.nativeFs).manifest.input_properties.scheme]: [], 151 - [(await input.opensubsonic).manifest.input_properties.scheme]: [], 152 - [(await input.s3).manifest.input_properties.scheme]: [], 153 - }, 126 + Object.fromEntries( 127 + Object.entries(CONNECTIONS).map(([k, _v]) => { 128 + return [k, []]; 129 + }), 130 + ), 154 131 ); 155 132 } 156 133 </script>