|
1 | | -import { DynamicModule, Logger as NestCommonLogger, Module, OnApplicationBootstrap } from '@nestjs/common'; |
2 | | -import { ChannelConfig, InMemoryChannelConfig, MessagingModuleOptions } from './config'; |
| 1 | +import { |
| 2 | + DynamicModule, |
| 3 | + FactoryProvider, |
| 4 | + Logger as NestCommonLogger, |
| 5 | + Module, |
| 6 | + OnApplicationBootstrap, |
| 7 | + Provider, |
| 8 | +} from '@nestjs/common'; |
| 9 | +import { |
| 10 | + ChannelConfig, |
| 11 | + DefineChannels, |
| 12 | + InMemoryChannelConfig, |
| 13 | + MessagingModuleAsyncOptions, |
| 14 | + MessagingModuleOptions, MandatoryMessagingModuleOptions, |
| 15 | +} from './config'; |
3 | 16 | import { Service } from './dependency-injection/service'; |
4 | 17 | import { CompositeChannelFactory } from './channel/factory/composite-channel.factory'; |
5 | 18 | import { ChannelRegistry } from './channel/channel.registry'; |
@@ -31,23 +44,50 @@ import { ExceptionListenerHandler } from './exception-listener/exception-listene |
31 | 44 |
|
32 | 45 | @Module({}) |
33 | 46 | export class MessagingModule implements OnApplicationBootstrap { |
| 47 | + |
34 | 48 | static forRoot(options: MessagingModuleOptions): DynamicModule { |
35 | | - const buses = options.buses ?? []; |
36 | 49 | const channels = options.channels ?? []; |
37 | 50 |
|
38 | | - const registerChannels = (): any => { |
39 | | - return { |
| 51 | + const registerChannels: FactoryProvider = { |
40 | 52 | provide: Service.CHANNELS, |
41 | 53 | useFactory: (compositeChannelFactory: CompositeChannelFactory) => { |
42 | 54 | return channels.map((channelConfig: ChannelConfig) => |
43 | 55 | compositeChannelFactory.create(channelConfig), |
44 | 56 | ); |
45 | 57 | }, |
46 | 58 | inject: [CompositeChannelFactory], |
47 | | - }; |
48 | 59 | }; |
49 | 60 |
|
50 | | - const registerBuses = (): any[] => { |
| 61 | + return MessagingModule.createDynamicModule(options, [registerChannels]); |
| 62 | + } |
| 63 | + |
| 64 | + static forRootAsync(options: MessagingModuleAsyncOptions): DynamicModule { |
| 65 | + const registerAsyncConfig: FactoryProvider = { |
| 66 | + provide: Service.MESSAGING_MODULE_ASYNC_CHANNEL_OPTIONS, |
| 67 | + useFactory: options.useChannelFactory, |
| 68 | + inject: options.inject ?? [], |
| 69 | + }; |
| 70 | + |
| 71 | + const registerChannels: FactoryProvider = { |
| 72 | + provide: Service.CHANNELS, |
| 73 | + useFactory: ( |
| 74 | + compositeChannelFactory: CompositeChannelFactory, |
| 75 | + channels: DefineChannels, |
| 76 | + ) => { |
| 77 | + return (channels ?? []).map((channelConfig: ChannelConfig) => |
| 78 | + compositeChannelFactory.create(channelConfig), |
| 79 | + ); |
| 80 | + }, |
| 81 | + inject: [CompositeChannelFactory, Service.MESSAGING_MODULE_ASYNC_CHANNEL_OPTIONS], |
| 82 | + }; |
| 83 | + |
| 84 | + return MessagingModule.createDynamicModule(options, [registerAsyncConfig, registerChannels], options.imports ?? []); |
| 85 | + } |
| 86 | + |
| 87 | + private static createDynamicModule(options: MandatoryMessagingModuleOptions, providers: Provider[] = [], imports: any = []): DynamicModule { |
| 88 | + const buses = options.buses ?? []; |
| 89 | + |
| 90 | + const registerBuses = (): FactoryProvider[] => { |
51 | 91 | return buses.map((bus) => ({ |
52 | 92 | provide: `${bus.name}`, |
53 | 93 | useFactory: ( |
@@ -78,39 +118,43 @@ export class MessagingModule implements OnApplicationBootstrap { |
78 | 118 | })); |
79 | 119 | }; |
80 | 120 |
|
| 121 | + const defaultMessageBus = (): Provider => { |
| 122 | + return { |
| 123 | + provide: Service.DEFAULT_MESSAGE_BUS, |
| 124 | + useFactory: ( |
| 125 | + messageHandlerRegistry: MessageHandlerRegistry, |
| 126 | + middlewareRegistry: MiddlewareRegistry, |
| 127 | + normalizerRegistry: NormalizerRegistry, |
| 128 | + ) => { |
| 129 | + return new InMemoryMessageBus( |
| 130 | + messageHandlerRegistry, |
| 131 | + middlewareRegistry, |
| 132 | + new InMemoryChannel( |
| 133 | + new InMemoryChannelConfig({ |
| 134 | + name: 'default.bus', |
| 135 | + middlewares: [], |
| 136 | + avoidErrorsForNotExistedHandlers: true, |
| 137 | + }), |
| 138 | + ), |
| 139 | + normalizerRegistry, |
| 140 | + ); |
| 141 | + }, |
| 142 | + inject: [ |
| 143 | + Service.MESSAGE_HANDLERS_REGISTRY, |
| 144 | + Service.MIDDLEWARE_REGISTRY, |
| 145 | + Service.MESSAGE_NORMALIZERS_REGISTRY, |
| 146 | + ], |
| 147 | + }; |
| 148 | + }; |
| 149 | + |
81 | 150 | return { |
82 | 151 | global: options.global ?? true, |
83 | 152 | module: MessagingModule, |
84 | | - imports: [DiscoveryModule], |
| 153 | + imports: [DiscoveryModule, ...imports ], |
85 | 154 | providers: [ |
| 155 | + ...providers, |
| 156 | + defaultMessageBus(), |
86 | 157 | ...registerBuses(), |
87 | | - registerChannels(), |
88 | | - { |
89 | | - provide: Service.DEFAULT_MESSAGE_BUS, |
90 | | - useFactory: ( |
91 | | - messageHandlerRegistry: MessageHandlerRegistry, |
92 | | - middlewareRegistry: MiddlewareRegistry, |
93 | | - normalizerRegistry: NormalizerRegistry, |
94 | | - ) => { |
95 | | - return new InMemoryMessageBus( |
96 | | - messageHandlerRegistry, |
97 | | - middlewareRegistry, |
98 | | - new InMemoryChannel( |
99 | | - new InMemoryChannelConfig({ |
100 | | - name: 'default.bus', |
101 | | - middlewares: [], |
102 | | - avoidErrorsForNotExistedHandlers: true, |
103 | | - }), |
104 | | - ), |
105 | | - normalizerRegistry, |
106 | | - ); |
107 | | - }, |
108 | | - inject: [ |
109 | | - Service.MESSAGE_HANDLERS_REGISTRY, |
110 | | - Service.MIDDLEWARE_REGISTRY, |
111 | | - Service.MESSAGE_NORMALIZERS_REGISTRY, |
112 | | - ], |
113 | | - }, |
114 | 158 | { |
115 | 159 | provide: Service.MESSAGE_HANDLERS_REGISTRY, |
116 | 160 | useClass: MessageHandlerRegistry, |
@@ -156,7 +200,7 @@ export class MessagingModule implements OnApplicationBootstrap { |
156 | 200 | ], |
157 | 201 | exports: [ |
158 | 202 | Service.DEFAULT_MESSAGE_BUS, |
159 | | - ...registerBuses(), |
| 203 | + ...registerBuses().map(bus => bus.provide), |
160 | 204 | DistributedConsumer, |
161 | 205 | ObjectForwardMessageNormalizer, |
162 | 206 | ], |
|
0 commit comments