-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CommandContext translator to interface adaptor.
We might want to make the translation function async i'm not sure yet, it depends if we need it once integration into draupnir progresses a little more.
- Loading branch information
Showing
3 changed files
with
84 additions
and
1 deletion.
There are no files selected for viewing
75 changes: 75 additions & 0 deletions
75
src/MatrixInterfaceAdaptor/AdaptorToCommandContextTranslator.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,75 @@ | ||
// SPDX-FileCopyrightText: 2024 Gnuxie <Gnuxie@protonmail.com> | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
// SPDX-FileAttributionText: <text> | ||
// This modified file incorporates work from @the-draupnir-project/interface-manager | ||
// https://github.com/the-draupnir-project/interface-manager | ||
// </text> | ||
|
||
import { CommandDescription } from "../Command"; | ||
|
||
/** | ||
* This is used to add clue code to take what is essentially a god context into a more specific | ||
* attenuated one that can be unit tested easily. | ||
* So basically, rather than giving a command the entirity of Draupnir, we can | ||
* give it juts the capability to ban a user. Which simplifies test setup. | ||
*/ | ||
export type AdaptorToCommandContextTranslationFunction< | ||
AdaptorContext, | ||
CommandContext, | ||
> = (adaptorContext: AdaptorContext) => CommandContext; | ||
|
||
export interface AdaptorToCommandContextTranslator<AdaptorContext> { | ||
translateContext<CommandContext>( | ||
commandDescription: CommandDescription<CommandContext>, | ||
adaptorContext: AdaptorContext | ||
): CommandContext; | ||
registerTranslation<CommandContext>( | ||
commandDescription: CommandDescription<CommandContext>, | ||
translationFunction: AdaptorToCommandContextTranslationFunction< | ||
AdaptorContext, | ||
CommandContext | ||
> | ||
): AdaptorToCommandContextTranslator<AdaptorContext>; | ||
} | ||
|
||
export class StandardAdaptorToCommandContextTranslator<AdaptorContext> | ||
implements AdaptorToCommandContextTranslator<AdaptorContext> | ||
{ | ||
private readonly translators = new Map< | ||
CommandDescription, | ||
AdaptorToCommandContextTranslationFunction<AdaptorContext, unknown> | ||
>(); | ||
translateContext<CommandContext>( | ||
commandDescription: CommandDescription<CommandContext>, | ||
adaptorContext: AdaptorContext | ||
): CommandContext { | ||
const entry = this.translators.get( | ||
commandDescription as CommandDescription | ||
); | ||
if (entry === undefined) { | ||
return adaptorContext as unknown as CommandContext; | ||
} else { | ||
return entry(adaptorContext) as CommandContext; | ||
} | ||
} | ||
registerTranslation<CommandContext>( | ||
commandDescription: CommandDescription<CommandContext>, | ||
translationFunction: AdaptorToCommandContextTranslationFunction< | ||
AdaptorContext, | ||
CommandContext | ||
> | ||
): AdaptorToCommandContextTranslator<AdaptorContext> { | ||
if (this.translators.has(commandDescription as CommandDescription)) { | ||
throw new TypeError( | ||
`There is already a translation function registered for the command ${commandDescription.summary}` | ||
); | ||
} | ||
this.translators.set( | ||
commandDescription as CommandDescription, | ||
translationFunction | ||
); | ||
return this; | ||
} | ||
} |
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