Skip to content

Commit

Permalink
Refactor player status observable to be more testable
Browse files Browse the repository at this point in the history
  • Loading branch information
jliuhtonen committed Sep 25, 2024
1 parent f0bf5ee commit 13770d2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 30 deletions.
43 changes: 22 additions & 21 deletions src/bluOs/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { xml2js } from "xml-js"
import * as zod from "zod"
import { Logger } from "pino"
import { createEtagCache } from "./etagCache.js"
import { discoverPlayersObservable, Player } from "./serviceDiscovery.js"
import { Player } from "./serviceDiscovery.js"

const xmlTextField = zod.object({
_text: zod.string(),
Expand Down Expand Up @@ -155,26 +155,7 @@ const parseBluOsStatus = (bluOsXml: string): StatusQueryResponse => {
}
}

export const createDiscoveredPlayersStatusObservable = (
logger: Logger,
): Observable<StatusQueryResponse> => {
return discoverPlayersObservable().pipe(
tap((players: Player[]) => logger.debug({ players }, "Discovered players")),
mergeMap((players: Player[]) =>
merge(
...players.map((p) =>
createBluOsStatusObservable({
...p,
logger: logger.child({ component: "bluOS" }),
}),
),
),
),
share(),
)
}

export const createBluOsStatusObservable = ({
const createBluOsStatusObservable = ({
ip,
port,
logger,
Expand Down Expand Up @@ -210,3 +191,23 @@ export const createBluOsStatusObservable = ({

return bluOsStatus
}

export const createPlayersStatusObservable = (
logger: Logger,
playersObservable: Observable<Player[]>,
): Observable<StatusQueryResponse> => {
return playersObservable.pipe(
tap((players: Player[]) => logger.debug({ players }, "Discovered players")),
mergeMap((players: Player[]) =>
merge(
...players.map((p) =>
createBluOsStatusObservable({
...p,
logger: logger.child({ component: "bluOS" }),
}),
),
),
),
share(),
)
}
17 changes: 8 additions & 9 deletions src/scrobbler.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Observable, filter, map } from "rxjs"
import { Observable, filter, map, of } from "rxjs"
import { Logger } from "pino"
import {
createPlayersStatusObservable,
PlayingTrack,
StatusQueryResponse,
createBluOsStatusObservable,
createDiscoveredPlayersStatusObservable,
} from "./bluOs/player.js"
import {
SubmitScrobbleResult,
Expand All @@ -14,6 +13,7 @@ import {
} from "./submitTrack.js"
import { Configuration } from "./configuration.js"
import { LastFmApi } from "./lastFm.js"
import { discoverPlayersObservable } from "./bluOs/serviceDiscovery.js"

export interface ScrobblerDeps {
config: Configuration
Expand All @@ -33,12 +33,11 @@ export const createScrobbler = async ({
lastFm,
sessionToken,
}: ScrobblerDeps): Promise<ScrobblerOutput> => {
const bluOsStatus: Observable<StatusQueryResponse> = config.bluOs
? createBluOsStatusObservable({
...config.bluOs,
logger: logger.child({ component: "bluOS" }),
})
: createDiscoveredPlayersStatusObservable(logger)
const playersObservable = config.bluOs
? of([{ ...config.bluOs }])
: discoverPlayersObservable()
const bluOsStatus: Observable<StatusQueryResponse> =
createPlayersStatusObservable(logger, playersObservable)

const playingTrack = bluOsStatus.pipe(
map((s) => s.playingTrack),
Expand Down

0 comments on commit 13770d2

Please sign in to comment.