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

v1.0.1

Published

Loadable decorator for mobx

Downloads

4

Readme

npm package license build coverage issues

Loadable decorator for mobx

mobx-loadable is a small library written in Typescript allowing to set some properties of an object loadable. The loadable property will then have a hidden state indicating that it is currently being loaded. This can be useful when used with a store which is sometimes refreshing its data from a server. Previous data value still can be accessed while loading, and then can be displayed, with an hourglass to indicate that it is being updated.

A loadable property is an observable property. Each change either to the value or to the loading state of the property can be observed.

Installation

Installation is done using npm install command:

$ npm install --save mobx-loadable

If you prefer using yarn:

$ yarn add mobx-loadable

Language/langue

Because French is my native language, finding all documents and messages in French is not an option. Other translations are welcome.

Anyway, because English is the language of programming, the code, including variable names and comments, are in English.

:fr: Une version française de ce document se trouve ici.

Usage

@loadable

The @loadable property decorator can be used either directly, i.e. @loadable or as a decorator factory, i.e. @loadable(). All the loadable properties are also automatically observable.

  @loadable()
  public readonly persons: Person[] = []
  @loadable
  private _boss: Person
  @loadable
  private _moto?: string

@load

The @load method decorator factory is used to update the loading state of the properties. Its first parameter is always the name of the property to update. The loading state update is automatically done in an @action, but you may need to manually add an @action anyway if you also modify the value of the property in the method. The loading state is updated when entering the method. The decorator factory can be used in 3 different ways:

  @load('persons', true)
  loadPersons() {
    axios.get('/persons').then(this.updatePersons)
  }

  @action
  @load('persons', false)
  updatePersons(response: any) {
      // this.persons = …
  }

When the second parameter of @load is a boolean, it will be the value set to the loading state.

  loadPersons() {
    this.setLoadingPersons(true)
    axios.get('/persons').then(() => {
      // this.updatePersons(response)
      this.setLoadingPersons(false)
    })
  }

  @load('persons')
  setLoadingPersons(loading: boolean) {
      // this.persons = …
  }

When only one parameter is given to @load, the first parameter given to the decorated method will be used as the loading state value.

  loadPersons() {
    this.updatePersons([])
    axios.get('/persons').then(() => {
      // const persons = convert(response)
      this.updatePersons(persons)
    })
  }

  @action
  @load('persons', (persons: any[]) => persons.length === 0)
  updatePersons(persons: Persons[]) {
      this.persons = persons
  }

When the second parameter given to @load is a function, this function will be called with the decorated method arguments and must return a boolean which will be used as the loading state value.

Loading and non-objects

In order for the loading state to be added, the property must be an object. If it is not the case, it will be transformed into an object when the loading state is set to true. In other words, while the property is loading:

| if the property is | it will become | |--------------------|----------------| | undefined | {} | | null | {} | | boolean | Boolean | | number | Number | | string | String | | symbol | Symbol |

So you have to be careful when using the property (e.g. as a non empty object, a Boolean with value false will be truthy inside a if). If the real value is not needed while loading, you may indicate that the property can return a value of type Loading:

  @computed
  get user() undefined | User | Loading {
    if (this._userId === undefined || isLoading(this._userId)) {
      return this._userId
    } else {
      return this._users.find(user => user.id === this._userId)
    }
  }

isLoading()

The isLoading method takes an object as argument and indicates if it is currently being loaded.

In Typescript, the isLoading method is a type guard. It means that with Typescript, it can be used to filter the Loading type of the property.

const user: User | Loading = getValues()
if (isLoading(user)) {
  // user is of type Loading
} else {
  // user is of type User
}