Skip to main content

readonlyMiddleware

Overview

readonlyMiddleware(node) rejects actions started on a node or any of its descendants, effectively making that subtree read-only. It returns dispose, which removes the middleware, and allowWrite, which permits actions started by the supplied callback.

Example:

// given a model instance named `todo`
const { dispose, allowWrite } = readonlyMiddleware(todo)

// this will throw
todo.setDone(false)
await todo.setDoneAsync(false)

// this will work
allowWrite(() => todo.setDone(false))
// For async actions, start exactly one action per `allowWrite` call.
await allowWrite(() => todo.setDoneAsync(false))

allowWrite authorizes the action invocation, not arbitrary work performed later by a promise callback. For flows, keep the full mutation sequence inside the single model flow started by allowWrite.