mobx-command
This is an implementation of Command pattern for MobX. It takes two functions, execute
and canExecute
,
both can return a promise that will be used for tracking purposes.
Available via NPM:
npm install mobx-command
Tests sumarize the api well, here is short version:
; ; //Mobx computed, combines isExecuting and canExecuteFromFn,//use this check for most cases, i.e. prevent fast clicks on same button on ui.//It is up to the user to check this flag before calling executeuserDetailsVm.resetPassword.canExecuteCombined; //execute command if canExecuteCombined is true. ; //execute command. Does not perform checks, forces execution.; //more granular flags, all mobx observable or computed //true while command is running, if it returns a promise to track this.//Typically used to indicate busy on a ui button invoking the commanduserDetailsVm.resetPassword.isExecuting; //Result of canExecute function; //If canExecute returns Promise<boolean> - will be false while check is runninguserDetailsVm.resetPassword.canExecuteFromFn; //true while canExecute is running, if it returns a promise to track thisuserDetailsVm.resetPassword.isCanExecuteAsyncRunning; //If canExecute returned a promise that was ultimately rejected - //this will contain rejection reasonuserDetailsVm.resetPassword.canExecuteAsyncRejectReason; //Raw computed from canExecute function, so you can subscribe to its recalculations, if neededuserDetailsVm.resetPassword.canExecuteFromFnRaw;
By default, canExecute tracking is started via a setTimeoue(...,0)
inside command
function.
Usually, commands are created inside a constructor, this avoids firing it while constructor is not finished.
If you need a different start mode - you can pass in canExecuteStartMode
with one of the following values:
/*** controlls when canExecute function will be ran for the first time*/ //in manual mode, you will have to explicitly start tracking:yourVm.commandInstance.forceInitCanExecuteTracking;