An after-command handler is a callback that runs after every handler for a dispatched command has completed. It provides a reliable point for notifying the renderer that state may have changed. section 5.1.3
A dispatcher can support after-command handlers with three small responsibilities:
- Store callbacks in a private #afterHandlers array. section 5.1.3
- Register a callback with afterEveryCommand(handler), without requiring a command name. Registration returns an unsubscribe function that removes the callback. section 5.1.3
- Invoke every registered callback after the command-specific handlers finish, regardless of whether the command has handlers. section 5.1.3
- Allow duplicate after-command registrations. Unlike command subscriptions, these notifications do not modify state, so repeated delivery can be valid. section 5.1.3
In an application state manager, register renderApp as an after-command handler. Reducer-backed command handlers update state first; renderApp then re-renders because state changes cannot be known in advance. Comparing state before and after each command is deliberately deferred because deeply nested comparisons can be expensive. section 5.2.3
Logging after a greeting command
Register a command handler that logs "Hello, John!" and an after-command handler that logs "Done greeting!". Dispatching dispatcher.dispatch('greet', 'John') produces the greeting first and the completion message second. section 5.1.4
