Skip to content

Commit

Permalink
feat(player-manager): add option deep merge
Browse files Browse the repository at this point in the history
  • Loading branch information
larsrickert committed May 24, 2022
1 parent 0458b8f commit b42ea27
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
38 changes: 21 additions & 17 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"ffmpeg-static": "^5.0.0"
},
"dependencies": {
"deepmerge-ts": "^4.0.3",
"libsodium-wrappers": "^0.7.10",
"play-dl": "^1.9.4",
"spotify-url-info": "^2.2.9"
Expand Down
14 changes: 9 additions & 5 deletions src/player-manager.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { deepmerge } from "deepmerge-ts";
import { Player } from "./player";
import { PlayerOptions } from "./types/player";

Expand All @@ -14,16 +15,19 @@ export class PlayerManager {
/**
* Gets an existing player for the given guildId or creates one if it does not exist.
*
* @param optionOverrides Option overrides for the guild player. Will be merged with options passed to the PlayerManager.
* @param optionOverrides Option overrides for the guild player. Will be deep merged with options passed to the PlayerManager.
*/
getPlayer(guildId: string, optionOverrides?: Partial<PlayerOptions>): Player {
const player = this.players.find((p) => p.guildId === guildId);
if (player) return player;

const newPlayer = new Player(guildId, {
...this.options,
...optionOverrides,
});
let options = this.options;
if (options && optionOverrides) {
options = deepmerge(options, optionOverrides);
}
if (!options && optionOverrides) options = optionOverrides;

const newPlayer = new Player(guildId, options);
this.players.push(newPlayer);
return newPlayer;
}
Expand Down

0 comments on commit b42ea27

Please sign in to comment.