zoukankan      html  css  js  c++  java
  • [MST] Defining Asynchronous Processes Using Flow

    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
    })
  • 相关阅读:
    关于浮动清除的一些小感悟,4种方法清除浮动
    6号css学习小记
    pexpect-pxssh-登陆Linux-执行命令
    chroot命令
    Loadrunner11点击录制脚本无响应,IE页面弹不出——解决方案汇总
    JAVA实验五(网络编程)
    Java实验三
    JAVA实验二(面向对象)
    JAVA实验一
    Tfs链接错误解决方案
  • 原文地址:https://www.cnblogs.com/Answer1215/p/8372851.html
Copyright © 2011-2022 走看看