Skip to content

Commit

Permalink
Singleton実現方法変える
Browse files Browse the repository at this point in the history
  • Loading branch information
na2na-p committed Nov 15, 2023
1 parent 963eea6 commit 27fa621
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
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';
Expand All @@ -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:
Expand Down
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
Expand Up @@ -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,
Expand Down
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);
});

0 comments on commit 27fa621

Please sign in to comment.