Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

VC周り #306

Merged
merged 14 commits into from
Dec 4, 2023
Prev Previous commit
Next Next commit
Singleton実現方法変える
  • Loading branch information
na2na-p committed Dec 4, 2023
commit 9f1ccafd785ce2448b69401a1a4019a2bc92457b
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Voice } from '@/features/core/index.js';
import { LogicException } from '@/features/others/Error/LogicException.js';
import { singleton } from '@/features/others/singleton/index.js';

import { VoiceChannelCommandOptions } from './VoiceChannel.constants.js';
import type { InteractArgs } from '../../CommandBase/index.js';
@@ -12,7 +13,9 @@ export class VoiceChannel extends CommandBase {

public override async interact({ interaction }: InteractArgs): Promise<void> {
const subcommand = interaction.options.getSubcommand();
const voice = Voice.getInstance();
const createVoiceInstance = () => new Voice();
const getVoiceInstance = singleton(createVoiceInstance);
const voice = getVoiceInstance();

switch (subcommand) {
case VoiceChannelCommandOptions.join.name:
14 changes: 0 additions & 14 deletions src/features/core/internal/Voice/internal/Voice.class.ts
Original file line number Diff line number Diff line change
@@ -11,25 +11,11 @@ import {
} from '@/features/library/index.js';
import { getInteractionMemberId } from '@/features/others/discord/index.js';

/**
* Singleton class for voice.
* NOTE: シングルトンの影響で複数サーバにしたときのテストしないと困るかも
*/
export class Voice {
static #instance: Voice | null = null;
#connection: Array<{
guildId: string;
connection: VoiceConnection;
}> = [];
private constructor() {}

public static getInstance(): Voice {
if (Voice.#instance === null) {
Voice.#instance = new Voice();
}

return Voice.#instance;
}

public async join({
interaction,
1 change: 1 addition & 0 deletions src/features/others/singleton/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { singleton } from './internal/singleton.func.js';
9 changes: 9 additions & 0 deletions src/features/others/singleton/internal/singleton.func.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type Getter<T> = () => T;
type Factory<T> = () => T;

export const singleton = <T>(factory: Factory<T>): Getter<T> =>
((): Getter<T> => {
let memo: T | null = null;

return () => (memo ? memo : (memo = factory()));
})();
19 changes: 19 additions & 0 deletions src/features/others/singleton/internal/singleton.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { singleton } from './singleton.func.js';

it('not identical', () => {
const factory = () => ({});

const x = factory();
const y = factory();

expect(x).not.toBe(y);
});
it('with singleton, identical', () => {
const factory = () => ({});
const getter = singleton(factory);

const x = getter();
const y = getter();

expect(x).toBe(y);
});