[MST] Use Volatile State and Lifecycle Methods to Manage Private State
MST has a pretty unique feature: It allows you to capture private state on models, and manage this state by using lifecycle hooks. For example by setting up a WebSocket connection and disposing of the connection automatically as soon as the instance gets removed from the store. In this lesson, we will leverage cancellable fetches to abort in-flight requests when appropriate
In this lesson you will learn:
- Aborting window.fetch requests :-).
- Storing private, volatile, internal state in the function closure
- A second life-cycle hook: beforeDestroy
The whole point for this post is showing how to abort fetch request:
export const Group = types
.model({
users: types.map(User)
})
.actions(self => {
let controller return {
afterCreate() {
self.load()
},
load: flow(function* load() {
controller = window.AbortController && new window.AbortController()
try {
const response = yield window.fetch(`http://localhost:3001/users`, {
signal: controller && controller.signal
})
applySnapshot(self.users, yield response.json())
console.log("success")
} catch (e) {
console.log("aborted", e.name)
}
}),
reload() {
if (controller) controller.abort()
self.load()
},
beforeDestroy() {
if (controller) controller.abort()
}
}
})
https://developer.mozilla.org/en-US/docs/Web/API/AbortController

[MST] Use Volatile State and Lifecycle Methods to Manage Private State的更多相关文章
- LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: Root WebApplicationContext: startup date [Sun Jan 13 17:59:19 CST 2019]; root of context hierarch
在运行项目时出现了:LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via ...
- LifecycleProcessor not initialized - call 'refresh' before invoking lifecycle methods via the context: --异常记录
升级了JDK之后,启动应用,直接抛出此异常.网上搜罗半天,没有正确的解决方案. 然后想到了是“升级了JDK”,重新检查所有JDK相关的配置的地方,在Debug Configurations里找到启动时 ...
- vue & lifecycle methods & this bug & ES6 Arrow function & this bind bug
vue & lifecycle methods & this bug ES6 Arrow function & this bind bug bad fetchTableData ...
- React LifeCycle Methods & re-learning 2019
React LifeCycle Methods & re-learning 2019 v16.9.0 https://reactjs.org/docs/react-component.html ...
- 执行bin/hdfs haadmin -transitionToActive nn1时出现,Automatic failover is enabled for NameNode at bigdata-pro02.kfk.com/192.168.80.152:8020 Refusing to manually manage HA state的解决办法(图文详解)
不多说,直接上干货! 首先, 那么,你也许,第一感觉,是想到的是 全网最详细的Hadoop HA集群启动后,两个namenode都是standby的解决办法(图文详解) 这里,nn1,不多赘述了.很简 ...
- Start state is missing. Add at least one state to the flow
springmvc配置过程中,会配置数据库文件,比如说如下文件:这个时候可能会出现“Start state is missing. Add at least one state to the flow ...
- PMD - Avoid autogenerated methods to access private fields and methods of inner / outer classes
PMD错误 Avoid autogenerated methods to access private fields and methods of inner / outer classes 样例 p ...
- springmvc.xml 中报错:Start state is missing. Add at least one state to the flow
最近一个学弟问我关于整合springMVC和spring出现的配置文件springmvc.xml出现的Start state is missing. Add at least one state to ...
- [MST] Create an Entry Form to Add Models to the State Tree
It is time to add new entries to the wishlist. We will achieve this by reusing forms and models we'v ...
随机推荐
- vue 过滤器使用的传参说明
在table中,需要对obj的数据类型进行文字转换,例如后台接口返回的姓别值:1,2.其中需要页面根据字典需要把1=>男,2=>女进行转换. 以前的习惯是每一个过滤方法都写一个方法进行转换 ...
- 小学生都能学会的python(深浅拷贝)
小学生都能学会的python(深浅拷贝) join() 把列表中的每一项用字符串拼接起来 # lst = ["汪峰", "吴君如", "李嘉欣&quo ...
- VUE:列表的过滤与排序
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- Windows里正确安装Zookeeper以服务运行
不多说,直接上干货! 为什么要在Win下来安装Zookeeper呢? 其实玩过大数据的人很清楚,在Linux下我更不说了.在win下,如Disconf .Dubbo等应用. 所以,它的应用是非常广的. ...
- ORACLE-014:oracle中查看DBLinkpassword
dblink在数据库中使用是比較多的,简单方便.只是随着oracle安全机制的加强.普通用户登录仅仅能看到,username以及连接字符串.那么dblinkpassword怎样查看呢? 非常easy. ...
- POSIX 线程编程(一)简介
简介 在共享内存的多处理器结构中,可以用线程来实现并行.对于UNIX系统, IEEE POSIX 1003.1c标准规定了C语言线程编程接口的标准.这份标准的实现就是POSIX threads, 或者 ...
- Cocos2d-x 3.4 之 消灭星星 > 第三篇(终) <
***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...
- Java线程演示样例 - 继承Thread类和实现Runnable接口
进程(Process)和线程(Thread)是程序执行的两个基本单元. Java并发编程很多其它的是和线程相关. 进程 进程是一个独立的执行单元,可将其视为一个程序或应用.然而,一个程序内部同事还包括 ...
- mysql的查询练习1
1.多表查询
- 换今日特价图片---轻开电子商务系统(企业入门级B2C站点)
跟换主页轮播图片一样,一共4个文件: 列表显示文件:site/links/img2.html 加入图片文件:site/links/img2_add.html 加入保存图片文件:site/links/i ...