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

fix: sync audio item broadcasting lock with parent engine

+30 -13
+25 -12
src/common/element.js
··· 256 256 export class BroadcastableDiffuseElement extends DiffuseElement { 257 257 broadcasted = false; 258 258 259 + /** @type {{ assumeLeadership?: boolean }} */ 260 + #broadcastingOptions = {}; 261 + 259 262 #broadcastingStatus; 260 263 broadcastingStatus; 261 264 ··· 280 283 * @template {{ [K in keyof ActionsWithStrategy]: ActionsWithStrategy[K]["fn"] }} Actions 281 284 * @param {string} channelName 282 285 * @param {ActionsWithStrategy} actionsWithStrategy 286 + * @param {typeof this.#broadcastingOptions} [options] 283 287 */ 284 - broadcast(channelName, actionsWithStrategy) { 288 + broadcast(channelName, actionsWithStrategy, options) { 285 289 if (this.broadcasted) return; 290 + if (options) this.#broadcastingOptions = options; 286 291 287 292 const channel = new BroadcastChannel(channelName); 288 293 const msg = new MessageChannel(); ··· 395 400 396 401 if (!this.broadcasted) return; 397 402 398 - // Grab a lock if it isn't acquired yet, 403 + // Grab a lock if it isn't acquired yet and if needed, 399 404 // and hold it until `this.lock.promise` resolves. 400 - navigator.locks.request( 401 - `${this.channelName}/lock`, 402 - { ifAvailable: true }, 403 - (lock) => { 404 - this.#status.resolve( 405 - lock ? { leader: true, initialLeader: true } : { leader: false }, 406 - ); 407 - if (lock) return this.#lock.promise; 408 - }, 409 - ); 405 + const assumeLeadership = this.#broadcastingOptions?.assumeLeadership; 406 + 407 + if (assumeLeadership === undefined || assumeLeadership === true) { 408 + navigator.locks.request( 409 + `${this.channelName}/lock`, 410 + { ifAvailable: true }, 411 + (lock) => { 412 + this.#status.resolve( 413 + lock ? { leader: true, initialLeader: true } : { leader: false }, 414 + ); 415 + if (lock) return this.#lock.promise; 416 + }, 417 + ); 418 + } else { 419 + this.#status.resolve( 420 + { leader: false }, 421 + ); 422 + } 410 423 411 424 // When the lock status is initially determined, log its status. 412 425 // Additionally, wait for lock if needed.
+5 -1
src/components/engine/audio/element.js
··· 380 380 /** 381 381 * @override 382 382 */ 383 - connectedCallback() { 383 + async connectedCallback() { 384 384 const audio = this.audio; 385 385 386 386 audio.addEventListener("canplay", this.canplayEvent); ··· 430 430 fn: this.$state.loadingState.set, 431 431 }, 432 432 setProgress: { strategy: "replicate", fn: this.$state.progress.set }, 433 + }, 434 + { 435 + // Sync leadership with engine's broadcasting channel 436 + assumeLeadership: (await this.engine?.broadcastingStatus())?.leader, 433 437 }, 434 438 ); 435 439