使用

   // indexStore.js
import axios from "axios";
import { from } from "rxjs";
import { map } from "rxjs/operators"; @util.Async(from(axios("http://localhost:5000/test")).pipe(map(v => v.data.users)))
@util.isEmpty(true)
// @observable // 不需要observer
list;

实现


import { set } from "mobx";
import * as u from "lodash"; /**
* * async: false => data => isData
* * async: true => data => isData$
*/
export const isEmpty = (async = false) => {
return function(target, key) {
let opt = async ? `is${u.upperFirst(key)}$` : `is${u.upperFirst(key)}`;
Object.defineProperty(target, opt, {
get: function proxyGetter() {
const k = async ? `${key}$` : key;
return !u.isEmpty(this[k]);
},
enumerable: true,
configurable: true,
});
};
}; // list=> list$
export const Async = (fn$, initData = null) => {
return function(target, key) {
set(target, {
[`${key}$`]: initData,
});
fn$.subscribe(
v => {
set(target, {
[`${key}$`]: v,
});
},
err => console.error(err),
() => {
/* done */
},
);
};
};

组件

const INDEXSTORE = "indexStore";

@inject(INDEXSTORE)
@observer
class Test extends Component {
render() {
const { indexStore } = this.props;
return (
<>
{indexStore.isList$ && (
<>
<h2>list</h2>
<ul>
{indexStore.list$.map(el => (
<li key={el}>{el}</li>
))}
</ul>
</>
)}
</>
);
}
}

react+mobx 编写 Async装饰器的更多相关文章

  1. react+mobx 编写 withStoreHistory 装饰器

    主要作用是向store里面注入一个history对象,方便story里面的函数调用 function withStoreHistory(storeName) { if (!storeName) ret ...

  2. python练习笔记——编写一个装饰器,模拟登录的简单验证

    编写一个装饰器,模拟登录的简单验证(至验证用户名和密码是否正确) 如果用户名为 root 密码为 123则正确,否则不正确.如果验证不通过则不执行被修饰函数 #编写一个装饰器,模拟登录的简单验证 #只 ...

  3. mobx 添加 isEmpty 装饰器

    避免 obj.xxx && obj.xxx.length 这样的写法 store import * as u from "lodash"; function isE ...

  4. Python练习笔记——编写一个装饰器,测算出一个函数的运行时间

    import time def time_value(dec): def wrapper(*args,**kwargs): start_time = time.time() get_str = dec ...

  5. Python进阶之装饰器

    函数也是对象 要理解Python装饰器,首先要明白在Python中,函数也是一种对象,因此可以把定义函数时的函数名看作是函数对象的一个引用.既然是引用,因此可以将函数赋值给一个变量,也可以把函数作为一 ...

  6. Day4 装饰器——迭代器——生成器

    一 装饰器 1.1 函数对象 一 函数是第一类对象,即函数可以当作数据传递 #1 可以被引用 #2 可以当作参数传递 #3 返回值可以是函数 #3 可以当作容器类型的元素 二 利用该特性,优雅的取代多 ...

  7. Day4 闭包、装饰器decorator、迭代器与生成器、面向过程编程、三元表达式、列表解析与生成器表达式、序列化与反序列化

    一.装饰器 一.装饰器的知识储备 1.可变长参数  :*args和**kwargs def index(name,age): print(name,age) def wrapper(*args,**k ...

  8. 浅显易懂的谈一谈python中的装饰器!!

    hello大家好~~我是稀里糊涂林老冷,一天天稀里糊涂的. 前一段时间学习了装饰器,觉着这东西好高大上哇靠!!哈哈,一定要总结一下,方便以后自己查阅,也希望帮助其他伙伴们共同进步! 装饰器: 大家可以 ...

  9. Python 装饰器(Decorator)

    装饰器的语法为 @dec_name ,置于函数定义之前.如: import atexit @atexit.register def goodbye(): print('Goodbye!') print ...

随机推荐

  1. 最新版Xamarin Mono For Android、Monotouch 安装、破解(实时同步更新)

    以上链接如不是最新,以官方为主,官方更新地址如下:http://xamarin.com/installer_assets/v3/Mac/Universal/InstallationManifest.x ...

  2. android应用程序中获取view的位置

    我们重点在获取view的y坐标,你懂的... 依次介绍以下四个方法: 1.getLocationInWindow int[] position = new int[2]; textview.getLo ...

  3. springboot拦截器@Autowired为null解决

    问题原因 拦截器加载的时间点在springcontext之前,所以在拦截器中注入自然为null 文件解决 在spring配置文件中这样写 @Bean public HandlerInterceptor ...

  4. Coursera机器学习+deeplearning.ai+斯坦福CS231n

    日志 20170410 Coursera机器学习 2017.11.28 update deeplearning 台大的机器学习课程:台湾大学林轩田和李宏毅机器学习课程 Coursera机器学习 Wee ...

  5. 每天一个linux命令:vmstat

    1.命令简介 vmstat(Virtual Memory Statistics 虚拟内存统计) 命令用来显示Linux系统虚拟内存状态,也可以报告关于进程.内存.I/O等系统整体运行状态. 2.用法 ...

  6. C++ 无锁数据结构

    https://www.zhihu.com/question/52629893/answer/131731126

  7. Jquery DataTables 获取表格数据及行数据

    注意table变量是 1.jQuery DataTables 行号获取 $("#example tbody tr").on("click", function( ...

  8. Apache Spark as a Compiler: Joining a Billion Rows per Second on a Laptop(中英双语)

    文章标题 Apache Spark as a Compiler: Joining a Billion Rows per Second on a Laptop Deep dive into the ne ...

  9. LNAMP服务器环境(源码安装)

    在安装前先看下它们安装时所需要的依赖库:http://www.cnblogs.com/fps2tao/p/7699448.html 1.nginx源码安装 下载:http://nginx.org/en ...

  10. 微表面分布函数(Microfacet Distribution Function)确切含义

    <Physically-Based Shading Models in Film and Game Production>中说:“D()的值不局限于0到1,可以任意大”,这句话使我比较好奇 ...