You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import{IMiddlewareHandler}from"mobx-state-tree";typeForceWriteWrapper=(fn: ()=>void)=>void;constcreateReadonlyMiddleware=(): [IMiddlewareHandler,ForceWriteWrapper]=>{letwriteLock=0;constmiddleware: IMiddlewareHandler=(call,next)=>{if(writeLock<=0){thrownewError(`tried to invoke action '${call.name}' over a readonly node`);}else{returnnext(call);}};constforceWrite: ForceWriteWrapper=(fn)=>{try{writeLock++;fn();}finally{writeLock--;}};return[middleware,forceWrite];};
usage:
const [readonlyMiddleware, forceWrite] = createReadonlyMiddleware();
addMiddleware(node, readonlyMiddleware);
node.setX(12345); // will throw
node.child.setX(12345); // will throw
// if a change wants to be made
forceWrite(() => {
node.setX(1234);
node.child.setX(12345);
});
do you think it would be useful to you / should be in mst-middlewares?
The text was updated successfully, but these errors were encountered:
I just created a readonly middleware
usage:
do you think it would be useful to you / should be in mst-middlewares?
The text was updated successfully, but these errors were encountered: