Postboy Help

Lifecycle

The executor lifecycle covers the full path from registration to the moment the caller receives a result.

1. Registration (one‑time)

Before an executor can be used, its logic must be registered.
This happens once, typically during application bootstrap.

import {ConnectExecutor, PostboyExecutor} from '@artstesh/postboy'; export class FormatDateExecutor extends PostboyExecutor<string> { static readonly ID = 'date.format'; constructor(public readonly date: Date) { super(); } } postboy.exec(new ConnectExecutor(FormatDateExecutor, (executor) => { return Intl.DateTimeFormat('en-US').format(executor.date); }));

From this point, the bus knows how to handle FormatDateExecutor.

2. Execution call

Any code that needs the operation passes an executor instance to postboy.exec(…).

const result = postboy.exec(new FormatDateExecutor(new Date()));

3. Middleware hooks

Before and after the handler runs, middleware can inspect, block, or augment the execution.

  • beforeExecute(executor) — can throw to cancel execution.

  • afterExecute(executor, result) — can log, measure, or modify the result.

Middleware does not replace the handler; it only observes the lifecycle.

4. Handler execution

Postboy resolves the registered logic (function or handler class) and passes the executor instance to it.

// Internally, the bus effectively calls: // registeredFunction(executor) // or handler.handle(executor)

The handler performs the actual work and returns the typed result.

5. Result returned

The result is passed back to the caller synchronously.

const formatted = postboy.exec(new FormatDateExecutor(new Date())); console.log(formatted); // "4/23/2026"

Full lifecycle in sequence

Registration (executor + logic) ↓ bus.exec(executor) ↓ middleware.beforeExecute(executor) ↓ handler(executor) → result ↓ middleware.afterExecute(executor, result) ↓ return result to caller

Key points

  • Registration is separate from execution.

  • The lifecycle is synchronous from the caller’s point of view.

  • Middleware is the only place where cross‑cutting concerns (logging, validation) belong.

  • The handler remains a pure unit of work, testable in isolation.

24 апреля 2026