Pessimistic scenarios in optimistic updates with LiveView

Yes, hook or a global listener. See:

There’s also an unreleased improvement in main related to this use case, as discussed in Make `JS.t()` a public data structure, or json serializable and implemented in Support pushing JS commands via events by rhcarvalho · Pull Request #4060 · phoenixframework/phoenix_live_view · GitHub .

The relevant piece of documentation in main:

The good news is that with the latest LV release (v1.1.28), i.e. without the improvement above, you can still follow the “common case” documented above, and pre-encode your command(s) as a data attribute in some element.

Note that you don’t have to restrict you JavaScript event handler to execute LV’s “JS Commands”, you can run any JavaScript code you want.