mirror of
https://github.com/Maks1mS/nestjs-telegraf.git
synced 2025-12-15 09:49:54 +03:00
refactor(): complex refactoring
This commit is contained in:
44
lib/decorators/telegram-action-handler.decorator.ts
Normal file
44
lib/decorators/telegram-action-handler.decorator.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { HandleParameters } from '../interfaces'
|
||||
|
||||
type Decorator = (
|
||||
params: HandleParameters,
|
||||
) => (target: any, propertyKey: string, descriptor: PropertyDescriptor) => void
|
||||
|
||||
type HandlerDecorator = Decorator & {
|
||||
handlers?: Map<any, Map<string, HandleParameters>>
|
||||
}
|
||||
|
||||
export const TelegramActionHandler: HandlerDecorator = (
|
||||
parameters: HandleParameters,
|
||||
) => (target: any, propertyKey: string) => {
|
||||
// eslint-disable-next-line no-use-before-define
|
||||
addHandlerToStore(target, propertyKey, parameters)
|
||||
}
|
||||
|
||||
export const addHandlerToStore = (
|
||||
instance: Object,
|
||||
name: string,
|
||||
config: HandleParameters,
|
||||
) => {
|
||||
const handlerClass = instance.constructor
|
||||
|
||||
if (!TelegramActionHandler.handlers) {
|
||||
TelegramActionHandler.handlers = new Map()
|
||||
}
|
||||
|
||||
if (!TelegramActionHandler.handlers.get(handlerClass)) {
|
||||
TelegramActionHandler.handlers.set(handlerClass, new Map())
|
||||
}
|
||||
|
||||
const oldParameters =
|
||||
TelegramActionHandler.handlers.get(handlerClass).get(name) || {}
|
||||
|
||||
TelegramActionHandler.handlers.get(handlerClass).set(name, {
|
||||
...oldParameters,
|
||||
...config,
|
||||
transformations: [
|
||||
...(oldParameters.transformations || []),
|
||||
...(config.transformations || []),
|
||||
],
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user