@artstesh/postboy 3.5.0 → 3.5.2

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.
package/lib/index.d.mts CHANGED
@@ -1,689 +1,1010 @@
1
1
  import { Subject, Observable } from 'rxjs';
2
2
 
3
3
  /**
4
- * Represents metadata associated with a Postboy message.
5
- *
6
- * This interface can be used to store optional metadata that provides
7
- * additional context or tracking information for a message. It supports
8
- * flexible extension by allowing additional properties through an index signature.
9
- *
10
- * Properties:
11
- * - `correlationId` (optional): A unique identifier used to correlate
12
- * related messages or operations across systems.
13
- * - `causationId` (optional): The identifier of the preceding message
14
- * or event that caused the current message to be produced.
15
- * - `source` (optional): The origin or source of the message, such as
16
- * a particular service or system.
17
- * - `tags` (optional): An array of tags or labels that can be attached
18
- * to the message for categorization, filtering, or logging purposes.
19
- * - `[key: string]` (optional): Additional custom properties can be
20
- * added to capture specific metadata not covered by the predefined fields.
4
+ * Free-form metadata attached to a message via `PostboyMessage.setMetadata`.
5
+ *
6
+ * The predefined fields describe message correlation and causality; the index signature
7
+ * accepts any custom key.
21
8
  */
22
9
  interface PostboyMessageMetadata {
10
+ /** Ties together all messages belonging to one logical operation or request. */
23
11
  correlationId?: string;
12
+ /** The `id` of the message that directly caused this one, when causality is tracked. */
24
13
  causationId?: string;
14
+ /** Free-form labels used for categorization, filtering, or logging. */
25
15
  tags?: Set<string>;
16
+ /** Any custom key-value data. */
26
17
  [key: string]: any;
27
18
  }
28
19
 
20
+ /**
21
+ * Root of the message hierarchy — anything that travels through the bus.
22
+ *
23
+ * A message is a plain data carrier: dispatch behavior comes from the subclasses.
24
+ * {@link PostboyGenericMessage} is the base for pub/sub messages, and
25
+ * {@link PostboyExecutor} carries a synchronous command. The bus routes every one of
26
+ * them by the static `ID` of the concrete class, exposed per instance by {@link id}.
27
+ */
29
28
  declare abstract class PostboyMessage {
29
+ /** Free-form data attached to the message; populate it via {@link setMetadata}. */
30
30
  metadata: PostboyMessageMetadata;
31
+ /** The static `ID` of the concrete message class — the key the bus routes by. */
31
32
  get id(): string;
33
+ /**
34
+ * Shallow-merges the given fields into {@link metadata}.
35
+ *
36
+ * @param metadata - Fields to add or override.
37
+ * @return This message, for chaining.
38
+ */
32
39
  setMetadata(metadata: Partial<PostboyMessageMetadata>): this;
33
40
  }
34
41
 
35
42
  /**
36
- * An inheritor should have a static ID field
43
+ * Base class for pub/sub messages the "letters" carried by the bus.
44
+ *
45
+ * A subclass must declare its own `static readonly ID` (a unique string): registration,
46
+ * subscription, locking, and dispatch are all keyed by that `ID`, not by class identity.
47
+ * Inheriting a parent's `ID` makes two message types silently collide on one registration.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * class PingMessage extends PostboyGenericMessage {
52
+ * static readonly ID = 'app.ping';
53
+ * constructor(public text: string) {
54
+ * super();
55
+ * }
56
+ * }
57
+ * ```
37
58
  */
38
59
  declare abstract class PostboyGenericMessage extends PostboyMessage {
39
60
  }
40
61
 
62
+ /**
63
+ * Base class for synchronous commands executed via `PostboyService.exec`.
64
+ *
65
+ * An executor is a short-lived value object: construct it with the command arguments and
66
+ * pass it to `exec`, which invokes the handler registered for the class's static `ID`
67
+ * and returns its result synchronously. For asynchronous results use a
68
+ * {@link PostboyCallbackMessage} instead.
69
+ *
70
+ * @example
71
+ * ```ts
72
+ * class GetDataExecutor extends PostboyExecutor<string> {
73
+ * static readonly ID = 'app.get-data';
74
+ * constructor(public key: string) {
75
+ * super();
76
+ * }
77
+ * }
78
+ *
79
+ * postboy.exec(new ConnectExecutor(GetDataExecutor, (e) => store.get(e.key)));
80
+ * const value: string = postboy.exec(new GetDataExecutor('foo'));
81
+ * ```
82
+ */
41
83
  declare abstract class PostboyExecutor<T> extends PostboyMessage {
84
+ /**
85
+ * Phantom marker existing only at the type level: it ties the class to its result type
86
+ * `T` so `exec` can infer it. Erased at runtime — never assign to it.
87
+ */
42
88
  protected readonly _postboyResultType?: T;
43
89
  }
44
90
 
45
91
  /**
46
- * An abstract class extending PostboyGenericMessage that provides mechanisms for managing
47
- * asynchronous data communication using RxJS observables. It is designed to work with
48
- * callback-based operations that emit a result of type T.
49
- *
50
- * @template T - The type of the data emitted by the observables in this class.
92
+ * Base class for async request/response messages: the requester fires it via
93
+ * `PostboyService.fireCallback`, a responder subscribed to the message type produces
94
+ * the result through {@link next} or {@link finish}, and the requester observes the
95
+ * values on {@link result}.
51
96
  *
52
- * @extends PostboyGenericMessage
97
+ * Use a callback message when the result arrives asynchronously; for synchronous
98
+ * results use a {@link PostboyExecutor} instead.
53
99
  *
54
- * @property {Observable<T>} result - An observable that emits the result of the operation
55
- * and completes once the operation is finished.
100
+ * @example
101
+ * ```ts
102
+ * class FetchDataMessage extends PostboyCallbackMessage<string> {
103
+ * static readonly ID = 'app.fetch-data';
104
+ * }
56
105
  *
57
- * @method next - Emits the next value for the result observable.
58
- * @param {T} value - The value to be emitted by the result observable.
59
- *
60
- * @method finish - Emits the final value for the result observable and completes it.
61
- * @param {T} value - The final value for the observable emission.
106
+ * // responder: produces the result
107
+ * postboy.sub(FetchDataMessage).subscribe((m) => m.finish('payload'));
108
+ * // requester: consumes it
109
+ * postboy.fireCallback(new FetchDataMessage()).subscribe((payload) => console.log(payload));
110
+ * ```
62
111
  */
63
112
  declare abstract class PostboyCallbackMessage<T> extends PostboyGenericMessage {
113
+ /** The subject backing {@link result}; protected so only subclass/responder code can emit. */
64
114
  protected result$: Subject<T>;
115
+ /** The observable of result values; completed by {@link finish}, {@link complete}, or a `DisconnectMessage`. */
65
116
  result: Observable<T>;
66
117
  /**
67
- * Emits the provided value.
118
+ * Emits an intermediate result value to every {@link result} subscriber without
119
+ * completing the stream — for operations that produce several values.
68
120
  *
69
- * @template T - The type of the value to emit.
70
- * @param {T} value - The value to emit through the `result$` observable.
71
- * @returns {void}
121
+ * @param value - The value to emit.
72
122
  */
73
123
  next: (value: T) => void;
74
124
  /**
75
- * Marks the operation as complete by emitting the provided value and then completing the result stream.
125
+ * Emits the final result value and completes {@link result}.
76
126
  *
77
- * @param {T} value - The value to emit prior to completing the result stream.
78
- * @return {void} This method does not return a value.
127
+ * @param value - The last value to emit.
79
128
  */
80
129
  finish(value: T): void;
81
- /**
82
- * Completes the current observable result stream.
83
- * This method marks the result observable as complete, ensuring no further values
84
- * or events will be emitted from it.
85
- *
86
- * @return {void} No value is returned from this method.
87
- */
130
+ /** Completes {@link result} without emitting another value. */
88
131
  complete(): void;
89
132
  }
90
133
 
134
+ /**
135
+ * A service sharing the lifecycle of the {@link PostboyAbstractRegistrator} it is
136
+ * attached to via `registerServices`.
137
+ */
91
138
  interface IPostboyDependingService {
139
+ /**
140
+ * Called by `PostboyAbstractRegistrator.up()` after the registrator's own
141
+ * registrations are done. Start or initialize the service here.
142
+ */
92
143
  up(): void;
93
- down?: () => void;
144
+ /**
145
+ * Optional teardown: called by `PostboyAbstractRegistrator.down()` before the
146
+ * registrator disconnects its recorded messages. Release resources here.
147
+ */
148
+ down?(): void;
94
149
  }
95
150
 
96
151
  /**
97
- * Abstract class representing a handler for executing a Postboy task.
152
+ * Base class for class-based executor handlers the object-oriented alternative to the
153
+ * `(e) => result` function accepted by `ConnectExecutor`.
98
154
  *
99
- * This class is intended to manage the execution flow of a PostboyExecutor instance.
100
- * Subclasses must implement the `handle` method, which executes a given PostboyExecutor
101
- * and returns a result of type R.
155
+ * Register an instance with `ConnectHandler` (or `PostboyAbstractRegistrator.recordHandler`);
156
+ * {@link handle} is then invoked on every `PostboyService.exec` of the executor type.
102
157
  *
103
- * @template R The type of the result returned by the `handle` method.
104
- * @template E The type of the executor extending the PostboyExecutor.
158
+ * @template R - Result type returned by {@link handle}.
159
+ * @template E - The executor type this handler serves.
105
160
  */
106
161
  declare abstract class PostboyExecutionHandler<R, E extends PostboyExecutor<R>> {
107
162
  /**
108
- * Abstract method to handle the specified executor and return a result.
163
+ * Processes an executor command and returns its result synchronously.
109
164
  *
110
- * @param {E} executor - The executor that will be processed by the method.
111
- * @return {R} The result obtained after handling the executor.
165
+ * @param executor - The executor instance passed to `PostboyService.exec`.
166
+ * @return The result handed back to the caller of `exec`.
112
167
  */
113
168
  abstract handle(executor: E): R;
114
169
  }
115
170
 
171
+ /**
172
+ * Constructor signature of a message class.
173
+ *
174
+ * The bus identifies message types by the static `ID` such a constructor carries (see
175
+ * {@link PostboyGenericMessage}), not by class identity — two different classes sharing
176
+ * an `ID` collide on the same registration (the later one overrides the earlier one
177
+ * with a warning).
178
+ */
116
179
  type MessageType<T extends PostboyGenericMessage> = new (...args: any[]) => T;
180
+ /**
181
+ * Base class for feature registrators: registers messages and executors for one feature
182
+ * and remembers every recorded `ID`, so that a single {@link down} disconnects them all.
183
+ *
184
+ * Subclasses make their `record*` calls inside the abstract {@link _up} hook; services
185
+ * attached via {@link registerServices} share the same lifecycle. Create one registrator
186
+ * per feature (or per namespace, via `AddNamespace`) and keep the instance to tear the
187
+ * feature down later.
188
+ *
189
+ * @example
190
+ * ```ts
191
+ * class FeatureRegistrator extends PostboyAbstractRegistrator {
192
+ * protected _up(): void {
193
+ * this.recordSubject(PingMessage).recordExecutor(GetDataExecutor, (e) => e.payload);
194
+ * }
195
+ * }
196
+ *
197
+ * const reg = new FeatureRegistrator(postboy, 'feature-a');
198
+ * reg.up(); // registrations are live
199
+ * reg.down(); // everything recorded above is disconnected
200
+ * ```
201
+ */
117
202
  declare abstract class PostboyAbstractRegistrator {
118
203
  protected postboy: PostboyService;
204
+ /**
205
+ * Identifier of this registrator: the name passed to the constructor, or a generated
206
+ * unique id when none was given. It identifies the registrator only — message routing
207
+ * is not affected by it.
208
+ */
119
209
  get namespace(): string;
120
210
  private ids;
121
211
  private services;
122
212
  private readonly _namespace;
213
+ /**
214
+ * @param postboy - The bus every `record*` call is executed on.
215
+ * @param namespace - Optional name exposed by {@link namespace}; a random unique id is generated when omitted.
216
+ */
123
217
  constructor(postboy: PostboyService, namespace?: string | null);
124
218
  /**
125
- * Registers a list of services to be used by the application.
219
+ * Sets the services that share this registrator's lifecycle: their `up()` runs on
220
+ * {@link up} (after the registrations), their `down()` on {@link down} (before the
221
+ * disconnection). Replaces any previously registered list.
126
222
  *
127
- * @param {IPostboyDependingService[]} services - An array of services to register.
128
- * @return {void} This method does not return a value.
223
+ * @param services - Services to attach to the lifecycle.
129
224
  */
130
225
  registerServices(services: IPostboyDependingService[]): void;
131
226
  /**
132
- * Initiates the 'up' process for the current instance and all associated services.
133
- *
134
- * @return {void} Does not return a value.
227
+ * Activates the registrator: runs the {@link _up} registration hook, then calls `up()`
228
+ * on every attached service.
135
229
  */
136
230
  up(): void;
231
+ /**
232
+ * Registration hook executed by {@link up}. Subclasses make all their `record*` calls
233
+ * here so that {@link down} can disconnect them.
234
+ */
137
235
  protected abstract _up(): void;
236
+ /**
237
+ * Tears the registrator down, in order: calls `down()` on every attached service (then
238
+ * clears the service list), then executes a `DisconnectMessage` for each recorded `ID`,
239
+ * completing subscriber streams and removing handlers registered by this registrator.
240
+ */
138
241
  down(): void;
139
242
  /**
140
- * Records a type and its corresponding Subject<T> into the Postboy system and updates the internal identifiers.
243
+ * Registers a message type with the given subject and remembers its `ID` for {@link down}.
141
244
  *
142
- * @param {MessageType<T>} type - A constructor for the generic message type T.
143
- * @param {Subject<T>} sub - The subject associated with the generic message type.
144
- * @return {this} Returns the current instance for method chaining.
245
+ * @param type - The constructor of the message type; must declare its own static `ID`.
246
+ * @param sub - The subject subscribers will observe.
247
+ * @return This registrator, for chaining.
145
248
  */
146
249
  record<T extends PostboyGenericMessage>(type: MessageType<T>, sub: Subject<T>): PostboyAbstractRegistrator;
147
250
  /**
148
- * Records a message type with a specific subject and applies a transformation pipe to the subject.
251
+ * Registers a message type whose stream is transformed by a pipe before reaching
252
+ * subscribers, and remembers its `ID` for {@link down}.
149
253
  *
150
- * @param {MessageType<T>} type - The constructor of the message type to record, which extends PostboyGenericMessage.
151
- * @param {Subject<T>} sub - The Subject instance to associate with the message type.
152
- * @param {(s: Subject<T>) => Observable<T>} pipe - A function that takes the subject as input and returns an Observable with transformations applied.
153
- * @return {this} The current instance of the class for chaining.
254
+ * @param type - The constructor of the message type; must declare its own static `ID`.
255
+ * @param sub - The subject subscribers will observe.
256
+ * @param pipe - Wraps the subject into the observable handed out by `PostboyService.sub`, e.g. to apply operators.
257
+ * @return This registrator, for chaining.
154
258
  */
155
259
  recordWithPipe<T extends PostboyGenericMessage>(type: MessageType<T>, sub: Subject<T>, pipe: (s: Subject<T>) => Observable<T>): PostboyAbstractRegistrator;
156
260
  /**
157
- * Records an executor associated with a specific type and execution logic.
261
+ * Registers a synchronous handler for an executor type and remembers its `ID` for {@link down}.
158
262
  *
159
- * @param type The class constructor of the executor type to be recorded, which extends PostboyExecutor.
160
- * @param exec A callback function that executes the logic using an instance of the specified executor type.
161
- * @return void
263
+ * @param type - The constructor of the executor class; must declare its own static `ID`.
264
+ * @param exec - Called with the executor instance on every `PostboyService.exec` of this type.
265
+ * @return This registrator, for chaining.
162
266
  */
163
267
  recordExecutor<E extends PostboyExecutor<T>, T>(type: new (...args: any[]) => E, exec: (e: E) => T): PostboyAbstractRegistrator;
164
268
  /**
165
- * Records a handler for a specific executor type.
269
+ * Registers a {@link PostboyExecutionHandler} for an executor type and remembers its
270
+ * `ID` for {@link down}. The handler's `handle` method is invoked on every
271
+ * `PostboyService.exec` of this type.
166
272
  *
167
- * @param executor The constructor of the executor type, which extends `PostboyExecutor`.
168
- * @param handler The execution handler associated with the given executor type.
169
- * @return void
273
+ * @param executor - The constructor of the executor class; must declare its own static `ID`.
274
+ * @param handler - The handler instance receiving the executor.
275
+ * @return This registrator, for chaining.
170
276
  */
171
277
  recordHandler<E extends PostboyExecutor<R>, R>(executor: new (...args: any[]) => E, handler: PostboyExecutionHandler<R, E>): PostboyAbstractRegistrator;
172
278
  /**
173
- * A utility function that facilitates the recording and replaying of messages
174
- * using a ReplaySubject. This function is designed to handle messages of a
175
- * specific type and allows specifying a buffer size to determine how many
176
- * of the most recent messages should be replayed.
279
+ * Registers the type with a `ReplaySubject`: new subscribers first receive up to
280
+ * `bufferSize` most recently fired messages, then live ones. Suited for
281
+ * "latest events" streams where late subscribers need recent history.
177
282
  *
178
- * @template T Extends the PostboyGenericMessage type, representing the type of message
179
- * to be recorded and replayed.
180
- * @param {MessageType<T>} type The constructor of the message type to be recorded and replayed.
181
- * @param {number} [bufferSize=1] The number of recent messages to retain in the ReplaySubject's buffer.
182
- * Defaults to 1 if not specified.
183
- * @returns The result of invoking the `record` method with the given message type and configured ReplaySubject.
283
+ * @param type - The constructor of the message type; must declare its own static `ID`.
284
+ * @param bufferSize - How many past messages to replay to new subscribers; defaults to 1.
285
+ * @return This registrator, for chaining.
184
286
  */
185
287
  recordReplay<T extends PostboyGenericMessage>(type: MessageType<T>, bufferSize?: number): PostboyAbstractRegistrator;
186
288
  /**
187
- * Represents a method that records a specific behavior associated with a message type.
188
- * It creates a `BehaviorSubject` initialized with the provided initial message
189
- * and associates it with the given message type using the `record` method.
289
+ * Registers the type with a `BehaviorSubject` seeded with `initial`: every new
290
+ * subscriber immediately receives the most recent message — the seed itself until
291
+ * anything is fired. Suited for state-like messages rather than one-off events.
190
292
  *
191
- * @template T - A type parameter extending `PostboyGenericMessage` that defines the message structure.
192
- * @param {MessageType<T>} type - The constructor function of the message type to be recorded.
193
- * @param {T} initial - The initial value of the message that will be set in the `BehaviorSubject`.
194
- * @returns {void} - This function does not return a value; instead, it modifies the internal state.
293
+ * @param type - The constructor of the message type; must declare its own static `ID`.
294
+ * @param initial - The message instance new subscribers receive before any `PostboyService.fire`.
295
+ * @return This registrator, for chaining.
195
296
  */
196
297
  recordBehavior<T extends PostboyGenericMessage>(type: MessageType<T>, initial: T): PostboyAbstractRegistrator;
197
298
  /**
198
- * A function that creates and returns a new generic message recorder for a specific message type.
299
+ * Registers the type with a plain `Subject`: subscribers only see messages fired after
300
+ * they subscribed. The default choice for event-like messages.
199
301
  *
200
- * @template T - A type parameter extending from `PostboyGenericMessage`.
201
- * @param {MessageType<T>} type - The constructor for the message type being recorded.
202
- * @returns {Subject<T>} A new instance of `Subject<T>` bound to the specified message type.
302
+ * @param type - The constructor of the message type; must declare its own static `ID`.
303
+ * @return This registrator, for chaining.
203
304
  */
204
305
  recordSubject<T extends PostboyGenericMessage>(type: MessageType<T>): this;
205
306
  }
206
307
 
308
+ /**
309
+ * The pipeline phase a middleware hook is running for. Each bus verb has its own stage:
310
+ * `Publish` for `fire`, `Callback` for `fireCallback`, `Execute` for `exec`.
311
+ */
207
312
  declare enum MiddlewareStage {
313
+ /** `PostboyService.fire` — pub/sub dispatch. */
208
314
  Publish = 1,
315
+ /** `PostboyService.fireCallback` — async request/response dispatch. */
209
316
  Callback = 2,
317
+ /** `PostboyService.exec` — synchronous command execution, including infrastructure messages. */
210
318
  Execute = 3
211
319
  }
212
320
 
321
+ /**
322
+ * What every middleware hook receives: the running stage plus the message or executor
323
+ * being processed. Filter by `stage` and `message.id` inside `canHandle`.
324
+ */
213
325
  interface PipelineContext<T extends PostboyMessage = PostboyMessage> {
326
+ /** The pipeline phase the `before`/`after` hook is running for. */
214
327
  stage: MiddlewareStage;
328
+ /** The fired message or the executed executor. */
215
329
  message: T;
216
330
  }
217
331
 
332
+ /**
333
+ * The verdict a middleware returns from its `before` hook: let the operation proceed,
334
+ * or cancel it.
335
+ */
218
336
  declare enum MiddlewareDecisionType {
337
+ /** Run the operation; the pipeline moves on to the next middleware. */
219
338
  Continue = 1,
339
+ /** Cancel the operation: the bus throws a {@link CancelError} and the `after` hooks are skipped. */
220
340
  Interrupt = 2
221
341
  }
222
342
 
343
+ /** The decision returned by a middleware's `before` hook. */
223
344
  interface MiddlewareDecision {
345
+ /** Whether the operation may proceed; {@link MiddlewareDecisionType.Interrupt} cancels it. */
224
346
  type: MiddlewareDecisionType;
225
347
  }
226
348
 
349
+ /**
350
+ * Structured information about a middleware cancellation, carried by
351
+ * {@link CancelError.details}.
352
+ */
227
353
  interface CancelDetails {
354
+ /** The stage whose `before` hook cancelled the operation. */
228
355
  stage: MiddlewareStage;
356
+ /** Human-readable reason; becomes the `CancelError` message when set. */
229
357
  reason?: string;
358
+ /** `name` of the middleware that returned the interrupt decision. */
230
359
  middleware?: string;
360
+ /** The static `ID` of the cancelled message or executor. */
231
361
  messageId?: string;
362
+ /** Reserved for namespace attribution; not populated by the built-in pipeline. */
232
363
  namespace?: string;
233
364
  }
234
365
 
366
+ /**
367
+ * The error thrown when a middleware interrupts an operation by returning an interrupt
368
+ * decision from its `before` hook — the operation does not run.
369
+ *
370
+ * Thrown by `PostboyService.fire`, `fireCallback`, and `exec`. Distinguish it from other
371
+ * errors via `error.name === 'PostboyCancelError'` or `error instanceof CancelError`,
372
+ * then inspect {@link details} to find the cancelling middleware and stage.
373
+ */
235
374
  declare class CancelError extends Error {
375
+ /** Structured information about the cancellation. */
236
376
  readonly details: CancelDetails;
377
+ /**
378
+ * @param details - What was cancelled, where, and why; `details.reason` — or a generated
379
+ * stage message — becomes the error message.
380
+ */
237
381
  constructor(details: CancelDetails);
238
382
  }
239
383
 
384
+ /**
385
+ * A summary of a pipeline run: whether the operation was cancelled, and by whom.
386
+ *
387
+ * Kept for middleware implementations that collect their own run reports; the built-in
388
+ * pipeline surfaces cancellations as a thrown {@link CancelError} instead.
389
+ */
240
390
  type PipelineResult = {
391
+ /** Whether an `Interrupt` decision stopped the operation. */
241
392
  cancelled: boolean;
393
+ /** `name` of the middleware that cancelled the operation. */
242
394
  cancelledBy?: string;
395
+ /** Why the operation was cancelled. */
243
396
  reason?: string;
244
397
  };
245
398
 
399
+ /**
400
+ * Execution context of a message — a snapshot of where in a message-causality chain the
401
+ * current code runs, built by the internal context tracking when it is active.
402
+ */
246
403
  interface PostboyMessageContext {
404
+ /** Shared by every message of one logical operation; equals the root message's id. */
247
405
  correlationId: string;
406
+ /** The static `ID` of the message currently being processed. */
248
407
  currentMessageId: string;
408
+ /** The `ID` of the message whose handling triggered this one, when nested. */
249
409
  parentMessageId?: string;
410
+ /** How many messages separate this one from the root of the chain. */
250
411
  depth: number;
412
+ /** When the root message of the chain was fired. */
251
413
  startedAt: Date;
414
+ /** Tags accumulated along the chain — the union of the messages' metadata tags. */
252
415
  tags?: Set<string>;
253
416
  }
254
417
 
255
418
  /**
256
- * A class that manages a reactive subscription using a provided Subject and transformation pipe.
257
- * Offers methods to interact with the subscription, such as emitting data, completing the subscription,
258
- * and accessing the transformed observable.
419
+ * The internal pairing of a registered subject with the observable handed out to
420
+ * subscribers the pipe, when given, is applied exactly once, here.
259
421
  *
260
- * @template T The type of data managed by the subscription.
422
+ * Created by the registration paths (`ConnectMessage` and the deprecated `record*`
423
+ * methods); consumer code never constructs it directly.
424
+ *
425
+ * @template T - The message type the stream carries.
261
426
  */
262
427
  declare class PostboySubscription<T> {
263
428
  private subscription;
264
429
  private readonly _subscription;
265
430
  /**
266
- * Constructs an instance of the class with a given subscription and a transformation pipe.
267
- *
268
- * @param {Subject<T>} subscription - The source Subject that will be transformed.
269
- * @param {(s: Subject<T>) => Observable<T>} pipe - A function that applies a transformation to the subscription.
431
+ * @param subscription - The subject `fire` pushes messages into.
432
+ * @param pipe - Optional wrapper producing the observable subscribers receive; defaults to the plain subject.
270
433
  */
271
434
  constructor(subscription: Subject<T>, pipe?: (s: Subject<T>) => Observable<T>);
272
- /**
273
- * Returns an observable subscription.
274
- *
275
- * @return {Observable<T>} An observable instance of type T.
276
- */
435
+ /** The observable handed out on every `PostboyService.sub` call — one shared stream for all subscribers. */
277
436
  sub(): Observable<T>;
278
- /**
279
- * Triggers an event by emitting the provided data to all subscribers.
280
- *
281
- * @param {T} data - The data to emit to the subscribers.
282
- * @return {void} - Does not return any value.
283
- */
437
+ /** Pushes a message into the underlying subject, notifying all subscribers synchronously. */
284
438
  fire(data: T): void;
285
- /**
286
- * Completes the subscription, signaling that no further values will be sent.
287
- * This is typically used to finalize or clean up resources.
288
- * @return {void} This method does not return any value.
289
- */
439
+ /** Completes the underlying subject, ending every subscriber's stream. */
290
440
  finish(): void;
291
441
  }
292
442
 
293
443
  /**
294
- * Abstract base class for defining middleware in a pipeline.
295
- * Middleware acts on various stages of pipeline execution, allowing operations
296
- * to be intercepted, modified, or monitored.
444
+ * Base class for custom middleware the "customs officers" inspecting everything that
445
+ * travels through the bus.
446
+ *
447
+ * A middleware joins the pipeline via `AddMiddleware` and leaves it via
448
+ * `RemoveMiddleware`, which also calls {@link dispose}. For every operation the hooks
449
+ * run per stage (`Publish` for `fire`, `Callback` for `fireCallback`, `Execute` for
450
+ * `exec` — see {@link MiddlewareStage}): {@link canHandle} is consulted first, then
451
+ * {@link before} ahead of the operation, then {@link after} once it has finished.
452
+ * Hooks run for infrastructure messages too — filter with {@link canHandle} if needed.
453
+ *
454
+ * Put validation and gating into {@link before}: returning an interrupt decision there
455
+ * cancels the operation with a {@link CancelError}. Put logging and other side effects
456
+ * into {@link after}.
457
+ *
458
+ * @example
459
+ * ```ts
460
+ * class AuthMiddleware extends PostboyMiddleware {
461
+ * canHandle(context: PipelineContext): boolean {
462
+ * return context.stage === MiddlewareStage.Execute;
463
+ * }
464
+ *
465
+ * before(context: PipelineContext): MiddlewareDecision {
466
+ * return isAllowed(context.message)
467
+ * ? { type: MiddlewareDecisionType.Continue }
468
+ * : { type: MiddlewareDecisionType.Interrupt };
469
+ * }
470
+ * }
471
+ *
472
+ * postboy.exec(new AddMiddleware(new AuthMiddleware()));
473
+ * ```
297
474
  */
298
475
  declare abstract class PostboyMiddleware {
476
+ /** Identifies the middleware in {@link CancelError} details; defaults to the class name. */
299
477
  readonly name: string;
300
478
  /**
301
- * Creates an instance of a class, optionally assigning a name.
302
- *
303
- * @param {string} [name] - An optional name to be assigned. If not provided, defaults to the class name.
479
+ * @param name - Custom name reported on cancellation; defaults to the class name.
304
480
  */
305
481
  constructor(name?: string);
306
482
  /**
307
- * Optional filter to skip middleware for unrelated messages/stages.
483
+ * Decides whether this middleware participates for the given context. Override to
484
+ * narrow the middleware by stage or message id. Default: handle everything.
308
485
  */
309
486
  canHandle(_context: PipelineContext): boolean;
310
487
  /**
311
- * Called before the stage is executed.
312
- * Return Interrupt to cancel the operation.
488
+ * Runs ahead of the operation. Return an interrupt decision
489
+ * ({@link MiddlewareDecisionType.Interrupt}) to cancel it: the bus then throws a
490
+ * {@link CancelError}, and the operation and all `after` hooks are skipped.
491
+ * Default: continue.
313
492
  */
314
493
  before(_context: PipelineContext): MiddlewareDecision;
315
494
  /**
316
- * Called after the stage has finished successfully.
317
- * `result` is typically set for execute-stage.
495
+ * Runs after the operation has finished successfully. `result` carries the executor's
496
+ * return value on the `Execute` stage and is `undefined` on `Publish` and `Callback`.
497
+ * Default: no-op.
318
498
  */
319
499
  after(_context: PipelineContext, _result?: unknown): void;
320
- /**
321
- * Optional cleanup hook.
322
- */
500
+ /** Cleanup hook called on `RemoveMiddleware` and on bus disposal. Default: no-op. */
323
501
  dispose(): void;
324
502
  }
325
503
 
504
+ /**
505
+ * The middleware pipeline of the bus: keeps the chain in insertion order and drives the
506
+ * hooks around every operation. Consumer code does not call it directly — middleware is
507
+ * managed through the `AddMiddleware`/`RemoveMiddleware` messages.
508
+ */
326
509
  declare class PostboyMiddlewareService {
510
+ /** The middleware chain, in insertion order. */
327
511
  protected middlewares: PostboyMiddleware[];
512
+ /** Appends the middleware to the end of the chain; appending the same instance twice runs its hooks twice. */
328
513
  addMiddleware(middleware: PostboyMiddleware): void;
514
+ /** Removes the middleware by identity and calls its `dispose()` hook; unknown instances are ignored. */
329
515
  removeMiddleware(middleware: PostboyMiddleware): void;
516
+ /** Disposes every middleware in the chain and empties it. */
330
517
  dispose(): void;
518
+ /**
519
+ * Runs the `before` hooks for the stage, consulting `canHandle` on each middleware.
520
+ * Stops and throws {@link CancelError} as soon as one returns an interrupt decision.
521
+ *
522
+ * @throws CancelError With the stage, middleware name, and message id of the interruption.
523
+ */
331
524
  before<T extends PostboyMessage>(stage: MiddlewareStage, message: T): void;
525
+ /** Runs the `after` hooks for the stage, passing the executor result when there is one. */
332
526
  after<T extends PostboyMessage, R = unknown>(stage: MiddlewareStage, message: T, result?: R): void;
527
+ /** `before` hooks for the `Publish` stage (`PostboyService.fire`). */
333
528
  beforePublish(message: PostboyMessage): void;
529
+ /** `after` hooks for the `Publish` stage (`PostboyService.fire`). */
334
530
  afterPublish(message: PostboyMessage): void;
531
+ /** `before` hooks for the `Callback` stage (`PostboyService.fireCallback`). */
335
532
  beforeCallback(message: PostboyMessage): void;
533
+ /** `after` hooks for the `Callback` stage — run on every emitted result value. */
336
534
  afterCallback(message: PostboyMessage, result?: unknown): void;
535
+ /** `before` hooks for the `Execute` stage (`PostboyService.exec`). */
337
536
  beforeExecute<T>(message: PostboyExecutor<T>): void;
537
+ /** `after` hooks for the `Execute` stage, receiving the executor's return value. */
338
538
  afterExecute<T>(message: PostboyExecutor<T>, result: T): void;
339
539
  private buildContext;
340
540
  private throwIfCancelled;
341
541
  }
342
542
 
343
543
  /**
344
- * The PostboyMessageStore is a utility class for managing message subscriptions and executors.
345
- * It provides functionality to register, retrieve, and unregister subscription-based messages and executors.
544
+ * The registry behind the bus: message subscriptions and executor handlers keyed by
545
+ * their static `ID`, plus completion callbacks for fired callback messages. Consumer
546
+ * code reaches it only through `PostboyService` and the infrastructure messages.
346
547
  */
347
548
  declare class PostboyMessageStore {
348
549
  protected messages: Map<string, PostboySubscription<any>>;
349
550
  protected executors: Map<string, (e: PostboyExecutor<any>) => any>;
350
551
  protected callbacks: Map<string, (() => void)[]>;
552
+ /**
553
+ * Registers a message subscription under the id, logging a warning and overriding
554
+ * when the id is already taken.
555
+ */
351
556
  registerMessage(id: string, sub: PostboySubscription<any>): void;
557
+ /**
558
+ * Registers an executor handler under the id, logging a warning and overriding
559
+ * when the id is already taken.
560
+ */
352
561
  registerExecutor(id: string, executor: (e: PostboyExecutor<any>) => any): void;
562
+ /** Records a callback completing the message's result when its type is unregistered or the bus disposed. */
353
563
  callbackFired(message: PostboyCallbackMessage<any>): void;
564
+ /**
565
+ * @param id - The static `ID` the subscription was registered under.
566
+ * @param name - Used only in the error message.
567
+ * @throws Error When no message is registered under the id.
568
+ */
354
569
  getMessage(id: string, name: string): PostboySubscription<any>;
570
+ /**
571
+ * @param id - The static `ID` the handler was registered under.
572
+ * @throws Error When no executor is registered under the id.
573
+ */
355
574
  getExecutor<T>(id: string): (e: PostboyExecutor<T>) => T;
575
+ /**
576
+ * Removes an id completely: completes its subscription stream, runs the recorded
577
+ * completion callbacks (completing fired callback results), and deletes the message,
578
+ * executor, and callback entries.
579
+ */
356
580
  unregister(id: string): void;
581
+ /**
582
+ * Tears the store down: unregisters every message, runs the remaining callbacks, and
583
+ * clears all maps — including the infrastructure handlers, so the bus must be
584
+ * re-created to work again.
585
+ */
357
586
  dispose(): void;
358
587
  }
359
588
 
360
589
  /**
361
- * Represents a store for managing namespaces in the Postboy system.
362
- * Provides functionality to add, eliminate, and dispose namespaces.
590
+ * The registry of namespaces behind `AddNamespace`/`EliminateNamespace`: maps each name
591
+ * to the registrator managing its registrations.
363
592
  */
364
593
  declare class PostboyNamespaceStore {
365
594
  private spaces;
366
595
  /**
367
- * Adds a new space or retrieves an existing one if it already exists.
596
+ * Returns the registrator of the given name, creating it on first use.
368
597
  *
369
- * @param {string} space - The name of the space to add or retrieve.
370
- * @param {PostboyService} postboy - The PostboyService instance used to create a namespace registrator.
371
- * @return {PostboyAbstractRegistrator} The registrator associated with the specified space.
598
+ * @param space - The unique namespace name.
599
+ * @param postboy - The bus the new registrator executes its registration messages on.
600
+ * @return The registrator of the namespace — the existing one when the name is known.
372
601
  */
373
602
  addSpace(space: string, postboy: PostboyService): PostboyAbstractRegistrator;
374
603
  /**
375
- * Removes a specified space from the spaces collection if it exists.
376
- * If the space exists, it will be deleted after invoking its down method.
604
+ * Tears the namespace down `down()` on its registrator, disconnecting everything it
605
+ * recorded and removes it. Unknown names are ignored.
377
606
  *
378
- * @param {string} space - The name of the space to be removed.
379
- * @return {void} This method does not return a value.
607
+ * @param space - The namespace name to remove.
380
608
  */
381
609
  eliminateSpace(space: string): void;
382
- /**
383
- * Disposes of the current instance by performing cleanup operations.
384
- * Iterates through all spaces, performs a "down" operation on each,
385
- * and then clears the collection of spaces.
386
- *
387
- * @return {void} No return value.
388
- */
610
+ /** Tears down every namespace and clears the store; called by `PostboyService.dispose()`. */
389
611
  dispose(): void;
390
612
  }
391
613
 
614
+ /**
615
+ * Assembles the internal collaborators of a `PostboyService`: its middleware pipeline,
616
+ * message store, and namespace store. The default factories create fresh instances with
617
+ * no shared state; substitute stubs (e.g. in tests) by passing a custom resolver to the
618
+ * `PostboyService` constructor.
619
+ */
392
620
  declare class PostboyDependencyResolver {
393
- /**
394
- * Retrieves an instance of the PostboyMiddlewareService.
395
- *
396
- * This function initializes and returns a new instance of the
397
- * PostboyMiddlewareService, which can be used to configure and manage
398
- * middleware for a specific module or application.
399
- *
400
- * @returns {PostboyMiddlewareService} A new instance of PostboyMiddlewareService.
401
- */
621
+ /** Factory for the bus middleware pipeline — one fresh instance per call. */
402
622
  getMiddlewareService: () => PostboyMiddlewareService;
403
- /**
404
- * A function that instantiates and returns a new instance of PostboyMessageStore.
405
- *
406
- * This function serves as a factory method for creating instances
407
- * of the PostboyMessageStore class.
408
- *
409
- * @returns {PostboyMessageStore} A new instance of the PostboyMessageStore class.
410
- */
623
+ /** Factory for the message registry — one fresh instance per call. */
411
624
  getMessageStore: () => PostboyMessageStore;
412
- /**
413
- * Creates and initializes a new instance of PostboyNamespaceStore using the provided PostboyService instance.
414
- *
415
- * @function getNamespaceStore
416
- * @returns {PostboyNamespaceStore} A new instance of PostboyNamespaceStore associated with the given PostboyService.
417
- */
625
+ /** Factory for the namespace registry — one fresh instance per call. */
418
626
  getNamespaceStore: () => PostboyNamespaceStore;
419
627
  }
420
628
 
629
+ /**
630
+ * The central message bus of the postboy library.
631
+ *
632
+ * All routing is keyed by the static `ID` declared on message and executor classes —
633
+ * not by class identity. Three kinds of traffic are supported:
634
+ * - pub/sub: register a message type with a `ConnectMessage`, subscribe via {@link sub},
635
+ * dispatch via {@link fire};
636
+ * - synchronous commands: register a handler with a `ConnectExecutor`/`ConnectHandler`,
637
+ * invoke it via {@link exec};
638
+ * - async request/response: a {@link PostboyCallbackMessage} fired via {@link fireCallback}.
639
+ *
640
+ * Every bus mutation — registration, middleware, locking, namespaces — is performed by
641
+ * executing one of the infrastructure messages via {@link exec}; the constructor wires
642
+ * their handlers automatically.
643
+ *
644
+ * @example
645
+ * ```ts
646
+ * class PingMessage extends PostboyGenericMessage {
647
+ * static readonly ID = 'app.ping';
648
+ * constructor(public text: string) {
649
+ * super();
650
+ * }
651
+ * }
652
+ *
653
+ * const postboy = new PostboyService();
654
+ * postboy.exec(new ConnectMessage(PingMessage, new Subject<PingMessage>()));
655
+ * postboy.sub(PingMessage).subscribe((m) => console.log(m.text));
656
+ * postboy.fire(new PingMessage('hello'));
657
+ * ```
658
+ */
421
659
  declare class PostboyService {
660
+ /** Ids of message types locked via `LockMessage`; {@link fire} and {@link fireCallback} skip their delivery. */
422
661
  protected locked: Set<string>;
423
662
  private middleware;
424
663
  private store;
425
664
  private namespaceStore;
426
665
  private dependencyResolver;
666
+ /**
667
+ * Creates a bus and registers handlers for all infrastructure messages.
668
+ *
669
+ * @param resolver - Supplies the internal collaborators (middleware pipeline, message store, namespace store).
670
+ * Defaults to a `PostboyDependencyResolver` with fresh instances; pass a custom one to inject test doubles.
671
+ */
427
672
  constructor(resolver?: PostboyDependencyResolver);
673
+ /** Registers handlers for the infrastructure messages — the only way to mutate the bus. */
428
674
  private registerInfrastructureMessages;
429
675
  /**
430
- * Fires a registered event and passes the message to its subscribers.
676
+ * Publishes a message to all current subscribers of its type.
677
+ *
678
+ * Runs the `Publish`-stage middleware `before` hooks, delivers the message to the
679
+ * registered subject, then runs the `after` hooks. If the type is locked (see
680
+ * `LockMessage`), delivery is silently skipped — subscribers receive nothing — but
681
+ * both middleware hooks still run.
431
682
  *
432
- * @param {PostboyGenericMessage} message - The message object containing the event data.
433
- * @return {void} This method does not return a value.
434
- * @throws {Error} Throws an error if no registered event is found for the provided message ID.
683
+ * @param message - The message instance to publish.
684
+ * @throws CancelError When a `Publish`-stage middleware returns an interrupt decision.
685
+ * @throws Error When no message of this type is registered; the `after` hooks are then skipped.
435
686
  */
436
687
  fire(message: PostboyGenericMessage): void;
437
688
  /**
438
- * Triggers a callback function associated with a given message.
439
- *
440
- * @param {PostboyCallbackMessage<T>} message - The message object used to trigger the callback.
441
- * It contains details about the event and result subscription.
442
- * @param {(e: T) => void} [action] - Optional callback function to execute when the result of the message is emitted.
443
- * @return {void} This method does not return any value.
689
+ * Fires a {@link PostboyCallbackMessage} and returns an observable of its result (async request/response).
690
+ *
691
+ * The responder side subscribes to the message type via {@link sub} and produces the
692
+ * result with `message.next(...)` / `message.finish(...)`.
693
+ *
694
+ * Dispatch semantics depend on `action`:
695
+ * - with `action`, the message is dispatched immediately and `action` is invoked once
696
+ * per emitted result value, independently of any subscriptions to the returned
697
+ * observable;
698
+ * - without `action`, dispatch is lazy: it happens on the first subscription to the
699
+ * returned observable.
700
+ *
701
+ * `Callback`-stage middleware `before` hooks run at call time; `after` hooks run on
702
+ * every emitted result value. If the type is locked, dispatch is silently skipped.
703
+ * The result observable completes when the message type is disconnected
704
+ * (see `DisconnectMessage`) or the bus is disposed.
705
+ *
706
+ * @param message - The callback message carrying the request.
707
+ * @param action - Optional callback invoked once per emitted result value.
708
+ * @return An observable emitting the result values produced by the responder.
709
+ * @throws CancelError When a `Callback`-stage middleware returns an interrupt decision.
710
+ * @throws Error When no message of this type is registered; thrown synchronously, before any dispatch.
711
+ *
712
+ * @example
713
+ * ```ts
714
+ * postboy.exec(new ConnectMessage(FetchDataMessage, new Subject<FetchDataMessage>()));
715
+ * // responder: produces the result
716
+ * postboy.sub(FetchDataMessage).subscribe((m) => m.finish('payload'));
717
+ * // requester: consumes it
718
+ * postboy.fireCallback(new FetchDataMessage()).subscribe((payload) => console.log(payload));
719
+ * ```
444
720
  */
445
721
  fireCallback<T>(message: PostboyCallbackMessage<T>, action?: (e: T) => void): Observable<T>;
446
722
  /**
447
- * Executes the provided executor function and returns its result.
723
+ * Synchronously executes a registered executor command and returns its result — never `await` it.
724
+ *
725
+ * Runs the `Execute`-stage middleware `before` hooks, invokes the handler registered
726
+ * for the executor's static `ID`, then the `after` hooks with the result. For async
727
+ * results use a {@link PostboyCallbackMessage} with {@link fireCallback} instead.
448
728
  *
449
- * @param {PostboyExecutor<T>} executor The executor to be executed, which includes its identifier and logic.
450
- * @return {T} The resulting output from the executed executor function.
451
- * @throws {Error} If the specified executor is not registered.
729
+ * This is also the entry point for the infrastructure messages themselves
730
+ * (`ConnectMessage`, `AddMiddleware`, ...), so middleware sees them on the `Execute`
731
+ * stage too filter them out with `canHandle` if needed.
732
+ *
733
+ * @param executor - The executor instance carrying the command.
734
+ * @return Whatever the registered handler returns.
735
+ * @throws CancelError When an `Execute`-stage middleware returns an interrupt decision.
736
+ * @throws Error When no handler is registered for this executor type.
452
737
  */
453
738
  exec<T>(executor: PostboyExecutor<T>): T;
454
739
  /**
455
- * Subscribes to a specific message type and returns an observable of that type.
740
+ * Returns the observable stream of a registered message type.
741
+ *
742
+ * Every call returns a view of the one registered subject — or of its pipe, when the
743
+ * type was registered via `ConnectMessage` with a pipe. It is an `Observable`, not a
744
+ * `Subject`: never call `next` on it, emit via {@link fire}. Subscribers only receive
745
+ * messages fired after their subscription, unless the type was registered with a
746
+ * replay or behavior subject.
456
747
  *
457
- * @param type The constructor function of the type that extends PostboyGenericMessage.
458
- * @return An Observable of the specified generic message type.
748
+ * @param type - The constructor of the message type; must declare its own static `ID`.
749
+ * @throws Error When the class has no static `ID` or no message of this type is registered.
459
750
  */
460
751
  sub<T extends PostboyGenericMessage>(type: MessageType<T>): Observable<T>;
461
752
  /**
462
- * Subscribes to a specific message type and automatically unsubscribes after receiving the first message.
753
+ * Like {@link sub}, but completes right after the first message of the type arrives.
463
754
  *
464
- * @param type The type of message to subscribe to.
465
- * @return An observable that emits the first message of the specified type and then completes.
755
+ * @param type - The constructor of the message type; must declare its own static `ID`.
756
+ * @return An observable that emits one message and then completes.
757
+ * @throws Error When the class has no static `ID` or no message of this type is registered.
466
758
  */
467
759
  once<T extends PostboyGenericMessage>(type: MessageType<T>): Observable<T>;
468
760
  /**
469
- * Registers a given message type and its associated subject subscription with the system.
761
+ * Registers a message type with the given subject.
762
+ *
763
+ * Re-registering the same `ID` logs a warning and overrides the previous registration.
470
764
  *
471
765
  * @deprecated The method should be replaced with firing {@link ConnectMessage} message.
472
- * @param type The constructor function of the message type that extends the PostboyGenericMessage.
473
- * @param sub The Subject instance for the provided message type, used for managing subscriptions.
474
- * @return {void} No return value.
766
+ * @param type - The constructor of the message type; must declare its own static `ID`.
767
+ * @param sub - The subject subscribers will observe.
475
768
  */
476
769
  record<T extends PostboyGenericMessage>(type: MessageType<T>, sub: Subject<T>): void;
477
770
  /**
478
- * Registers a generic message type with a Subject and a transformation pipe.
771
+ * Registers a message type whose stream is transformed by a pipe before reaching subscribers.
772
+ *
773
+ * Re-registering the same `ID` logs a warning and overrides the previous registration.
479
774
  *
480
775
  * @deprecated The method should be replaced with firing {@link ConnectMessage} message.
481
- * @param {MessageType<T>} type - The constructor of the message type being registered.
482
- * @param {Subject<T>} sub - The Subject instance used to handle incoming messages of the specified type.
483
- * @param {(s: Subject<T>) => Observable<T>} pipe - A function that applies a transformation or processing logic to the Subject and returns an Observable.
484
- * @return {void} No return value.
776
+ * @param type - The constructor of the message type; must declare its own static `ID`.
777
+ * @param sub - The subject subscribers will observe.
778
+ * @param pipe - Wraps the subject into the observable handed out by {@link sub}, e.g. to apply operators.
485
779
  */
486
780
  recordWithPipe<T extends PostboyGenericMessage>(type: MessageType<T>, sub: Subject<T>, pipe: (s: Subject<T>) => Observable<T>): void;
487
781
  /**
488
- * Registers an executor for a specified message type.
782
+ * Registers a synchronous handler for an executor type, invoked by {@link exec}.
783
+ *
784
+ * Re-registering the same `ID` logs a warning and overrides the previous registration.
489
785
  *
490
786
  * @deprecated The method should be replaced with firing {@link ConnectExecutor} message.
491
- * @param {MessageType<E>} type - The message type for which the executor is being registered.
492
- * @param {(e: E) => T} exec - The executor function that will handle messages of the specified type.
493
- * @return {void} This method does not return any value.
787
+ * @param type - The constructor of the executor class; must declare its own static `ID`.
788
+ * @param exec - Called with the executor instance on every {@link exec} of this type.
494
789
  */
495
790
  recordExecutor<E extends PostboyExecutor<T>, T>(type: MessageType<E>, exec: (e: E) => T): void;
496
791
  /**
497
- * Registers a handler for a specific executor type.
792
+ * Registers a {@link PostboyExecutionHandler} instance for an executor type, invoked by {@link exec}.
793
+ *
794
+ * Re-registering the same `ID` logs a warning and overrides the previous registration.
498
795
  *
499
796
  * @deprecated The method should be replaced with firing {@link ConnectHandler} message.
500
- * @param executor The constructor of the executor class that extends `PostboyExecutor<R>`.
501
- * @param handler An instance of `PostboyExecutionHandler<R, E>` that defines the logic for handling the executor.
502
- * @return void
797
+ * @param executor - The constructor of the executor class; must declare its own static `ID`.
798
+ * @param handler - Its `handle` method is called with the executor instance on every {@link exec} of this type.
503
799
  */
504
800
  recordHandler<E extends PostboyExecutor<R>, R>(executor: new (...args: any[]) => E, handler: PostboyExecutionHandler<R, E>): void;
505
801
  /**
506
- * Disposes of resources and cleans up any internal components or stores associated with the instance.
507
- * This method ensures that all resources are properly released to avoid memory leaks.
802
+ * Tears down the whole bus: calls `down()` on every namespace registrator (completing
803
+ * everything they registered), completes all remaining message subscriptions and
804
+ * callback results, and disposes every middleware.
508
805
  *
509
- * @return {void} This method does not return a value.
806
+ * Infrastructure registrations are re-created only by constructing a new service.
510
807
  */
511
808
  dispose(): void;
512
809
  }
513
810
 
514
811
  /**
515
- * Represents a {@link PostboyMiddleware} addition operation for Postboy.
516
- * This class extends the functionality of the PostboyExecutor to add middleware to the processing chain.
812
+ * Infrastructure message that appends a {@link PostboyMiddleware} to the pipeline.
813
+ *
814
+ * Executing it via `PostboyService.exec` adds the middleware to the end of the chain:
815
+ * hooks then run for every stage its `canHandle` accepts, in insertion order. Adding
816
+ * the same instance twice makes its hooks run twice.
517
817
  *
518
- * The middleware to be added is provided during instantiation.
818
+ * @example
819
+ * ```ts
820
+ * postboy.exec(new AddMiddleware(new LoggingMiddleware()));
821
+ * ```
519
822
  */
520
823
  declare class AddMiddleware extends PostboyExecutor<void> {
521
824
  middleware: PostboyMiddleware;
522
825
  static readonly ID = "0a8cfe0a-6193-4082-8440-d0793367b21d";
523
826
  /**
524
- * Initializes a new instance of the class with the specified middleware.
525
- *
526
- * @param {PostboyMiddleware} middleware - The {@link PostboyMiddleware} to be used for processing.
827
+ * @param middleware - The middleware instance to append.
527
828
  */
528
829
  constructor(middleware: PostboyMiddleware);
529
830
  }
530
831
 
531
832
  /**
532
- * Represents a class responsible for removing {@link PostboyMiddleware} in execution flow.
533
- * This class extends PostboyExecutor and operates with a void return type.
833
+ * Infrastructure message that removes a {@link PostboyMiddleware} from the pipeline.
834
+ *
835
+ * Executing it via `PostboyService.exec` drops the instance from the chain — matched by
836
+ * identity, not by name or class — and calls its `dispose()` hook. Pass the very same
837
+ * instance that was added via `AddMiddleware`.
534
838
  */
535
839
  declare class RemoveMiddleware extends PostboyExecutor<void> {
536
840
  middleware: PostboyMiddleware;
537
841
  static readonly ID = "c25c708c-53c9-498d-a28b-936fbaf68b91";
538
842
  /**
539
- * Constructs an instance of the class with the specified middleware.
540
- *
541
- * @param {PostboyMiddleware} middleware - The {@link PostboyMiddleware} instance to be removed.
843
+ * @param middleware - The middleware instance previously added via `AddMiddleware`.
542
844
  */
543
845
  constructor(middleware: PostboyMiddleware);
544
846
  }
545
847
 
546
848
  /**
547
- * Locks a specific message type to prevent firing of them.
849
+ * Infrastructure message that locks a message type on the bus.
548
850
  *
549
- * This class is used to handle operations associated with locking mechanisms
550
- * for a specified message type.
851
+ * Executing it via `PostboyService.exec` adds the type's static `ID` to the locked set:
852
+ * subsequent `fire()` and `fireCallback()` calls for that type silently skip delivery —
853
+ * subscribers receive nothing — while middleware `before`/`after` hooks still run and
854
+ * `exec()` is unaffected. Registration stays intact, so unlocking resumes delivery
855
+ * immediately. Treat a locked message as a no-op, not an error.
551
856
  *
552
- * @template T - A type that extends {@link PostboyGenericMessage}.
857
+ * @example
858
+ * ```ts
859
+ * postboy.exec(new LockMessage(PingMessage)); // PingMessage stops being delivered
860
+ * postboy.exec(new UnlockMessage(PingMessage)); // delivery resumes
861
+ * ```
553
862
  */
554
863
  declare class LockMessage<T extends PostboyGenericMessage> extends PostboyExecutor<void> {
555
864
  type: MessageType<T>;
556
865
  static readonly ID = "477df3e2-1f99-4476-9a3b-afd1fa426436";
557
866
  /**
558
- * Constructs an instance of the class with the specified message type.
559
- *
560
- * @param {MessageType<T>} type - The type of the message to be used for the instance.
867
+ * @param type - The constructor of the message type to lock.
561
868
  */
562
869
  constructor(type: MessageType<T>);
563
870
  }
564
871
 
565
872
  /**
566
- * A specialized executor that handles the unlocking process for messages of a specified type.
567
- * Unlocks a previously locked message, making it available for processing again.
873
+ * Infrastructure message that unlocks a message type locked by `LockMessage`.
568
874
  *
569
- * @template T - The type parameter extending {@link PostboyGenericMessage}, representing the message type handled by the executor.
570
- * @extends {PostboyExecutor<void>}
875
+ * Executing it via `PostboyService.exec` removes the type's static `ID` from the locked
876
+ * set, so `fire()` and `fireCallback()` deliver it again. Unlocking a type that was
877
+ * never locked changes nothing.
571
878
  */
572
879
  declare class UnlockMessage<T extends PostboyGenericMessage> extends PostboyExecutor<void> {
573
880
  type: MessageType<T>;
574
881
  static readonly ID = "d71d25e3-90ac-4009-b972-9e6c6b05611e";
575
882
  /**
576
- * Creates an instance of the class with the specified message type.
577
- *
578
- * @param {MessageType<T>} type - The message type for this instance.
883
+ * @param type - The constructor of the message type to unlock.
579
884
  */
580
885
  constructor(type: MessageType<T>);
581
886
  }
582
887
 
583
888
  /**
584
- * AddNamespace is a class that extends the PostboyExecutor with a specific implementation
585
- * for adding namespaces to a PostboyService instance.
889
+ * Infrastructure message that creates or returns the existing registrator of a
890
+ * namespace.
891
+ *
892
+ * Executing it via `PostboyService.exec` registers the given name in the namespace
893
+ * store and returns the {@link PostboyAbstractRegistrator} for it: record messages and
894
+ * executors on that registrator, then tear them all down at once with
895
+ * `EliminateNamespace` (or the registrator's own `down()`). Executing it again with the
896
+ * same name returns the same registrator without recreating it.
586
897
  *
587
- * This class is identified uniquely by its static ID property for tracking and referencing purposes.
898
+ * @example
899
+ * ```ts
900
+ * const reg = postboy.exec(new AddNamespace('feature-a'));
901
+ * reg.recordSubject(PingMessage);
902
+ * // later: postboy.exec(new EliminateNamespace('feature-a')); — disconnects everything recorded
903
+ * ```
588
904
  */
589
905
  declare class AddNamespace extends PostboyExecutor<PostboyAbstractRegistrator> {
590
906
  space: string;
591
907
  static readonly ID = "6d1a6f7d-6b6e-4c4d-8af8-9cc9a32e850c";
592
908
  /**
593
- * Creates an instance of the class with the specified space identifier.
594
- *
595
- * @param {string} space - The identifier for the space.
909
+ * @param space - The unique name of the namespace.
596
910
  */
597
911
  constructor(space: string);
598
912
  }
599
913
 
600
914
  /**
601
- * Represents an executor that eliminates a specific namespace.
602
- * This class extends the PostboyExecutor with a void return type.
915
+ * Infrastructure message that tears down a namespace created by `AddNamespace`.
916
+ *
917
+ * Executing it via `PostboyService.exec` calls `down()` on the namespace's registrator —
918
+ * disconnecting every message and executor it recorded — and removes the namespace.
919
+ * Eliminating an unknown name changes nothing.
603
920
  */
604
921
  declare class EliminateNamespace extends PostboyExecutor<void> {
605
922
  space: string;
606
923
  static readonly ID = "03bb03bb-53e0-4b74-9aad-64d5c54a8972";
607
924
  /**
608
- * Creates an instance of the class with the specified space value.
609
- *
610
- * @param {string} space - The string value representing the space configuration.
925
+ * @param space - The name the namespace was created with.
611
926
  */
612
927
  constructor(space: string);
613
928
  }
614
929
 
615
930
  /**
616
- * Represents a connection handler that extends the PostboyExecutor class.
931
+ * Infrastructure message that registers a {@link PostboyExecutionHandler} for an
932
+ * executor type.
617
933
  *
618
- * This class encapsulates logic associated with executing a handler in a structured way.
619
- * It is generic and operates on the provided executor and handler types.
934
+ * Executing it via `PostboyService.exec` binds the executor class's static `ID` to the
935
+ * handler: every subsequent `exec` of that type calls `handler.handle(...)` and returns
936
+ * its result. Re-registering the same `ID` logs a warning and overrides the previous
937
+ * registration. The non-deprecated replacement for `PostboyService.recordHandler`.
620
938
  *
621
- * Type Parameters:
622
- * E - Represents an extension of the PostboyExecutor class with a specific type parameter R.
623
- * R - Represents the type of the result that the executor is expected to operate on.
939
+ * @template E - The executor type being served.
940
+ * @template R - The result its handler returns.
624
941
  */
625
942
  declare class ConnectHandler<E extends PostboyExecutor<R>, R> extends PostboyExecutor<void> {
626
943
  executor: new (...args: any[]) => E;
627
944
  handler: PostboyExecutionHandler<R, E>;
628
945
  static readonly ID = "bf618cea-6f32-417c-9548-8eafe937378b";
629
946
  /**
630
- * Constructs an instance of the class.
631
- *
632
- * @param {new (...args: any[]) => E} executor - A constructor function for the executor object.
633
- * @param {PostboyExecutionHandler<R, E>} handler - A handler that processes the execution logic.
947
+ * @param executor - The constructor of the executor class; must declare its own static `ID`.
948
+ * @param handler - The handler whose `handle` method receives the executor.
634
949
  */
635
950
  constructor(executor: new (...args: any[]) => E, handler: PostboyExecutionHandler<R, E>);
636
951
  }
637
952
 
638
953
  /**
639
- * Registers an executor that connects to a specific type of message handler.
640
- * This class extends the functionality of `PostboyExecutor` and is designed to manage
641
- * the execution of a handler defined by a specific message type and an execution function.
954
+ * Infrastructure message that registers a synchronous handler for an executor type.
642
955
  *
643
- * @template E The type of the executor extending {@link PostboyExecutor}.
644
- * @template T The return type of the execution function.
956
+ * Executing it via `PostboyService.exec` binds the type's static `ID` to the given
957
+ * function: every subsequent `exec` of that executor type invokes it and returns its
958
+ * result. Re-registering the same `ID` logs a warning and overrides the previous
959
+ * registration. The non-deprecated replacement for `PostboyService.recordExecutor`.
645
960
  *
646
- * @extends {PostboyExecutor<void>}
961
+ * @template E - The executor type being registered.
962
+ * @template T - The result its handler returns.
647
963
  */
648
964
  declare class ConnectExecutor<E extends PostboyExecutor<T>, T> extends PostboyExecutor<void> {
649
965
  type: MessageType<E>;
650
966
  exec: (e: E) => T;
651
967
  static readonly ID = "cb80e8ad-b68c-4b2d-8c44-617ea6017cb3";
652
968
  /**
653
- * Constructs an instance of the class with the specified type and execution function.
654
- *
655
- * @param {MessageType<E>} type - The type of the message.
656
- * @param {(e: E) => T} exec - The function to be executed with the input of type E that returns a value of type T.
969
+ * @param type - The constructor of the executor class; must declare its own static `ID`.
970
+ * @param exec - The handler invoked with the executor instance on every `exec` of this type.
657
971
  */
658
972
  constructor(type: MessageType<E>, exec: (e: E) => T);
659
973
  }
660
974
 
661
975
  /**
662
- * Represents a message signaling a disconnection event. This class is used
663
- * within the framework to manage and process disconnection notifications.
664
- * Extends the {@link PostboyExecutor} base class.
976
+ * Infrastructure message that unregisters a message or executor type from the bus.
665
977
  *
666
- * @extends {PostboyExecutor<void>}
978
+ * Executing it via `PostboyService.exec` completes the registered stream (ending every
979
+ * subscriber's subscription), runs the registered completion callbacks — in particular,
980
+ * it completes the result of a fired callback message — and forgets the `ID`, so
981
+ * further `fire`, `sub`, and `exec` for it throw until it is registered again. This is
982
+ * the teardown primitive behind `PostboyAbstractRegistrator.down()`.
667
983
  */
668
984
  declare class DisconnectMessage extends PostboyExecutor<void> {
669
985
  messageId: string;
670
986
  static readonly ID = "94579e43-5bc9-4517-bcda-b595bcda1ae7";
671
987
  /**
672
- * Creates an instance of the class with the specified message identifier.
673
- *
674
- * @param {string} messageId - The unique identifier for the message.
988
+ * @param messageId - The static `ID` of the message or executor type to remove — not an instance id.
675
989
  */
676
990
  constructor(messageId: string);
677
991
  }
678
992
 
679
993
  /**
680
- * Represents a message that facilitates connection functionality
681
- * within the application's messaging system.
994
+ * Infrastructure message that registers a pub/sub message type on the bus.
682
995
  *
683
- * @template T - A type that extends {@link PostboyGenericMessage}, representing
684
- * the structure of the message being handled.
996
+ * Executing it via `PostboyService.exec` binds the type's static `ID` to the given
997
+ * subject: `sub()` and `once()` start returning its (piped) stream, and `fire()`
998
+ * delivers into it. Re-registering the same `ID` logs a warning and overrides the
999
+ * previous registration. The non-deprecated replacement for the `PostboyService.record*`
1000
+ * methods.
685
1001
  *
686
- * @extends PostboyExecutor<void>
1002
+ * @example
1003
+ * ```ts
1004
+ * postboy.exec(new ConnectMessage(PingMessage, new Subject<PingMessage>()));
1005
+ * // with a pipe — subscribers see the transformed stream
1006
+ * postboy.exec(new ConnectMessage(PingMessage, new Subject<PingMessage>(), (s) => s.pipe(share())));
1007
+ * ```
687
1008
  */
688
1009
  declare class ConnectMessage<T extends PostboyGenericMessage> extends PostboyExecutor<void> {
689
1010
  type: MessageType<T>;
@@ -691,11 +1012,9 @@ declare class ConnectMessage<T extends PostboyGenericMessage> extends PostboyExe
691
1012
  pipe?: ((s: Subject<T>) => Observable<T>) | undefined;
692
1013
  static readonly ID = "aa03a192-bdc7-402d-9f2f-bf3748229ea2";
693
1014
  /**
694
- * Constructs a new instance of the class.
695
- *
696
- * @param {MessageType<T>} type - The type of the message.
697
- * @param {Subject<T>} sub - The subject to be used for message handling.
698
- * @param {(s: Subject<T>) => Observable<T>} [pipe] - An optional function to transform the subject.
1015
+ * @param type - The constructor of the message type; must declare its own static `ID`.
1016
+ * @param sub - The subject the message type is served from.
1017
+ * @param pipe - Optional wrapper producing the observable subscribers receive, e.g. to apply operators.
699
1018
  */
700
1019
  constructor(type: MessageType<T>, sub: Subject<T>, pipe?: ((s: Subject<T>) => Observable<T>) | undefined);
701
1020
  }