Function readonlyMiddleware

  • Attaches an action middleware that will throw when any action is started over the node or any of the child nodes, thus effectively making the subtree readonly.

    It will return an object with a dispose function to remove the middleware and a allowWrite function that will allow actions to be started inside the provided code block.

    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))
    // note: for async always use one action invocation per allowWrite!
    await allowWrite(() => todo.setDoneAsync(false))

    Parameters

    • subtreeRoot: object

      Subtree root target object.

    Returns ReadonlyMiddlewareReturn

    An object with the middleware disposer (dispose) and a allowWrite function.