Skip to content
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

Improve transaction support for data changes #2

Closed
zerkalica opened this issue Feb 13, 2015 · 5 comments
Closed

Improve transaction support for data changes #2

zerkalica opened this issue Feb 13, 2015 · 5 comments

Comments

@zerkalica
Copy link

Is analog Immutable.withMutations for atomic data changes?

In this example only first array change will trigger update listener:

var state = new Freezer({arr: [0,1,2,3,4], a: {b: {bb: 1, cc: 2}, c: {cc: 2}}})
var data = state.get();

data.arr.getListener().on('update', (data) => {
    console.log(data)
})

data.arr
    .push( 5 ) // [0,1,2,3,4,5]
    .pop() // [0,1,2,3,4]
    .unshift( 'a' ) // ['a',0,1,2,3,4]
    .shift() // [0,1,2,3,4]
    .splice( 1, 1, 'a', 'b') // [ 0, 'a', 'b', 2, 3, 4]

// outputs { '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5 }
@arqex
Copy link
Owner

arqex commented Feb 13, 2015

hi @zerkalica

Thanks for ping me with the problem. It should trigger update just once, but the result should be as you said:

[ 0, 'a', 'b', 2, 3, 4]

I will have a look at it tomorrow.

It doesn't work like immutable's withMutations, every operation will update the tree completely, creating a new frozen object, but all the operations in the same tick should trigger update just once with the final result of all of them. That way the components are only refreshed once.

Anyways, it should be great to have a way of applying multiple changes at once, like a database transaction isn't it? Something like;

data.arr.transaction()
    .push( 5 ) // [0,1,2,3,4,5]
    .pop() // [0,1,2,3,4]
    .unshift( 'a' ) // ['a',0,1,2,3,4]
    .shift() // [0,1,2,3,4]
    .splice( 1, 1, 'a', 'b') // [ 0, 'a', 'b', 2, 3, 4]
    .run()
;

What do you think?

@zerkalica
Copy link
Author

Looks good, but will it work with hashes, not only array?

var store = new Freezer({
root: {
    a: {aa: {c: 1}},
    b: {bb: {cc: 321}},
    arr: [1, 2, 3, 4]
}
});

store.get().root.transaction()
.a.set({aa: {c: 123}}
.b.set({bb: {cc: 321}}
.arr.push(5)
.run()

@arqex
Copy link
Owner

arqex commented Feb 13, 2015

We can work on it. Any ideas on how to?
El 13/02/2015 21:34, "Stefan" notifications@github.com escribió:

Looks good, but will it work with hashes, not only array?

var store = new Freezer({
root: {
a: {aa: {c: 1}},
b: {bb: {cc: 321}},
arr: [1, 2, 3, 4]
}
});

store.get().root.transaction()
.a.set({aa: {c: 123}}
.b.set({bb: {cc: 321}}
.arr.push(5)
.run()


Reply to this email directly or view it on GitHub
#2 (comment).

@arqex
Copy link
Owner

arqex commented Feb 14, 2015

@zerkalica

I have released a new version fixing the parameter of the 'update' event. Transaction support will come in a future release.

Cheers,
Javi

@arqex
Copy link
Owner

arqex commented Mar 31, 2015

@zerkalica

Transactions are now working in freezer 0.5

https://github.com/arqex/freezer#batch-updates

I hope you like them.

@arqex arqex closed this as completed Mar 31, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants