Skip to content

Commit

Permalink
LastFM - Fix playback check issue & improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Inrixia committed Jun 21, 2024
1 parent 16d72d9 commit 29f204c
Show file tree
Hide file tree
Showing 12 changed files with 852 additions and 59 deletions.
5 changes: 2 additions & 3 deletions lib/Caches/AlbumCache.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { actions, store } from "@neptune";
import type { Album } from "neptune-types/tidal";
import { interceptPromise } from "../intercept/interceptPromise";
import { undefinedWarn } from "../undefinedError";

import { libTrace } from "../trace";
export class AlbumCache {
private static readonly _cache: Record<number, Album> = {};
public static async get(albumId?: number) {
Expand All @@ -17,7 +16,7 @@ export class AlbumCache {
if (this._cache[albumId] === undefined) {
const album = await interceptPromise(() => actions.content.loadAlbum({ albumId }), ["content/LOAD_ALBUM_SUCCESS"], [])
.then((res) => <Album>res?.[0].album)
.catch(undefinedWarn("AlbumCache.get"));
.catch(libTrace.warn.withContext("AlbumCache.get"));
if (album !== undefined) this._cache[albumId] = album;
}

Expand Down
8 changes: 4 additions & 4 deletions lib/Caches/ExtendedTrackItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { MusicBrainz } from "../musicbrainzApi";
import { Recording } from "../musicbrainzApi/types/Recording";
import { Release } from "../musicbrainzApi/types/UPCData";
import { TrackItemCache } from "./TrackItemCache";
import { undefinedWarn } from "../undefinedError";
import { AlbumCache } from "./AlbumCache";
import { libTrace } from "../trace";

export class ExtendedTrackItem {
public readonly trackId: ItemId;
Expand Down Expand Up @@ -48,14 +48,14 @@ export class ExtendedTrackItem {
public async recording(): Promise<Recording | undefined> {
if (this._recording !== undefined) return this._recording;

this._recording = await MusicBrainz.getRecording(this.trackItem()?.isrc).catch(undefinedWarn("MusicBrainz.getRecording"));
this._recording = await MusicBrainz.getRecording(this.trackItem()?.isrc).catch(libTrace.warn.withContext("MusicBrainz.getRecording"));
if (this._recording !== undefined) return this._recording;

const trackItem = this.trackItem();
if (trackItem === undefined) return undefined;

const releaseAlbum = await this.releaseAlbum();
const albumRelease = await MusicBrainz.getAlbumRelease(releaseAlbum?.id).catch(undefinedWarn("MusicBrainz.getAlbumRelease"));
const albumRelease = await MusicBrainz.getAlbumRelease(releaseAlbum?.id).catch(libTrace.warn.withContext("MusicBrainz.getAlbumRelease"));

const volumeNumber = (trackItem.volumeNumber ?? 1) - 1;
const trackNumber = (trackItem.trackNumber ?? 1) - 1;
Expand All @@ -66,7 +66,7 @@ export class ExtendedTrackItem {
if (this._releaseAlbum !== undefined) return this._releaseAlbum;

const album = await this.album();
const upcData = await MusicBrainz.getUPCData(album?.upc).catch(undefinedWarn("MusicBrainz.getUPCData"));
const upcData = await MusicBrainz.getUPCData(album?.upc).catch(libTrace.warn.withContext("MusicBrainz.getUPCData"));

return (this._releaseAlbum = upcData?.releases?.[0]);
}
Expand Down
15 changes: 0 additions & 15 deletions lib/messageLogging.ts

This file was deleted.

64 changes: 64 additions & 0 deletions lib/trace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { actions } from "@neptune";
import { Message, Severity } from "neptune-types/tidal";

type LoggerFunc = (...data: any[]) => void;
type MessengerFunc = (messageInfo: Message) => void;

type Logger<T extends LoggerFunc = LoggerFunc> = {
(...data: Parameters<T>): undefined;
withContext(context: string): (...data: Parameters<T>) => undefined;
};
type Messenger = {
(message: unknown): undefined;
withContext(context: string): (message: unknown) => undefined;
};

export const Tracer = (source: string) => {
const createLogger = <T extends LoggerFunc>(logger: T): Logger<T> => {
const _logger = (...data: Parameters<T>) => {
logger(source, ...data);
return undefined;
};
_logger.withContext =
(context: string) =>
(...data: Parameters<T>) => {
logger(context, ...data);
return undefined;
};
return _logger;
};

const log = createLogger(console.log);
const warn = createLogger(console.warn);
const err = createLogger(console.error);

const createMessager = (logger: Logger, messager: MessengerFunc, severity: Severity): Messenger => {
const _messager = (message: unknown) => {
logger(message);
messager({ message: `${source} - ${message}`, category: "OTHER", severity });
return undefined;
};
_messager.withContext = (context: string) => {
const loggerWithContext = logger.withContext(context);
return (message: unknown) => {
loggerWithContext(message);
if (message instanceof Error) message = message.message;
messager({ message: `${source}.${context} - ${message}`, category: "OTHER", severity });
return undefined;
};
};
return _messager;
};

return {
log,
warn,
err,
msg: {
log: createMessager(log, actions.message.messageInfo, "INFO"),
warn: createMessager(warn, actions.message.messageWarn, "WARN"),
err: createMessager(err, actions.message.messageError, "ERROR"),
},
};
};
export const libTrace = Tracer("[lib]");
5 changes: 0 additions & 5 deletions lib/undefinedError.ts

This file was deleted.

Loading

0 comments on commit 29f204c

Please sign in to comment.