码迷,mamicode.com
首页 > 其他好文 > 详细

[MST] Defining Asynchronous Processes Using Flow

时间:2018-01-29 00:29:15      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:await   oca   sync   mode   ges   flow   optional   cal   can   

In real life scenarios, many operations on our data are asynchronous. For example, because additional recourses need to get fetched. MST has first class support for asynchronous actions. We will start with a naively implemented async process, and work through async / await towards MST flows and generators.

In this lesson, you will learn

  • That async process is painful if they need to respect the ‘only actions can modify‘ semantics of MST
  • Flows are the idiomatic way to describe async processes in MST
  • Flows are robust; they make it possible to get full control over the lifecycle of a process

 

The whole point is to using ‘flow( function* generatorFn() => {})‘ to fix some limitation.

 

import { types, flow } from "mobx-state-tree"

import { WishList } from "./WishList"

const User = types
    .model({
        id: types.string,
        name: types.string,
        gender: types.enumeration("gender", ["m", "f"]),
        wishList: types.optional(WishList, {})
    })
    .actions(self => ({
        getSuggestions: flow(function* getSuggestions() {
            const response = yield window.fetch(`http://localhost:3001/suggestions_${self.gender}`)
            self.wishList.items.push(...(yield response.json()))
        })
    }))

export const Group = types.model({
    users: types.map(User) // similar to object entities
})

 

[MST] Defining Asynchronous Processes Using Flow

标签:await   oca   sync   mode   ges   flow   optional   cal   can   

原文地址:https://www.cnblogs.com/Answer1215/p/8372851.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!