Skip to content

Commit

Permalink
memoizeeeeeeeeee
Browse files Browse the repository at this point in the history
  • Loading branch information
RuiNtD committed Oct 19, 2024
1 parent 1ca97f7 commit f694a91
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
Binary file modified bun.lockb
Binary file not shown.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
},
"dependencies": {
"@inquirer/prompts": "^7.0.0",
"@types/memoizee": "^0.4.11",
"@typeschema/valibot": "^0.14.0",
"@xhayper/discord-rpc": "^1.2.0",
"axios": "^1.7.7",
"chalk": "^5.3.0",
"discord-api-types": "^0.37.102",
"luxon": "^3.5.0",
"memoize": "^10.0.0",
"memoizee": "^0.4.17",
"startup-run": "^0.0.10",
"valibot": "^0.42.1",
"yaml": "^2.6.0"
Expand Down
4 changes: 2 additions & 2 deletions src/listenProvider/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ export interface User {
url?: string;
}

export interface Provider {
export interface ListenProvider {
readonly name: string;
readonly logoAsset: string;

getListening(): Promise<Track | undefined | null>;
getUser(): Promise<User | undefined>;
}

function getProvider(): Provider {
function getProvider(): ListenProvider {
switch (config.provider) {
case "lastfm":
return LastFMProvider;
Expand Down
6 changes: 3 additions & 3 deletions src/listenProvider/lastFm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import chalk from "chalk";
import { getLogger } from "../logger.ts";
import * as v from "valibot";
import axios, { AxiosError } from "axios";
import memoize from "memoize";
import type { Provider, Track, User } from "./index.ts";
import memoize from "memoizee";
import type { ListenProvider, Track, User } from "./index.ts";
import * as Time from "../lib/time.ts";

const api = axios.create({
Expand Down Expand Up @@ -131,7 +131,7 @@ async function _getUser(): Promise<User | undefined> {
}
}

const LastFMProvider: Provider = {
const LastFMProvider: ListenProvider = {
name: "Last.fm",
logoAsset: "lastfm",

Expand Down
13 changes: 8 additions & 5 deletions src/listenProvider/listenbrainz.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import chalk from "chalk";
import { getLogger } from "../logger.ts";
import * as v from "valibot";
import axios, { AxiosError } from "axios";
import memoize from "memoize";
import type { Provider, Track } from "./index.ts";
import memoize from "memoizee";
import type { ListenProvider, Track } from "./index.ts";
import * as Time from "../lib/time.ts";

const api = axios.create({
Expand Down Expand Up @@ -64,7 +64,7 @@ async function _lookup(
return;
}
}
const lookupMetadata = memoize(_lookup);
const lookupMetadata = memoize(_lookup, { promise: true, maxAge: Time.Hour });

const LBPlayingAPI = v.object({
payload: v.object({
Expand Down Expand Up @@ -117,11 +117,14 @@ async function _getListening(): Promise<Track | undefined | null> {
}
}

const LBProvider: Provider = {
const LBProvider: ListenProvider = {
name: "ListenBrainz",
logoAsset: "listenbrainz",

getListening: memoize(_getListening, { maxAge: Time.Second * 5 }),
getListening: memoize(_getListening, {
promise: true,
maxAge: Time.Second * 5,
}),
async getUser() {
return {
name: username,
Expand Down

0 comments on commit f694a91

Please sign in to comment.