A music player that connects to your cloud/distributed storage.
0
fork

Configure Feed

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

at v4 115 lines 3.1 kB view raw
1import { computed } from "~/common/signal.js"; 2import { OutputTransformer } from "../../base.js"; 3import { defineElement } from "~/common/element.js"; 4 5/** 6 * @import { OutputManagerDeputy } from "~/components/output/types.d.ts" 7 */ 8 9/** 10 * @extends {OutputTransformer<string>} 11 */ 12class JsonStringOutputTransformer extends OutputTransformer { 13 constructor() { 14 super(); 15 16 const base = this.base(); 17 18 /** @type {OutputManagerDeputy} */ 19 const manager = { 20 facets: { 21 ...base.facets, 22 collection: computed(() => { 23 const col = base.facets.collection(); 24 if (col.state !== "loaded") return col; 25 return { 26 state: "loaded", 27 data: typeof col.data === "string" ? parseArray(col.data) : [], 28 }; 29 }), 30 save: async (newFacets) => { 31 const json = JSON.stringify(newFacets); 32 await base.facets.save(json); 33 }, 34 }, 35 playlistItems: { 36 ...base.playlistItems, 37 collection: computed(() => { 38 const col = base.playlistItems.collection(); 39 if (col.state !== "loaded") return col; 40 return { 41 state: "loaded", 42 data: typeof col.data === "string" ? parseArray(col.data) : [], 43 }; 44 }), 45 save: async (newPlaylistItems) => { 46 const json = JSON.stringify(newPlaylistItems); 47 await base.playlistItems.save(json); 48 }, 49 }, 50 settings: { 51 ...base.settings, 52 collection: computed(() => { 53 const col = base.settings.collection(); 54 if (col.state !== "loaded") return col; 55 return { 56 state: "loaded", 57 data: typeof col.data === "string" ? parseArray(col.data) : [], 58 }; 59 }), 60 save: async (newSettings) => { 61 const json = JSON.stringify(newSettings); 62 await base.settings.save(json); 63 }, 64 }, 65 tracks: { 66 ...base.tracks, 67 collection: computed(() => { 68 const col = base.tracks.collection(); 69 if (col.state !== "loaded") return col; 70 return { 71 state: "loaded", 72 data: typeof col.data === "string" ? parseArray(col.data) : [], 73 }; 74 }), 75 save: async (newTracks) => { 76 const json = JSON.stringify(newTracks); 77 await base.tracks.save(json); 78 }, 79 }, 80 81 // Other 82 ready: base.ready, 83 }; 84 85 // Assign manager properties to class 86 this.facets = manager.facets; 87 this.playlistItems = manager.playlistItems; 88 this.settings = manager.settings; 89 this.tracks = manager.tracks; 90 this.ready = manager.ready; 91 } 92} 93 94/** 95 * @param {string} json 96 */ 97function parseArray(json) { 98 try { 99 return JSON.parse(json); 100 } catch (err) { 101 console.error(err); 102 return []; 103 } 104} 105 106export default JsonStringOutputTransformer; 107 108//////////////////////////////////////////// 109// REGISTER 110//////////////////////////////////////////// 111 112export const CLASS = JsonStringOutputTransformer; 113export const NAME = "dtos-json"; 114 115defineElement(NAME, CLASS);