-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
133 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
import type { CommandBase } from '@/features/commands/index.js'; | ||
import type { getConfig } from '@/features/config/index.js'; | ||
import type { STORE_TYPES } from '@/features/core/index.js'; | ||
|
||
export type ClassConstructorArgs = { | ||
config: ReturnType<typeof getConfig>; | ||
commands: ReadonlyArray<CommandBase>; | ||
storeDriver: keyof typeof STORE_TYPES; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { singleton } from '@/features/others/singleton/index.js'; | ||
|
||
import { InMemoryStore } from './internal/InMemoryStore.class.js'; | ||
|
||
export type { Store } from './internal/Store.class.js'; | ||
export { STORE_TYPES } from './internal/Store.constants.js'; | ||
export type { InMemoryStore }; | ||
|
||
const createInMemoryStoreInstance = () => new InMemoryStore(); | ||
|
||
export const getInMemoryStoreInstance = singleton(createInMemoryStoreInstance); |
51 changes: 51 additions & 0 deletions
51
src/features/core/internal/Store/internal/InMemoryStore.class.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import type { Store } from '@/features/core/index.js'; | ||
import type { AudioPlayer, VoiceConnection } from '@/features/library/index.js'; | ||
|
||
import { InMemoryStore } from './InMemoryStore.class.js'; | ||
|
||
describe('InMemoryStore', () => { | ||
let store: Store; | ||
const guildId = 'guildId123'; | ||
const voiceConnection = {} as VoiceConnection; | ||
const audioPlayer = {} as AudioPlayer; | ||
|
||
beforeEach(() => { | ||
store = new InMemoryStore(); | ||
}); | ||
|
||
describe('getVoiceConnection', () => { | ||
it('should return undefined for a non-existing guildId', () => { | ||
expect(store.getVoiceConnection(guildId)).toBeUndefined(); | ||
}); | ||
|
||
it('should return the correct VoiceConnection for an existing guildId', () => { | ||
store.setVoiceConnection(guildId, voiceConnection); | ||
expect(store.getVoiceConnection(guildId)).toBe(voiceConnection); | ||
}); | ||
}); | ||
|
||
describe('setVoiceConnection', () => { | ||
it('should set the VoiceConnection for a guildId', () => { | ||
store.setVoiceConnection(guildId, voiceConnection); | ||
expect(store.getVoiceConnection(guildId)).toBe(voiceConnection); | ||
}); | ||
}); | ||
|
||
describe('getVoicePlayer', () => { | ||
it('should return undefined for a non-existing guildId', () => { | ||
expect(store.getVoicePlayer(guildId)).toBeUndefined(); | ||
}); | ||
|
||
it('should return the correct AudioPlayer for an existing guildId', () => { | ||
store.setVoicePlayer(guildId, audioPlayer); | ||
expect(store.getVoicePlayer(guildId)).toBe(audioPlayer); | ||
}); | ||
}); | ||
|
||
describe('setVoicePlayer', () => { | ||
it('should set the AudioPlayer for a guildId', () => { | ||
store.setVoicePlayer(guildId, audioPlayer); | ||
expect(store.getVoicePlayer(guildId)).toBe(audioPlayer); | ||
}); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
src/features/core/internal/Store/internal/InMemoryStore.class.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { | ||
AudioPlayer, | ||
Guild, | ||
VoiceConnection, | ||
} from '@/features/library/index.js'; | ||
|
||
import { Store } from './Store.class.js'; | ||
|
||
export type VoiceConnections = Map<Guild['id'], VoiceConnection>; | ||
export type VoicePlayers = Map<Guild['id'], AudioPlayer | undefined>; | ||
|
||
export class InMemoryStore extends Store { | ||
#voiceConnections: VoiceConnections = new Map(); | ||
#voicePlayers: VoicePlayers = new Map(); | ||
|
||
public getVoiceConnection(guildId: Guild['id']) { | ||
return this.#voiceConnections.get(guildId); | ||
} | ||
|
||
public setVoiceConnection(guildId: Guild['id'], connection: VoiceConnection) { | ||
this.#voiceConnections.set(guildId, connection); | ||
} | ||
|
||
public getVoicePlayer(guildId: Guild['id']) { | ||
return this.#voicePlayers.get(guildId); | ||
} | ||
|
||
public setVoicePlayer(guildId: Guild['id'], player: AudioPlayer) { | ||
this.#voicePlayers.set(guildId, player); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import type { AudioPlayer, VoiceConnection } from '@/features/library/index.js'; | ||
|
||
export abstract class Store { | ||
abstract getVoiceConnection(guildId: string): VoiceConnection | undefined; | ||
abstract setVoiceConnection( | ||
guildId: string, | ||
connection: VoiceConnection | ||
): void; | ||
abstract getVoicePlayer(guildId: string): AudioPlayer | undefined; | ||
abstract setVoicePlayer(guildId: string, player: AudioPlayer): void; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export const STORE_TYPES = { | ||
IN_MEMORY: 'IN_MEMORY', | ||
} as const satisfies Record<string, string>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters