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-when

v1.0.0

Published

@when decorator for MobX state changes and reactively executing functions.

Downloads

9

Readme

mobx-when

@when() decorator for changing MobX, especially useful React.Component.

This decorator takes a function to listen changes through computed of MobX. Hence it needs a class having two functions for initializing and deinitializing disposer of the computed. Since this library may be used with React, those are componentDidMount and componentWillUnmount by default. Hence, for React applications, you don't need to provide any name for the decorator.

This function has global interface declaration called WhenDecorator, which can be used for extending the @when decorator. Usage examples are shown below.

The function decorated will be executed whenever the computed value changes. This enables developer both connecting a function to a changing/computed variable and calling the function manually.

This library utilizes WeakMap to prevent/reduce instance modifications as much as possible.

Example

class UserState {
  @observable id?: string
  @observable loggedIn = false
  @observable comments?: IComment[]
}

const userState = new UserState()

class CommentsView extends React.PureComponent {
  @when(() => userState.loggedIn)
  loadUserComments() {
    // The function here is also bound, hence, `this` refers to CommentsView
    // We assume that when user is logged, it gets an ID.
    return loadCommentsOf(userState.id)
  }

  someOtherFunction = () => {
    // You can also invoke the function above manually
    this.loadUserComments().then(comments => {
      userState.comments = comments
    })

    // ...
    console.log('User comments are loading')
    // ...
  }
}

// Extending example
// Since an interface is given, you can extend the interface to add
// more functionalities to the decorator.

declare global {
  interface WhenDecorator {
    userLoggedIn(...parameters: any[]): MethodDecorator
  }
}

when.userLoggedIn = (...parameters: any[]) => when(() => state.loggedIn, ...parameters)

// Now, we can do

class SomeView extends React.PureComponent {
  @when.userLoggedIn()
  private reloadUserInformation() {
    // ... Reloading user information, since this function will
    // automatically be called when user is logged in, we know that
    // user is logged in and can use id to load anything related.

    // ...
  }
}