npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

mobx-command

v0.14.0

Published

Command pattern implementation for MobX

Downloads

7

Readme

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:

import { command } from 'mobx-command';

class UserDetailsVm {

    public userId: number = 5;

    public resetPassword = command({
        execute: async (skipConfirmation: boolean = false) => {
            let confirmation = skipConfirmation || await confirmSensitiveAction("Are you sure?");
            if (!confirmation) {
                return 0;
            }
            let operationId = await userServiceClient.resetUserPasswrod(this.userId);
            return operationId;
        },
        canExecute: () => userServiceClient.isAdminCurrentUser(),
    });
    // canExecute is lazy
    // if you wan't to trigger it immediately, pass evaluateCanExecuteImmediately: true 
}

let userDetailsVm = new UserDetailsVm();

//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 execute
userDetailsVm.resetPassword.canExecuteCombined; 

//execute command if canExecuteCombined is true. 
let operationId1: number | undefined = userDetailsVm.resetPassword.executeIfCan(true);

//execute command. Does not perform checks, forces execution.
let operationId2: number = userDetailsVm.resetPassword.executeForced(true);

//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 command
userDetailsVm.resetPassword.isExecuting;

//Result of canExecute function; 
//If canExecute returns Promise<boolean> - will be false while check is running
userDetailsVm.resetPassword.canExecuteFromFn;

//true while canExecute is running, if it returns a promise to track this
userDetailsVm.resetPassword.isCanExecuteAsyncRunning;

//If canExecute returned a promise that was ultimately rejected - 
//this will contain rejection reason
userDetailsVm.resetPassword.canExecuteAsyncRejectReason;

//Raw computed from canExecute function, so you can subscribe to its recalculations, if needed
userDetailsVm.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
*/
export enum CanExecuteStartMode {
    /**
    *  run canExecute synchronously inside command
    */
    InsideCommandFunction = 1,
    /**
    *  DEFAULT, run canExecute via setTimeout(...,0) inside command
    */
    AsyncInsideCommandFunction = 2,
    /**
    *  canExecute will be run when one of relevant properties of command is accessed
    *  Observers do not like this mode, since it is likely to alters state 
    */
    OnFirstCheck = 3,
    /**
    *  canExecute will be run via setTimeout(...,0) when one of relevant properties of command is accessed
    *  Observers do not like this mode, since it is likely to alters state 
    */
   AsyncOnFirstCheck = 4,
    /**
    *  command does not start tracking canExecute (assuming false), 
    *  untill forceInitCanExecuteTracking is called
    */
    Manual = 5
}

//in manual mode, you will have to explicitly start tracking:
yourVm.commandInstance.forceInitCanExecuteTracking();