Callbacks
A callback message is an asynchronous request/response. The sender fires it and receives an Observable<T> of the result; a responder subscribes to the message and completes it. Callbacks extend PostboyCallbackMessage<T> and use the fireCallback verb. When no result is needed, use an event; when the result must be synchronous, use an executor.
When to use callbacks
Use callbacks when the sender depends on a response that arrives asynchronously: queries, remote calls, long operations with progress. Do not use them for pure notifications (events) or synchronous lookups (executors).
Anatomy of PostboyCallbackMessage<T>
Member | Purpose |
|---|---|
| the stream the sender consumes |
| emit a partial or interim result |
| emit the final result and complete |
| complete the stream without a final value |
The responder pattern
One side registers the message and answers requests by calling finish:
The other side asks and consumes the result:
The sender does not know whether the responder answered from a cache, an HTTP call, or a timer — only that a User | null arrives.
fireCallback and lazy dispatch
The signature is fireCallback<T>(message, action?): Observable<T>. Without action, the dispatch is lazy: the message is sent only when the returned Observable is subscribed.
Pass action — or subscribe immediately — when the request must be sent right away. The action callback runs on every emitted value:
Progress with next and finish
next emits interim results; finish emits the last one and completes the stream:
Typed responses
T documents the whole contract. When several outcomes are possible, model them explicitly:
The sender can branch exhaustively over result.status — no ambiguous null.
Pitfalls
A responder that never calls
finishorcompleteleaves the sender's subscription open forever.Throwing inside the responder does not error the sender's stream — translate failures into the result (see Callback Patterns).
fireCallbackwithoutactionand without a subscription never sends the request.Re-firing the same message instance re-subscribes its
action; use a fresh instance per request.Calling
nextorfinishfrom anywhere other than the responder'ssubhandler — the sender only subscribes toresult.
Next steps
Callback Patterns — async flows, errors, cancellation, composition