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.

refactor: dasl sync

+46 -118
+1 -1
.env
··· 1 1 ATPROTO_CLIENT_ID=http://127.0.0.1:3000/oauth-client-metadata.json 2 - #DISABLE_AUTOMATIC_TRACKS_PROCESSING=t 2 + DISABLE_AUTOMATIC_TRACKS_PROCESSING=t
+1
src/components/orchestrator/process-tracks/element.js
··· 158 158 console.log("🪵 Processing initiated"); 159 159 160 160 const cachedTracks = this.output.tracks.collection(); 161 + console.log(cachedTracks); 161 162 const result = await this.#proxy.process(cachedTracks); 162 163 163 164 // Save if collection changed
+44 -117
src/components/transformer/output/bytes/dasl-sync/element.js
··· 5 5 import "@components/output/polymorphic/indexed-db/element.js"; 6 6 7 7 import * as CID from "@common/cid.js"; 8 - import { computed, signal, untracked } from "@common/signal.js"; 8 + import { computed, signal } from "@common/signal.js"; 9 9 import { compareTimestamps } from "@common/utils.js"; 10 10 import { OutputTransformer } from "../../base.js"; 11 11 import { IDB_PREFIX } from "./constants.js"; ··· 26 26 27 27 /** 28 28 * @template {{ id: string; updatedAt: string }} T 29 + * @param {string} kind 29 30 * @param {SignalReader<Uint8Array | undefined>} localCollection 30 31 * @param {SignalReader<Uint8Array | undefined>} remoteCollection 32 + * @param {SignalReader<"loading" | "loaded" | "sleeping">} remoteState 31 33 * @param {{ saveLocal: (bytes: Uint8Array) => void, saveRemote: (bytes: Uint8Array) => Promise<void> }} sync 32 34 */ 33 - const state = (localCollection, remoteCollection, sync) => { 35 + const state = ( 36 + kind, 37 + localCollection, 38 + remoteCollection, 39 + remoteState, 40 + sync, 41 + ) => { 34 42 /** 35 - * @typedef {{ container: Container<T> | { local: Container<T>; merged: { signal: SignalReader<Container<T> | undefined>; promise: Promise<Container<T>> } }; diverged: boolean; local: boolean; remote: boolean; }} State 43 + * @typedef {Container<T>} State 36 44 */ 37 45 38 - const sig = signal( 39 - /** @type {State} */ ({ 40 - container: { 41 - cid: undefined, 42 - data: [], 43 - inventory: { current: {}, removed: [] }, 44 - }, 45 - diverged: false, 46 - local: false, 47 - remote: false, 48 - }), 49 - { eager: true }, 50 - ); 51 - 52 46 /** @returns {State} */ 53 47 const determine = () => { 54 48 const lb = localCollection(); 55 49 const rb = remote.ready() ? remoteCollection() : undefined; 50 + const rs = remoteState(); 56 51 57 52 /** @type {Container<T> | undefined} */ 58 53 const l = lb ? decode(lb) : undefined; 59 54 60 55 /** @type {Container<T> | undefined} */ 61 - const r = rb ? decode(rb) : undefined; 56 + const r = rb && rs === "loaded" ? decode(rb) : undefined; 62 57 63 58 if (!r) { 64 - return l 65 - ? { 66 - container: l, 67 - diverged: remote.ready(), 68 - local: false, 69 - remote: remote.ready(), 59 + if (l) { 60 + if (remote.ready() && rs === "loaded") { 61 + const bytes = this.save(l); 62 + sync.saveRemote(bytes); 70 63 } 71 - : { 72 - container: { 73 - cid: undefined, 74 - data: [], 75 - inventory: { current: {}, removed: [] }, 76 - }, 77 - diverged: false, 78 - local: false, 79 - remote: false, 80 - }; 64 + 65 + return l; 66 + } 67 + 68 + return { 69 + cid: undefined, 70 + data: [], 71 + inventory: { current: {}, removed: [] }, 72 + }; 81 73 } else if (!l) { 82 - return { container: r, diverged: true, local: true, remote: false }; 74 + const bytes = this.save(r); 75 + sync.saveLocal(bytes); 76 + return r; 83 77 } 84 78 85 79 const diverged = this.hasDiverged({ local: l, remote: r }); 86 - const mergedSignal = signal( 87 - /** @type {Container<T> | undefined} */ (undefined), 88 - ); 89 - 90 - /** 91 - * @type {State["container"]} 92 - */ 93 - let container = r; 94 80 95 81 if (diverged.local || diverged.remote) { 96 - const promise = this.merge(l, r).then((c) => { 82 + this.merge(l, r).then((c) => { 97 83 console.log("Merged:", c); 98 84 const bytes = this.save(c); 99 85 if (diverged.local) sync.saveLocal(bytes); 100 86 if (diverged.remote) sync.saveRemote(bytes); 101 - mergedSignal.set(c); 102 - return c; 103 87 }); 104 - 105 - container = { 106 - local: l, 107 - merged: { promise, signal: mergedSignal.get }, 108 - }; 109 88 } 110 89 111 - return { 112 - container, 113 - diverged: diverged.local || diverged.remote, 114 - local: diverged.local, 115 - remote: diverged.remote, 116 - }; 90 + return l; 117 91 }; 118 92 119 - this.effect(() => { 120 - const result = determine(); 121 - const current = untracked(sig.get); 122 - 123 - const newCID = "merged" in result.container 124 - ? undefined // handle async case separately 125 - : result.container.cid; 126 - 127 - const currentCID = "merged" in current.container 128 - ? undefined 129 - : current.container.cid; 130 - 131 - // Skip if both are non-merged and CIDs match 132 - if ( 133 - newCID !== undefined && currentCID !== undefined && 134 - newCID === currentCID 135 - ) { 136 - return; 137 - } 138 - 139 - // For the non-merged common case, set synchronously 140 - if (!("merged" in result.container)) { 141 - sig.set(result); 142 - return; 143 - } 144 - 145 - // Only go async for the merge case 146 - result.container.merged.promise.then(async (merged) => { 147 - const cur = untracked(sig.get); 148 - const curCID = "merged" in cur.container 149 - ? (await cur.container.merged.promise).cid 150 - : cur.container.cid; 151 - if (merged.cid !== curCID) { 152 - sig.set(result); 153 - } 154 - }); 155 - }); 156 - 157 - return sig.get; 93 + return computed(determine); 158 94 }; 159 95 160 96 // Local ··· 167 103 168 104 // Container signals 169 105 const facets = state( 106 + "facets", 170 107 local.facets.get, 171 108 remote.facets.collection, 109 + remote.facets.state, 172 110 { 173 111 saveLocal: this.putLocalFn("facets", local.facets), 174 112 saveRemote: remote.facets.save, ··· 176 114 ); 177 115 178 116 const playlistItems = state( 117 + "playlistItems", 179 118 local.playlistItems.get, 180 119 remote.playlistItems.collection, 120 + remote.playlistItems.state, 181 121 { 182 122 saveLocal: this.putLocalFn("playlistItems", local.playlistItems), 183 123 saveRemote: remote.playlistItems.save, ··· 185 125 ); 186 126 187 127 const themes = state( 128 + "themes", 188 129 local.themes.get, 189 130 remote.themes.collection, 131 + remote.themes.state, 190 132 { 191 133 saveLocal: this.putLocalFn("themes", local.themes), 192 134 saveRemote: remote.themes.save, ··· 194 136 ); 195 137 196 138 const tracks = state( 139 + "tracks", 197 140 local.tracks.get, 198 141 remote.tracks.collection, 142 + remote.tracks.state, 199 143 { 200 144 saveLocal: this.putLocalFn("tracks", local.tracks), 201 145 saveRemote: remote.tracks.save, ··· 414 358 * @template {{ id: string; updatedAt: string }} T 415 359 * @param {{ save: (bytes: Uint8Array) => Promise<void> | void }} local 416 360 * @param {{ collection: SignalReader<Uint8Array | undefined>, reload: () => Promise<void>, save: (bytes: Uint8Array) => Promise<void>, state: SignalReader<"loading" | "loaded" | "sleeping"> }} remote 417 - * @param {SignalReader<{ container: Container<T> | { local: Container<T>; merged: { signal: SignalReader<Container<T> | undefined>; promise: Promise<Container<T>> } }}>} container 361 + * @param {SignalReader<Container<T>>} container 418 362 * @returns {{ collection: SignalReader<T[]>, reload: () => Promise<void>, save: (items: T[]) => Promise<void>, state: SignalReader<"loading" | "loaded" | "sleeping"> }} 419 363 */ 420 364 managerProp(local, remote, container) { 421 365 return { 422 366 collection: computed(() => { 423 - const c = container().container; 424 - 425 - if ("merged" in c) { 426 - return c.merged.signal()?.data ?? c.local?.data; 427 - } 428 - 429 - return c.data; 367 + return container().data; 430 368 }), 431 369 reload: remote.reload, 432 370 save: async (/** @type {T[]} */ newItems) => { 433 - let c = container().container; 434 - 435 - if ("merged" in c) { 436 - c = await c.merged.promise; 437 - } 438 - 439 371 const adjustedContainer = await this.updateContainer({ 440 372 collection: newItems, 441 - previous: c, 373 + previous: container(), 442 374 }); 443 375 444 376 console.log("Save:", newItems); ··· 448 380 await local.save(bytes); 449 381 }, 450 382 state: computed(() => { 451 - const c = container().container; 452 - 453 - /** @type {Container<T> | undefined} */ 454 - const cont = "merged" in c ? c.merged.signal() : c; 455 - 456 - if (cont?.cid) return "loaded"; 383 + if (container().cid) return "loaded"; 457 384 return "loading"; 458 385 }), 459 386 };