Open yaofly2012 opened 5 years ago
但是 MobX 把透明的函数响应式编程(Transparent Functional Reactive Programming)提升到了一个更好的水平并且提供了独立的实现。它以一种无障碍、同步、可预见和高效的方式实现了 TFRP。
在把对象转成observale对象时通过clone的方式,不影响原来的对象。
var list = [1, 2, 4];
var list2 = observable(list);
list2是observable对下,list依旧是原来的数组。
只能用于普通对象(原型是 Object.prototype或者Null的对下),否则报错:
mobx.module.js:90 Uncaught Error: [mobx] The provided value could not be converted into an observable. If you want just create an observable reference to the object use 'observable.box(value)'
如果普通对下的属性是个非普通对象,observable只会把该属性转成observable,不会把非普通对象的属性转成observable。
var p = {
name: 'john'
}
var c = Object.create(p, {
age: {
value: 23,
writable: true,
enumerable: true,
configurable: true
}
});
var observableObj = observable({
name: 'john',
child: c
})
autorun(() => {
console.log(observableObj.child.age)
})
setInterval(() => {
observableObj.child.age = Math.random()
}, 1000)
上例中name和child属性会转成observable,而child.age属性不会转成observable。
observable对象的属性如果赋值新的对象,则也会递归的遍历新对下转成observable。
function demo4() {
var a = observable({name: 'john'});
a.name = {
age: 22 // age属性也会被转成observable
}
autorun(() => {
console.log(a.name.age)
})
setInterval(() => {
a.name.age = Math.random();
}, 1000)
}
特殊的name属性,命名observable对象。
重写array内置的方法
扩展了array的方法
var name = 'john';
var name2 = observable.box(name);
console.log(`${name == name2}`); // true
observable和observable.object关系?
obsevabe函数可以使用对象,数组,Map了,为啥还单独暴露observable.object/array/map方法?
两者的区别? 先autorun,再添加元素
autorun(() => {
console.log(`${todos[2] ? todos[2].name : 'no ele in index 2'}`)
})
todos[2] = { name: 'john', title: 'a person', completed: false };
setTimeout(() => {
todos[2].name = 'lulu' // 不会触发回调
}, 1000)
先添加元素,后autorun
todos[2] = { name: 'john', title: 'a person', completed: false };
autorun(() => {
console.log(`${todos[2] ? todos[2].name : 'no ele in index 2'}`)
})
setTimeout(() => {
todos[2].name = 'lulu' // 会触发回调
}, 1000)
mobx.module.js:3247 Uncaught Error: [mobx.array] Index out of bounds, 3 is larger than 2
为什么这样做?
observableArray.sort and reverse will not change the array in-place, but only will return a sorted / reversed copy
因为什么原因导致mobx4及以下版本的obvervable数组是个伪数组?
为啥没有observable Set ?
如何给根state对象增加新的abservable属性?
在data Function里使用数组的map函数,为什么会对length属性进行的tracking ?
响应式的调用predicate函数,当predicate函数返回的Promise 被resolved或者返回true,触发effort回调函数调用,并自动取消自动运行程序。
注意:
这通常是当你需要从反应式代码桥接到命令式代码的情况 ?
什么是“反应式”,“命令式”
console.log 是异步 API,它只会稍后对参数进行格式化,因此 autorun 不会追踪 console.log 访问的数据
什么?
批处理action函数内部对state修改,即:
action(fn)()的语法糖,立即执行fn。
Becoming fully reactive: an in-depth explanation of MobX
是什么? a (simple, scalable)state management solution
要解决的问题: producing an consistent state and state that is in-sync with local variables that linger around.
How: The strategy to achieve that is simple: Make sure that everything that can be derived from the application state, will be derived. Automatically.
Issues:
什么是“原子级的自动更新” ?
不可能观察到中间值 ?
可以直接修改state的值,为啥建议统一使用action修改state? action能带来什么好处
concern:
概念
怎么算是依赖:
表示计算值report依赖
this.todos
的第一个元素的task属性和length
属性,并不是表示report依赖this.todos
。原则
Issues?
autorun函数
Demoing过程遇到的问题