-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Transactions #1535
Comments
Here's an implementation that I have been using: const { assign } = Object
const clone = (...args) => assign({}, ...args)
function mixin(target, ...sources) {
for (let i=0; i < sources.length; i++) {
const source = sources[i]
const propertyNames = Object.getOwnPropertyNames(source)
for (let j=0; j < propertyNames.length; j++) {
const propertyName = propertyNames[j]
Object.defineProperty(
target,
propertyName,
Object.getOwnPropertyDescriptor(source, propertyName))
}
}
return target
}
Store.prototype.transaction = async transaction(fn) {
const store__transaction = mixin(new Store(), this)
store__transaction._state = clone(this._state)
store__transaction._computed = clone(this._computed)
const __set = {}
store__transaction.set = __set__ => {
assign(__set, __set__)
assign(store__transaction._state, __set__)
}
store__transaction._state = clone(store__transaction._state)
await Promise.all([fn(store__transaction)])
this.set(__set)
return store__transaction
} |
A better implementation may depend on #1435 |
This is addressed on components in v3 by having updates scheduled asynchronously in the next microtask. There's no longer a single central store, so I don't think we want to encourage having some way to have atomic updates there. Closing. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Calling
set
multiple times from different helper functions can cause undesired interstitial state.Transactions would allow these
set
calls to be consolidated into a singleset
.Here is one potential syntax:
The text was updated successfully, but these errors were encountered: