@zdavison/matador 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (256) hide show
  1. package/cli.ts +453 -0
  2. package/dist/checkpoint/context.d.ts +59 -0
  3. package/dist/checkpoint/context.d.ts.map +1 -0
  4. package/dist/checkpoint/context.js +140 -0
  5. package/dist/checkpoint/context.test.d.ts +2 -0
  6. package/dist/checkpoint/context.test.d.ts.map +1 -0
  7. package/dist/checkpoint/context.test.js +424 -0
  8. package/dist/checkpoint/index.d.ts +7 -0
  9. package/dist/checkpoint/index.d.ts.map +1 -0
  10. package/dist/checkpoint/index.js +6 -0
  11. package/dist/checkpoint/stores/memory.d.ts +29 -0
  12. package/dist/checkpoint/stores/memory.d.ts.map +1 -0
  13. package/dist/checkpoint/stores/memory.js +39 -0
  14. package/dist/checkpoint/stores/noop.d.ts +14 -0
  15. package/dist/checkpoint/stores/noop.d.ts.map +1 -0
  16. package/dist/checkpoint/stores/noop.js +18 -0
  17. package/dist/checkpoint/stores/stores.test.d.ts +2 -0
  18. package/dist/checkpoint/stores/stores.test.d.ts.map +1 -0
  19. package/dist/checkpoint/stores/stores.test.js +146 -0
  20. package/dist/checkpoint/types.d.ts +119 -0
  21. package/dist/checkpoint/types.d.ts.map +1 -0
  22. package/dist/checkpoint/types.js +1 -0
  23. package/dist/codec/codec.d.ts +29 -0
  24. package/dist/codec/codec.d.ts.map +1 -0
  25. package/dist/codec/codec.js +15 -0
  26. package/dist/codec/header-aware-codec.d.ts +36 -0
  27. package/dist/codec/header-aware-codec.d.ts.map +1 -0
  28. package/dist/codec/header-aware-codec.js +1 -0
  29. package/dist/codec/index.d.ts +6 -0
  30. package/dist/codec/index.d.ts.map +1 -0
  31. package/dist/codec/index.js +3 -0
  32. package/dist/codec/json-codec.d.ts +13 -0
  33. package/dist/codec/json-codec.d.ts.map +1 -0
  34. package/dist/codec/json-codec.js +64 -0
  35. package/dist/codec/rabbitmq-codec.d.ts +28 -0
  36. package/dist/codec/rabbitmq-codec.d.ts.map +1 -0
  37. package/dist/codec/rabbitmq-codec.js +242 -0
  38. package/dist/codec/rabbitmq-codec.test.d.ts +2 -0
  39. package/dist/codec/rabbitmq-codec.test.d.ts.map +1 -0
  40. package/dist/codec/rabbitmq-codec.test.js +433 -0
  41. package/dist/core/fanout.d.ts +59 -0
  42. package/dist/core/fanout.d.ts.map +1 -0
  43. package/dist/core/fanout.js +121 -0
  44. package/dist/core/fanout.test.d.ts +2 -0
  45. package/dist/core/fanout.test.d.ts.map +1 -0
  46. package/dist/core/fanout.test.js +1055 -0
  47. package/dist/core/index.d.ts +7 -0
  48. package/dist/core/index.d.ts.map +1 -0
  49. package/dist/core/index.js +3 -0
  50. package/dist/core/matador.d.ts +133 -0
  51. package/dist/core/matador.d.ts.map +1 -0
  52. package/dist/core/matador.js +222 -0
  53. package/dist/core/matador.test.d.ts +2 -0
  54. package/dist/core/matador.test.d.ts.map +1 -0
  55. package/dist/core/matador.test.js +446 -0
  56. package/dist/core/shutdown.d.ts +78 -0
  57. package/dist/core/shutdown.d.ts.map +1 -0
  58. package/dist/core/shutdown.js +111 -0
  59. package/dist/core/shutdown.test.d.ts +2 -0
  60. package/dist/core/shutdown.test.d.ts.map +1 -0
  61. package/dist/core/shutdown.test.js +524 -0
  62. package/dist/errors/checkpoint-errors.d.ts +30 -0
  63. package/dist/errors/checkpoint-errors.d.ts.map +1 -0
  64. package/dist/errors/checkpoint-errors.js +49 -0
  65. package/dist/errors/has-description.d.ts +18 -0
  66. package/dist/errors/has-description.d.ts.map +1 -0
  67. package/dist/errors/has-description.js +9 -0
  68. package/dist/errors/index.d.ts +6 -0
  69. package/dist/errors/index.d.ts.map +1 -0
  70. package/dist/errors/index.js +22 -0
  71. package/dist/errors/matador-errors.d.ts +183 -0
  72. package/dist/errors/matador-errors.d.ts.map +1 -0
  73. package/dist/errors/matador-errors.js +376 -0
  74. package/dist/errors/retry-errors.d.ts +67 -0
  75. package/dist/errors/retry-errors.d.ts.map +1 -0
  76. package/dist/errors/retry-errors.js +108 -0
  77. package/dist/hooks/index.d.ts +4 -0
  78. package/dist/hooks/index.d.ts.map +1 -0
  79. package/dist/hooks/index.js +2 -0
  80. package/dist/hooks/safe-hooks.d.ts +34 -0
  81. package/dist/hooks/safe-hooks.d.ts.map +1 -0
  82. package/dist/hooks/safe-hooks.js +135 -0
  83. package/dist/hooks/types.d.ts +189 -0
  84. package/dist/hooks/types.d.ts.map +1 -0
  85. package/dist/hooks/types.js +9 -0
  86. package/dist/index.cjs +3156 -0
  87. package/dist/index.cjs.map +1 -0
  88. package/dist/index.d.cts +23 -0
  89. package/dist/index.d.ts +23 -0
  90. package/dist/index.d.ts.map +1 -0
  91. package/dist/index.js +31 -0
  92. package/dist/index.js.map +1 -0
  93. package/dist/pipeline/index.d.ts +3 -0
  94. package/dist/pipeline/index.d.ts.map +1 -0
  95. package/dist/pipeline/index.js +1 -0
  96. package/dist/pipeline/pipeline.d.ts +60 -0
  97. package/dist/pipeline/pipeline.d.ts.map +1 -0
  98. package/dist/pipeline/pipeline.js +261 -0
  99. package/dist/pipeline/pipeline.test.d.ts +2 -0
  100. package/dist/pipeline/pipeline.test.d.ts.map +1 -0
  101. package/dist/pipeline/pipeline.test.js +1065 -0
  102. package/dist/retry/index.d.ts +4 -0
  103. package/dist/retry/index.d.ts.map +1 -0
  104. package/dist/retry/index.js +1 -0
  105. package/dist/retry/policy.d.ts +43 -0
  106. package/dist/retry/policy.d.ts.map +1 -0
  107. package/dist/retry/policy.js +1 -0
  108. package/dist/retry/standard-policy.d.ts +44 -0
  109. package/dist/retry/standard-policy.d.ts.map +1 -0
  110. package/dist/retry/standard-policy.js +102 -0
  111. package/dist/retry/standard-policy.test.d.ts +2 -0
  112. package/dist/retry/standard-policy.test.d.ts.map +1 -0
  113. package/dist/retry/standard-policy.test.js +190 -0
  114. package/dist/schema/index.d.ts +4 -0
  115. package/dist/schema/index.d.ts.map +1 -0
  116. package/dist/schema/index.js +2 -0
  117. package/dist/schema/registry.d.ts +63 -0
  118. package/dist/schema/registry.d.ts.map +1 -0
  119. package/dist/schema/registry.js +171 -0
  120. package/dist/schema/registry.test.d.ts +2 -0
  121. package/dist/schema/registry.test.d.ts.map +1 -0
  122. package/dist/schema/registry.test.js +278 -0
  123. package/dist/schema/types.d.ts +158 -0
  124. package/dist/schema/types.d.ts.map +1 -0
  125. package/dist/schema/types.js +74 -0
  126. package/dist/schema/types.test.d.ts +2 -0
  127. package/dist/schema/types.test.d.ts.map +1 -0
  128. package/dist/schema/types.test.js +243 -0
  129. package/dist/topology/builder.d.ts +66 -0
  130. package/dist/topology/builder.d.ts.map +1 -0
  131. package/dist/topology/builder.js +156 -0
  132. package/dist/topology/builder.test.d.ts +2 -0
  133. package/dist/topology/builder.test.d.ts.map +1 -0
  134. package/dist/topology/builder.test.js +222 -0
  135. package/dist/topology/index.d.ts +5 -0
  136. package/dist/topology/index.d.ts.map +1 -0
  137. package/dist/topology/index.js +2 -0
  138. package/dist/topology/types.d.ts +76 -0
  139. package/dist/topology/types.d.ts.map +1 -0
  140. package/dist/topology/types.js +18 -0
  141. package/dist/transport/capabilities.d.ts +66 -0
  142. package/dist/transport/capabilities.d.ts.map +1 -0
  143. package/dist/transport/capabilities.js +18 -0
  144. package/dist/transport/connection-manager.d.ts +95 -0
  145. package/dist/transport/connection-manager.d.ts.map +1 -0
  146. package/dist/transport/connection-manager.js +144 -0
  147. package/dist/transport/index.d.ts +11 -0
  148. package/dist/transport/index.d.ts.map +1 -0
  149. package/dist/transport/index.js +5 -0
  150. package/dist/transport/local/local-transport.d.ts +62 -0
  151. package/dist/transport/local/local-transport.d.ts.map +1 -0
  152. package/dist/transport/local/local-transport.js +241 -0
  153. package/dist/transport/local/local-transport.test.d.ts +2 -0
  154. package/dist/transport/local/local-transport.test.d.ts.map +1 -0
  155. package/dist/transport/local/local-transport.test.js +192 -0
  156. package/dist/transport/multi/multi-transport.d.ts +94 -0
  157. package/dist/transport/multi/multi-transport.d.ts.map +1 -0
  158. package/dist/transport/multi/multi-transport.js +184 -0
  159. package/dist/transport/multi/multi-transport.test.d.ts +2 -0
  160. package/dist/transport/multi/multi-transport.test.d.ts.map +1 -0
  161. package/dist/transport/multi/multi-transport.test.js +236 -0
  162. package/dist/transport/rabbitmq/rabbitmq-transport.d.ts +73 -0
  163. package/dist/transport/rabbitmq/rabbitmq-transport.d.ts.map +1 -0
  164. package/dist/transport/rabbitmq/rabbitmq-transport.js +518 -0
  165. package/dist/transport/transport.d.ts +152 -0
  166. package/dist/transport/transport.d.ts.map +1 -0
  167. package/dist/transport/transport.js +1 -0
  168. package/dist/types/common.d.ts +41 -0
  169. package/dist/types/common.d.ts.map +1 -0
  170. package/dist/types/common.js +12 -0
  171. package/dist/types/envelope.d.ts +82 -0
  172. package/dist/types/envelope.d.ts.map +1 -0
  173. package/dist/types/envelope.js +36 -0
  174. package/dist/types/event.d.ts +96 -0
  175. package/dist/types/event.d.ts.map +1 -0
  176. package/dist/types/event.js +26 -0
  177. package/dist/types/event.test.d.ts +2 -0
  178. package/dist/types/event.test.d.ts.map +1 -0
  179. package/dist/types/event.test.js +130 -0
  180. package/dist/types/index.d.ts +9 -0
  181. package/dist/types/index.d.ts.map +1 -0
  182. package/dist/types/index.js +4 -0
  183. package/dist/types/subscriber.d.ts +207 -0
  184. package/dist/types/subscriber.d.ts.map +1 -0
  185. package/dist/types/subscriber.js +99 -0
  186. package/examples/config.ts +126 -0
  187. package/examples/event.ts +26 -0
  188. package/examples/order-event.json +19 -0
  189. package/package.json +66 -0
  190. package/src/checkpoint/context.test.ts +510 -0
  191. package/src/checkpoint/context.ts +213 -0
  192. package/src/checkpoint/index.ts +30 -0
  193. package/src/checkpoint/stores/memory.ts +47 -0
  194. package/src/checkpoint/stores/noop.ts +22 -0
  195. package/src/checkpoint/stores/stores.test.ts +177 -0
  196. package/src/checkpoint/types.ts +147 -0
  197. package/src/codec/codec.ts +42 -0
  198. package/src/codec/header-aware-codec.ts +41 -0
  199. package/src/codec/index.ts +11 -0
  200. package/src/codec/json-codec.ts +69 -0
  201. package/src/codec/rabbitmq-codec.test.ts +516 -0
  202. package/src/codec/rabbitmq-codec.ts +336 -0
  203. package/src/core/fanout.test.ts +1351 -0
  204. package/src/core/fanout.ts +184 -0
  205. package/src/core/index.ts +12 -0
  206. package/src/core/matador.test.ts +575 -0
  207. package/src/core/matador.ts +344 -0
  208. package/src/core/shutdown.test.ts +853 -0
  209. package/src/core/shutdown.ts +165 -0
  210. package/src/errors/checkpoint-errors.ts +62 -0
  211. package/src/errors/has-description.ts +25 -0
  212. package/src/errors/index.ts +57 -0
  213. package/src/errors/matador-errors.ts +477 -0
  214. package/src/errors/retry-errors.ts +134 -0
  215. package/src/hooks/index.ts +15 -0
  216. package/src/hooks/safe-hooks.ts +223 -0
  217. package/src/hooks/types.ts +248 -0
  218. package/src/index.ts +231 -0
  219. package/src/pipeline/index.ts +2 -0
  220. package/src/pipeline/pipeline.test.ts +1317 -0
  221. package/src/pipeline/pipeline.ts +371 -0
  222. package/src/retry/index.ts +4 -0
  223. package/src/retry/policy.ts +46 -0
  224. package/src/retry/standard-policy.test.ts +282 -0
  225. package/src/retry/standard-policy.ts +156 -0
  226. package/src/schema/index.ts +16 -0
  227. package/src/schema/registry.test.ts +339 -0
  228. package/src/schema/registry.ts +226 -0
  229. package/src/schema/types.test.ts +281 -0
  230. package/src/schema/types.ts +217 -0
  231. package/src/topology/builder.test.ts +275 -0
  232. package/src/topology/builder.ts +199 -0
  233. package/src/topology/index.ts +15 -0
  234. package/src/topology/types.ts +109 -0
  235. package/src/transport/capabilities.ts +88 -0
  236. package/src/transport/connection-manager.ts +218 -0
  237. package/src/transport/index.ts +42 -0
  238. package/src/transport/local/local-transport.test.ts +262 -0
  239. package/src/transport/local/local-transport.ts +327 -0
  240. package/src/transport/multi/multi-transport.test.ts +320 -0
  241. package/src/transport/multi/multi-transport.ts +294 -0
  242. package/src/transport/rabbitmq/rabbitmq-transport.ts +753 -0
  243. package/src/transport/transport.ts +200 -0
  244. package/src/types/common.ts +53 -0
  245. package/src/types/envelope.ts +152 -0
  246. package/src/types/event.test.ts +157 -0
  247. package/src/types/event.ts +118 -0
  248. package/src/types/index.ts +52 -0
  249. package/src/types/subscriber.ts +310 -0
  250. package/test/e2e/multi-transport.e2e.test.ts +236 -0
  251. package/test/e2e/rabbitmq-transport.e2e.test.ts +327 -0
  252. package/test/e2e/transport-compliance.e2e.test.ts +505 -0
  253. package/test/integration/matador.integration.test.ts +634 -0
  254. package/tsconfig.json +29 -0
  255. package/tsconfig.tsbuildinfo +1 -0
  256. package/tsup.config.ts +13 -0
@@ -0,0 +1,344 @@
1
+ import type { CheckpointStore } from '../checkpoint/index.js';
2
+ import type { Codec } from '../codec/index.js';
3
+ import { JsonCodec } from '../codec/index.js';
4
+ import {
5
+ InvalidSchemaError,
6
+ NotStartedError,
7
+ ShutdownInProgressError,
8
+ } from '../errors/index.js';
9
+ import type { MatadorHooks } from '../hooks/index.js';
10
+ import { SafeHooks } from '../hooks/index.js';
11
+ import { ProcessingPipeline } from '../pipeline/index.js';
12
+ import type { RetryPolicy } from '../retry/index.js';
13
+ import { StandardRetryPolicy } from '../retry/index.js';
14
+ import type { MatadorSchema } from '../schema/index.js';
15
+ import { SchemaRegistry, isSchemaEntryTuple } from '../schema/index.js';
16
+ import type { Topology } from '../topology/index.js';
17
+ import { getQualifiedQueueName } from '../topology/index.js';
18
+ import type { Subscription, Transport } from '../transport/index.js';
19
+ import type {
20
+ AnySubscriber,
21
+ Event,
22
+ EventClass,
23
+ EventOptions,
24
+ } from '../types/index.js';
25
+ import type { SendResult } from './fanout.js';
26
+ import { FanoutEngine } from './fanout.js';
27
+ import type { HandlersState, ShutdownConfig } from './shutdown.js';
28
+ import { ShutdownManager } from './shutdown.js';
29
+
30
+ /**
31
+ * Configuration for Matador.
32
+ */
33
+ export interface MatadorConfig {
34
+ /** Transport for message delivery */
35
+ readonly transport: Transport;
36
+
37
+ /** Topology configuration */
38
+ readonly topology: Topology;
39
+
40
+ /** Event schema mapping event keys to event classes and subscribers */
41
+ readonly schema: MatadorSchema;
42
+
43
+ /** Queues to consume from (empty = no consumption) */
44
+ readonly consumeFrom?: readonly string[] | undefined;
45
+
46
+ /** Custom codec (defaults to JSON) */
47
+ readonly codec?: Codec | undefined;
48
+
49
+ /** Custom retry policy */
50
+ readonly retryPolicy?: RetryPolicy | undefined;
51
+
52
+ /** Shutdown configuration */
53
+ readonly shutdownConfig?: Partial<ShutdownConfig> | undefined;
54
+
55
+ /**
56
+ * Checkpoint store for resumable subscribers.
57
+ * Required for persisting io() results across retries.
58
+ * If not provided, resumable subscribers will work but checkpoints
59
+ * won't persist across retries.
60
+ */
61
+ readonly checkpointStore?: CheckpointStore | undefined;
62
+ }
63
+
64
+ /**
65
+ * Matador - Transport-agnostic event processing library.
66
+ *
67
+ * Main orchestrator that wires together:
68
+ * - Transport: Message delivery
69
+ * - Schema: Event-subscriber registry
70
+ * - Pipeline: Message processing
71
+ * - Fanout: Event sending
72
+ * - Shutdown: Graceful termination
73
+ */
74
+ export class Matador {
75
+ private readonly transport: Transport;
76
+ private readonly topology: Topology;
77
+ private readonly schema;
78
+ private readonly codec: Codec;
79
+ private readonly retryPolicy: RetryPolicy;
80
+ private readonly hooks;
81
+ private readonly pipeline;
82
+ private readonly fanout;
83
+ private readonly shutdownManager;
84
+ private readonly consumeFrom: readonly string[];
85
+ private readonly subscriptions: Subscription[] = [];
86
+ private started = false;
87
+
88
+ /**
89
+ * Creates a new Matador instance.
90
+ *
91
+ * @param config - Static configuration (transport, topology, schema, etc.)
92
+ * @param hooks - Lifecycle hooks for logging, monitoring, and dynamic configuration.
93
+ * Passed separately to support NestJS dependency injection.
94
+ */
95
+ constructor(config: MatadorConfig, hooks?: MatadorHooks) {
96
+ this.transport = config.transport;
97
+ this.topology = config.topology;
98
+ this.consumeFrom = config.consumeFrom ?? [];
99
+
100
+ // Initialize components
101
+ this.schema = new SchemaRegistry();
102
+ this.codec = config.codec ?? new JsonCodec();
103
+ this.retryPolicy = config.retryPolicy ?? new StandardRetryPolicy();
104
+ this.hooks = new SafeHooks(hooks);
105
+
106
+ // Register schema from config
107
+ this.registerSchema(config.schema);
108
+
109
+ // Create pipeline
110
+ this.pipeline = new ProcessingPipeline({
111
+ transport: this.transport,
112
+ schema: this.schema,
113
+ codec: this.codec,
114
+ retryPolicy: this.retryPolicy,
115
+ hooks: this.hooks,
116
+ checkpointStore: config.checkpointStore,
117
+ });
118
+
119
+ // Create fanout engine
120
+ const defaultQueue = this.topology.queues[0]?.name ?? 'default';
121
+ this.fanout = new FanoutEngine({
122
+ transport: this.transport,
123
+ schema: this.schema,
124
+ hooks: this.hooks,
125
+ namespace: this.topology.namespace,
126
+ defaultQueue,
127
+ });
128
+
129
+ // Create shutdown manager
130
+ this.shutdownManager = new ShutdownManager(
131
+ () => this.fanout.eventsBeingEnqueuedCount,
132
+ () => this.stopReceiving(),
133
+ () => this.transport.disconnect(),
134
+ config.shutdownConfig,
135
+ );
136
+ }
137
+
138
+ /**
139
+ * Registers an event class with its subscribers.
140
+ */
141
+ register<T>(
142
+ eventClass: EventClass<T>,
143
+ subscribers: readonly AnySubscriber[],
144
+ ): this {
145
+ this.schema.register(eventClass, subscribers);
146
+ return this;
147
+ }
148
+
149
+ /**
150
+ * Registers events from a schema object.
151
+ * Supports both object format and tuple format entries.
152
+ *
153
+ * @example
154
+ * ```typescript
155
+ * // Tuple format
156
+ * matador.registerSchema({
157
+ * [UserCreatedEvent.key]: [UserCreatedEvent, [emailSubscriber]],
158
+ * [OrderPlacedEvent.key]: [OrderPlacedEvent, [invoiceSubscriber]],
159
+ * });
160
+ *
161
+ * // Object format
162
+ * matador.registerSchema({
163
+ * [UserCreatedEvent.key]: { eventClass: UserCreatedEvent, subscribers: [emailSubscriber] },
164
+ * });
165
+ * ```
166
+ */
167
+ registerSchema(schema: MatadorSchema): this {
168
+ for (const entry of Object.values(schema)) {
169
+ if (isSchemaEntryTuple(entry)) {
170
+ // Tuple format: [EventClass, Subscriber[]]
171
+ const [eventClass, subscribers] = entry;
172
+ this.schema.register(eventClass, subscribers);
173
+ } else {
174
+ // Object format: { eventClass, subscribers }
175
+ this.schema.register(entry.eventClass, entry.subscribers);
176
+ }
177
+ }
178
+ return this;
179
+ }
180
+
181
+ /**
182
+ * Starts Matador - connects transport and begins consuming.
183
+ * This method is idempotent - calling it multiple times is safe.
184
+ */
185
+ async start(): Promise<void> {
186
+ // Idempotent: if already started, just return
187
+ if (this.started) {
188
+ return;
189
+ }
190
+
191
+ // Validate schema
192
+ const validation = this.schema.validate();
193
+ if (!validation.valid) {
194
+ const errors = validation.issues.filter((i) => i.severity === 'error');
195
+ throw new InvalidSchemaError(
196
+ 'Schema validation failed',
197
+ errors.map((e) => e.message).join(', '),
198
+ );
199
+ }
200
+
201
+ // Connect transport
202
+ await this.transport.connect();
203
+
204
+ // Apply topology
205
+ await this.transport.applyTopology(this.topology);
206
+
207
+ // Subscribe to queues
208
+ for (const queueName of this.consumeFrom) {
209
+ const qualifiedName = getQualifiedQueueName(
210
+ this.topology.namespace,
211
+ queueName,
212
+ );
213
+ const queueDef = this.topology.queues.find((q) => q.name === queueName);
214
+
215
+ const subscription = await this.transport.subscribe(
216
+ qualifiedName,
217
+ async (envelope, receipt) => {
218
+ this.shutdownManager.incrementProcessing();
219
+ try {
220
+ const rawMessage = this.codec.encode(envelope);
221
+ await this.pipeline.process(rawMessage, receipt);
222
+ } finally {
223
+ this.shutdownManager.decrementProcessing();
224
+ }
225
+ },
226
+ queueDef?.concurrency !== undefined
227
+ ? { concurrency: queueDef.concurrency }
228
+ : undefined,
229
+ );
230
+
231
+ this.subscriptions.push(subscription);
232
+ }
233
+
234
+ this.started = true;
235
+ }
236
+
237
+ /**
238
+ * Sends an event to all registered subscribers.
239
+ *
240
+ * @example
241
+ * ```typescript
242
+ * // Pass the event class and data directly
243
+ * await matador.send(UserCreatedEvent, { userId: '123' });
244
+ *
245
+ * // Or pass an event instance
246
+ * const event = new UserCreatedEvent({ userId: '123' });
247
+ * await matador.send(event);
248
+ * ```
249
+ */
250
+ async send<T>(
251
+ eventClass: EventClass<T>,
252
+ data: T,
253
+ options?: EventOptions,
254
+ ): Promise<SendResult>;
255
+ async send<T>(event: Event<T>, options?: EventOptions): Promise<SendResult>;
256
+ async send<T>(
257
+ eventOrClass: Event<T> | EventClass<T>,
258
+ dataOrOptions?: T | EventOptions,
259
+ maybeOptions?: EventOptions,
260
+ ): Promise<SendResult> {
261
+ if (!this.started) {
262
+ throw new NotStartedError('send');
263
+ }
264
+
265
+ if (!this.shutdownManager.isEnqueueAllowed) {
266
+ throw new ShutdownInProgressError();
267
+ }
268
+
269
+ // Determine if first arg is an event instance or event class
270
+ const isEventClass =
271
+ typeof eventOrClass === 'function' && 'key' in eventOrClass;
272
+
273
+ if (isEventClass) {
274
+ // Called as: send(EventClass, data, options?)
275
+ const eventClass = eventOrClass as EventClass<T>;
276
+ const data = dataOrOptions as T;
277
+ const options = maybeOptions;
278
+ const event = new eventClass(data);
279
+ return this.fanout.send(eventClass, event, options);
280
+ } else {
281
+ // Called as: send(event, options?)
282
+ const event = eventOrClass as Event<T>;
283
+ const options = dataOrOptions as EventOptions | undefined;
284
+ const eventClass = event.constructor as EventClass<T>;
285
+ return this.fanout.send(eventClass, event, options);
286
+ }
287
+ }
288
+
289
+ /**
290
+ * Gets current handler state.
291
+ */
292
+ getHandlersState(): HandlersState {
293
+ return this.shutdownManager.getHandlersState();
294
+ }
295
+
296
+ /**
297
+ * Checks if Matador is idle (no processing or enqueuing).
298
+ */
299
+ isIdle(): boolean {
300
+ return this.shutdownManager.getHandlersState().isIdle;
301
+ }
302
+
303
+ /**
304
+ * Waits for all handlers to become idle.
305
+ */
306
+ async waitForIdle(timeoutMs = 30000): Promise<boolean> {
307
+ const deadline = Date.now() + timeoutMs;
308
+
309
+ while (!this.isIdle()) {
310
+ if (Date.now() > deadline) {
311
+ return false;
312
+ }
313
+ await new Promise((resolve) => setTimeout(resolve, 100));
314
+ }
315
+
316
+ return true;
317
+ }
318
+
319
+ /**
320
+ * Gracefully shuts down Matador.
321
+ */
322
+ async shutdown(): Promise<void> {
323
+ if (!this.started) {
324
+ return;
325
+ }
326
+
327
+ await this.shutdownManager.shutdown();
328
+ this.started = false;
329
+ }
330
+
331
+ /**
332
+ * Checks if transport is connected.
333
+ */
334
+ isConnected(): boolean {
335
+ return this.transport.isConnected();
336
+ }
337
+
338
+ private async stopReceiving(): Promise<void> {
339
+ for (const subscription of this.subscriptions) {
340
+ await subscription.unsubscribe();
341
+ }
342
+ this.subscriptions.length = 0;
343
+ }
344
+ }