Skip to content

Commit

Permalink
fix(player): watch airplay availability
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Jan 31, 2024
1 parent d2031ca commit f7bfe28
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions packages/vidstack/src/providers/video/remote-playback.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { effect, signal } from 'maverick.js';
import { listenEvent } from 'maverick.js/std';

import type { MediaContext } from '../../core/api/media-context';
Expand All @@ -9,27 +10,43 @@ export abstract class VideoRemotePlaybackAdapter implements MediaRemotePlaybackA
protected abstract readonly _canPrompt: boolean;

protected _state?: RemotePlaybackState;
protected _supported?: boolean;
protected _supported = signal(false);

get supported() {
return !__SERVER__ && (this._supported ??= !!this._video.remote && this._canPrompt);
return this._supported();
}

constructor(
protected _video: HTMLVideoElement,
protected _ctx: MediaContext,
) {
if (!this.supported) return;
this._setup();
}

private _setup() {
if (__SERVER__ || !this._video.remote || !this._canPrompt) return;

this._video.remote
.watchAvailability((available) => {
this._supported.set(available);
})
.catch(() => {
this._supported.set(false);
});

effect(this._watchSupported.bind(this));
}

private _watchSupported() {
const events = ['connecting', 'connect', 'disconnect'],
onStateChange = this._onStateChange.bind(this);

onStateChange();
listenEvent(_video, 'playing', onStateChange);
listenEvent(this._video, 'playing', onStateChange);

for (const type of events) {
// @ts-expect-error - video remote not typed
listenEvent(_video.remote, type, onStateChange);
listenEvent(this._video.remote, type, onStateChange);
}
}

Expand Down

0 comments on commit f7bfe28

Please sign in to comment.