mirror of
				https://github.com/Maks1mS/nestjs-telegraf.git
				synced 2025-10-31 22:11:23 +03:00 
			
		
		
		
	feat: add GlobalUpdate decorator
This commit is contained in:
		
							
								
								
									
										9
									
								
								lib/decorators/core/global-update.decorator.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								lib/decorators/core/global-update.decorator.ts
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| import { SetMetadata } from '@nestjs/common'; | ||||
| import { GLOBAL_UPDATE_METADATA } from '../../telegraf.constants'; | ||||
|  | ||||
| /** | ||||
|  * `@GlobalUpdate` decorator, it's like `@Update` decorator, | ||||
|  * but processed before the scenes | ||||
|  */ | ||||
| export const GlobalUpdate = (): ClassDecorator => | ||||
|   SetMetadata(GLOBAL_UPDATE_METADATA, true); | ||||
| @@ -1,4 +1,5 @@ | ||||
| export * from './update.decorator'; | ||||
| export * from './global-update.decorator'; | ||||
| export * from './scene.decorator'; | ||||
| export * from './wizard.decorator'; | ||||
| export * from './inject-bot.decorator'; | ||||
|   | ||||
| @@ -48,8 +48,6 @@ export class ListenersExplorerService | ||||
|     this.bot = this.moduleRef.get<Telegraf<any>>(this.botName, { | ||||
|       strict: false, | ||||
|     }); | ||||
|     this.bot.use(this.stage.middleware()); | ||||
|  | ||||
|     this.explore(); | ||||
|   } | ||||
|  | ||||
| @@ -59,10 +57,23 @@ export class ListenersExplorerService | ||||
|       this.telegrafOptions.include || [], | ||||
|     ); | ||||
|  | ||||
|     this.registerGlobalUpdates(modules); | ||||
|  | ||||
|     this.bot.use(this.stage.middleware()); | ||||
|  | ||||
|     this.registerUpdates(modules); | ||||
|     this.registerScenes(modules); | ||||
|   } | ||||
|  | ||||
|   private registerGlobalUpdates(modules: Module[]): void { | ||||
|     const globalUpdates = this.flatMap<InstanceWrapper>(modules, (instance) => | ||||
|       this.filterGlobalUpdates(instance), | ||||
|     ); | ||||
|     globalUpdates.forEach((wrapper) => | ||||
|       this.registerListeners(this.bot, wrapper), | ||||
|     ); | ||||
|   } | ||||
|  | ||||
|   private registerUpdates(modules: Module[]): void { | ||||
|     const updates = this.flatMap<InstanceWrapper>(modules, (instance) => | ||||
|       this.filterUpdates(instance), | ||||
| @@ -92,6 +103,20 @@ export class ListenersExplorerService | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   private filterGlobalUpdates( | ||||
|     wrapper: InstanceWrapper, | ||||
|   ): InstanceWrapper<unknown> { | ||||
|     const { instance } = wrapper; | ||||
|     if (!instance) return undefined; | ||||
|  | ||||
|     const isGlobalUpdate = this.metadataAccessor.isGlobalUpdate( | ||||
|       wrapper.metatype, | ||||
|     ); | ||||
|     if (!isGlobalUpdate) return undefined; | ||||
|  | ||||
|     return wrapper; | ||||
|   } | ||||
|  | ||||
|   private filterUpdates(wrapper: InstanceWrapper): InstanceWrapper<unknown> { | ||||
|     const { instance } = wrapper; | ||||
|     if (!instance) return undefined; | ||||
|   | ||||
| @@ -3,6 +3,7 @@ import { Reflector } from '@nestjs/core'; | ||||
| import { | ||||
|   SCENE_METADATA, | ||||
|   LISTENERS_METADATA, | ||||
|   GLOBAL_UPDATE_METADATA, | ||||
|   UPDATE_METADATA, | ||||
|   WIZARD_STEP_METADATA, | ||||
| } from '../telegraf.constants'; | ||||
| @@ -16,6 +17,11 @@ import { | ||||
| export class MetadataAccessorService { | ||||
|   constructor(private readonly reflector: Reflector) {} | ||||
|  | ||||
|   isGlobalUpdate(target: Function): boolean { | ||||
|     if (!target) return false; | ||||
|     return !!this.reflector.get(GLOBAL_UPDATE_METADATA, target); | ||||
|   } | ||||
|  | ||||
|   isUpdate(target: Function): boolean { | ||||
|     if (!target) return false; | ||||
|     return !!this.reflector.get(UPDATE_METADATA, target); | ||||
|   | ||||
| @@ -5,6 +5,7 @@ export const TELEGRAF_BOT_NAME = 'TELEGRAF_BOT_NAME'; | ||||
| export const DEFAULT_BOT_NAME = 'DEFAULT_BOT_NAME'; | ||||
|  | ||||
| export const UPDATE_METADATA = 'UPDATE_METADATA'; | ||||
| export const GLOBAL_UPDATE_METADATA = 'GLOBAL_UPDATE_METADATA'; | ||||
| export const SCENE_METADATA = 'SCENE_METADATA'; | ||||
| export const LISTENERS_METADATA = 'LISTENERS_METADATA'; | ||||
| export const WIZARD_STEP_METADATA = 'WIZARD_STEP_METADATA'; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user