[MST] Defining Asynchronous Processes Using Flow

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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
})

 

以上是关于[MST] Defining Asynchronous Processes Using Flow的主要内容,如果未能解决你的问题,请参考以下文章

JAXB - XML Schema Types, Defining Subtypes

7.2 Models -- Defining Models

Windows PowerShell: Defining Parameters

4.2 Routing -- Defining Your Routes

第三节MapStruct翻译--Defining a mapper

Codeforces 7E - Defining Macros 题解