Skip to content

Commit

Permalink
refactor window.playlist into playbackmanager and make playbackmanage…
Browse files Browse the repository at this point in the history
…r static
  • Loading branch information
hawken93 committed Jan 14, 2021
1 parent 85a8b37 commit 7edbd29
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 121 deletions.
2 changes: 0 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,4 @@ import './css/jellyfin.css';

window.mediaElement = document.getElementById('video-player');

window.playlist = [];
window.currentPlaylistIndex = -1;
window.repeatMode = RepeatMode.RepeatNone;
25 changes: 7 additions & 18 deletions src/components/commandHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@ import {

import { reportPlaybackProgress } from './jellyfinActions';

import { playbackManager } from './playbackManager';
import { PlaybackManager } from './playbackManager';

import { DocumentManager } from './documentManager';

export abstract class CommandHandler {
private static playerManager: cast.framework.PlayerManager;
private static playbackManager: playbackManager;
private static supportedCommands: SupportedCommands = {
PlayNext: CommandHandler.playNextHandler,
PlayNow: CommandHandler.playNowHandler,
Expand All @@ -52,12 +51,8 @@ export abstract class CommandHandler {
Unpause: CommandHandler.UnpauseHandler
};

static configure(
playerManager: cast.framework.PlayerManager,
playbackManager: playbackManager
): void {
static configure(playerManager: cast.framework.PlayerManager): void {
this.playerManager = playerManager;
this.playbackManager = playbackManager;
}

static playNextHandler(data: DataMessage): void {
Expand Down Expand Up @@ -89,24 +84,18 @@ export abstract class CommandHandler {
}

static displayContentHandler(data: DataMessage): void {
if (!this.playbackManager.isPlaying()) {
if (!PlaybackManager.isPlaying()) {
DocumentManager.showItemId((<DisplayRequest>data.options).ItemId);
}
}

static nextTrackHandler(): void {
if (
window.playlist &&
window.currentPlaylistIndex < window.playlist.length - 1
) {
this.playbackManager.playNextItem({}, true);
}
if (PlaybackManager.hasNextItem())
PlaybackManager.playNextItem({}, true);
}

static previousTrackHandler(): void {
if (window.playlist && window.currentPlaylistIndex > 0) {
this.playbackManager.playPreviousItem({});
}
if (PlaybackManager.hasPrevItem()) PlaybackManager.playPreviousItem({});
}

static setAudioStreamIndexHandler(data: DataMessage): void {
Expand Down Expand Up @@ -138,7 +127,7 @@ export abstract class CommandHandler {
}

static IdentifyHandler(): void {
if (!this.playbackManager.isPlaying()) {
if (!PlaybackManager.isPlaying()) {
DocumentManager.startBackdropInterval();
} else {
// When a client connects send back the initial device state (volume etc) via a playbackstop message
Expand Down
27 changes: 13 additions & 14 deletions src/components/maincontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
} from './jellyfinActions';
import { getDeviceProfile } from './deviceprofileBuilder';
import { JellyfinApi } from './jellyfinApi';
import { playbackManager } from './playbackManager';
import { PlaybackManager } from './playbackManager';
import { CommandHandler } from './commandHandler';
import { getMaxBitrateSupport } from './codecSupportHelper';
import { DocumentManager } from './documentManager';
Expand All @@ -34,9 +34,9 @@ import { GlobalScope, PlayRequest } from '~/types/global';
window.castReceiverContext = cast.framework.CastReceiverContext.getInstance();
window.playerManager = window.castReceiverContext.getPlayerManager();

const playbackMgr = new playbackManager(window.playerManager);
PlaybackManager.setPlayerManager(window.playerManager);

CommandHandler.configure(window.playerManager, playbackMgr);
CommandHandler.configure(window.playerManager);

resetPlaybackScope($scope);

Expand Down Expand Up @@ -146,7 +146,7 @@ window.playerManager.addEventListener(
);

function defaultOnStopped(): void {
playbackMgr.onStopped(true);
PlaybackManager.onStopped();
}

window.playerManager.addEventListener(
Expand All @@ -168,10 +168,9 @@ window.playerManager.addEventListener(
reportPlaybackStopped($scope, getReportingParams($scope));
resetPlaybackScope($scope);

if (!playbackMgr.playNextItem()) {
window.playlist = [];
window.currentPlaylistIndex = -1;
DocumentManager.startBackdropInterval();
if (!PlaybackManager.playNextItem()) {
PlaybackManager.resetPlaylist();
PlaybackManager.onStopped();
}
}
);
Expand Down Expand Up @@ -403,7 +402,7 @@ export async function changeStream(
// await stopActiveEncodings($scope.playSessionId);
//}

return await playbackMgr.playItemInternal($scope.item, {
return await PlaybackManager.playItemInternal($scope.item, {
audioStreamIndex:
params.AudioStreamIndex == null
? $scope.audioStreamIndex
Expand Down Expand Up @@ -458,10 +457,10 @@ export function translateItems(
i < length;
i++
) {
window.playlist.push(options.items[i]);
PlaybackManager.enqueue(options.items[i]);
}
} else {
playbackMgr.playFromOptions(data.options);
PlaybackManager.playFromOptions(data.options);
}
}
);
Expand All @@ -474,7 +473,7 @@ export function instantMix(
): Promise<void> {
return getInstantMixItems(data.userId, item).then(function (result) {
options.items = result.Items;
playbackMgr.playFromOptions(data.options);
PlaybackManager.playFromOptions(data.options);
});
}

Expand All @@ -485,7 +484,7 @@ export function shuffle(
): Promise<void> {
return getShuffleItems(data.userId, item).then(function (result) {
options.items = result.Items;
playbackMgr.playFromOptions(data.options);
PlaybackManager.playFromOptions(data.options);
});
}

Expand All @@ -508,7 +507,7 @@ export function onStopPlayerBeforePlaybackDone(
dataType: 'json',
type: 'GET'
}).then(
(data) => playbackMgr.playItemInternal(data, options),
(data) => PlaybackManager.playItemInternal(data, options),
broadcastConnectionErrorMessage
);
}
Expand Down
Loading

0 comments on commit 7edbd29

Please sign in to comment.