[RFC] Add batch API
See original GitHub issue💬 RFC
🔦 Context
In v2, our binding system becomes synchronous. While there are many benefits, there’ also cons:
- v1 user surprises: in v1, change notification is handled in next tick, this means user code can have multiple mutation without having to deal with intermediate states.
Consider following example:
class Contact {
get isComplete() {
return this.address && this.name && this.phone;
}
}
If we have a the mutation like this:
function updateContact(contact) {
contact.address = 'santa'
contact.name = 'santa'
contact.phone = 'void';
}
In this example, the changes for the properties address
, name
and phone
are meant to be delivered together.
In v1, change handling is queued, so the isComplete
observer will only recompute only once, giving the expected behavior.
In v2, change handling is immediate, each mutation is handled by isComplete
immediately & synchronously. This, while gives greater amount of control related to timing, sometimes makes it more tedious to handle in cases where there’ multiple consecutive changes that should go together like the above.
A solution for v2, an is common in synchronous binding systems (probably?), is to have a way to batch all the changes notification, and is normally implemented similar to the following form:
function updateContact(contact) {
contact.address = 'santa'
contact.name = 'santa'
contact.phone = 'void'
}
batch(() => {
updateContact(contact)
})
// or
function updateContact(contact) {
batch(() => {
contact.address = 'santa' // not handled
contact.name = 'santa' // not handled
contact.phone = 'void' // not handled
})
// now handled
}
An example implementation of this is in https://github.com/aurelia/aurelia/pull/1018
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:5 (4 by maintainers)
How is the original idea with the flexible
$batch.start()
$batch.end()
, and a simplified@batch
decorator in #411?The start/end API can be replaced by this one, but the
@batch
decorator could be a nice helper.@m-gallesio it should be simple to patch some methods on the observers, and we should be able to have roughly the same timing with v1. It’ll be in compat package with the rest we currently have there.
We got array mutation batching, only need to add back property batching. Should be before beta