Skip to content

Releases: danieldunderfelt/mobx-app

Stable-enough first MAJOR VERSION with support for async stores!

27 Feb 19:29
Compare
Choose a tag to compare

First major!

Hooray, the first major version of MobX-app is here! For this release, I added support for async stores. createStore will now handle promises returned from store factories, so if you need your store to wait for something, you can!

My use case is that I need to use the async import() to conditionally require modules in some of my stores. This makes the whole store async and I thought "gee, it would sure be awfully nice if createStore could handle this". Then I remembered that I am actually the author of the library where createStore comes from and so it was implemented.

How to use async stores

If you don't use async stores, rest assured that I also added some tests for the createStore method so you may now use it with renewed vigor and assurance that it actually works. Nothing else changes for you as createStore continues to work exactly the same way as before.

If you want to use this new ADVANCED functionality, check this out:

// Make the async store
const asyncStore = async state => {
  ...blah...blah...

  const actionThatNeedsToBeAsync = async value => {
    const asyncThing = await import('asyncThing')
    return await asyncThing(value)
  }

  return {
    asyncAction: actionThatNeedsToBeAsync
  }
}

/*
 * Let createStore do it's thing. If a store factory returned a
 * promise, createStore will return a promise as well that
 * resolves to the familiar { actions, state } combo.
 */

const { actions, state } = await createStore({ asyncStore })

// And off you go, the same way as before!

In essence, if you gave it a store factory that returns a promise, everything will be a promise. Async/await makes working with promises soooo smooth and I wanted to support this use case. And just to hammer it in: if you don't have a store factory that returns a promise, createStore will not return a promise either. If even ONE store returns a promise, createStore will start returning promises also.

Welcome to JavaScript where everything is made up and the types don't matter!

Enjoy.

Changes

  • Added conditional promise handling to createStore
  • Added dependency @expo/mux to handle promises
  • Added tests for createStore

While everything should continue to work as before, the reason for the major version is that this library now needs promises to be available in the environment. We follow semver here.