liveBook · Manning

After-command handler

As explained in Build a Frontend Web Framework (From Scratch)

An after-command handler is a callback that runs after the handlers for a dispatched command have completed.

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

How Build a Frontend Web Framework (From Scratch) covers it

How deep it goes
Defines registration, unsubscription, duplicate handling, and dispatch order, but avoids state-change comparison optimization.
Examples
Uses renderApp integration and a greet command that logs completion after its command handler runs.
What it assumes
Assumes command dispatch and handlers, while introducing renderer notification and state-manager integration incrementally.
What you can do after
Build it from scratch with afterEveryCommand, unsubscribe callbacks, and post-dispatch callback execution.
How it connects
Connects the dispatcher, commands, command handlers, reducers, state manager, and renderer.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build a Frontend Web Framework (From Scratch). The section links point to the parts of the book it draws on.