cellx
cellx copied to clipboard
Лишний рендер для isPending
В примере ниже лишний: "c.subscribe: isPending: true, err: null, val: 5", т.к. данные сфетчились и записались, а потом еще отдельно isPending в false переключили
let a = new cellx.Cell((push, fail, old) => {
setTimeout(() => {
push(2)
}, 100)
return old || 1
})
let b = new cellx.Cell((push, fail, old) => {
setTimeout(() => {
push(3)
}, 500)
return old || 2
})
cellx.configure({asynchronous: false})
const c = new cellx.Cell(() => a.get() + b.get())
const d = new cellx.Cell(() => {
return {
v: c.get(),
isPending: c.isPending(),
error: c.getError()
}
})
d.subscribe((err, {value}) => {
console.log('c.subscribe: isPending: '+value.isPending + ', err: ' + (err ? err.message : 'null') + ', val: ' + value.v)
})
const v = d.get()
console.log('c.get:', v)
/*
"c.get:"
[object Object] {
error: null,
isPending: true,
v: 3
}
"c.subscribe: isPending: true, err: null, val: 4"
"c.subscribe: isPending: true, err: null, val: 5"
"c.subscribe: isPending: false, err: null, val: 5"
*/
http://jsbin.com/padehafano/edit?js,console
В синхронном режиме изменения не схлопываются, d
зависит от c
и c._pendingStatusCell
, после второго push
оба меняются и дважды происходит перерасчёт и обновление d
.
А там транзакцию внутри нельзя влепить? Или подскажи где ее тут влепить. По-логике все-таки это лишний рендер.
подскажи где ее тут влепить
просто обернуть второй push
в cellx.transact
.
let b = new cellx.Cell((push, fail, old) => {
setTimeout(() => {
cellx.transact(() => {
push(3)
})
}, 500)
return old || 2
})
Да, так работает, но как-то это не очевидно, видимо из-за неявной установки isPending перед pull.