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

always run in transact() mode #105

Open
aktxyz opened this issue Jun 20, 2017 · 4 comments
Open

always run in transact() mode #105

aktxyz opened this issue Jun 20, 2017 · 4 comments

Comments

@aktxyz
Copy link

aktxyz commented Jun 20, 2017

I find that I use transact all over the place, and I "thought" I was doing it correctly.

store.something.transact();
store.something.set('a',1);
store.something.set('b',2);

That has been working great ... but not I updated freezerjs to the latest and am getting detach warnings all over.

Looks like I really should be doing ...

date = store.something.transact();
data.set('a',1);
data.set('b',2);

So I actually have 2 questions ...

  1. the way I have been doing seems to work fine, what are the issues with doing it the first way above.

  2. is there a way to put the store into transact() mode globally, to make things a bit cleaner for my scenario.

@arqex
Copy link
Owner

arqex commented Jun 20, 2017

Hey aktxyz

If you want to update 2 elements in something you can do

store.something.set({a:1,b:2});

Also the update methods are chainable so this will do the same, but it's less efficient since it updates the tree completely twice:

store.something
  .set(a, 1)
  .set(b, 2)
;

The first set(a,1) return the updated node to let you edit it again.

I think that transaction is a last resort mechanism, and I have found myself using it only twice in the years I've been using Freezer. You need to update the whole tree hundreds of times in the same tick to feel that there is a performance object, so if I am not doing hundreds of updates I never use it.

@aktxyz
Copy link
Author

aktxyz commented Jun 20, 2017

I use the 2 methods you describe quite a bit also, but there are cases where the code for the store updates are not all together, so end up having to .transact() to work.

If I do a .transact() at the root, will that apply to all children?

@arqex
Copy link
Owner

arqex commented Jun 22, 2017

I usually organize the functions that update the state by the part of the state they update. Eg, for I would create a file, somethingReactions.js to edit what's inside of something.

In that file I use to create a helper function to make simpler run the updates something like:

// 'd' after 'domain'
var d = function (){ return freezer.get().something };

That way I rest assured that I am updating the current content of the store:

d().set('a',1);
d().set('b',2);

I think transact is not a good thing to use so lightly, because it works stopping the tree updates on that node and if I don't call run immediately after using it, I feel like I am loosing the control of what's happening. I might be doing some other updates to the store before the transaction run, without knowing that there is a transaction going on, maybe in the same store branch I am working.

That's the reason that I never use it, unless I have to make lots of updates in one node's children, and I always call run afterwards.

By the way, I am writing an article to celebrate the 1000 stars, where I try to explain how I use freezer :)

If you want to preview and share your thoughts I'll be so grateful:
https://medium.com/@arqex/7-tips-using-react-with-freezer-f4d75e363b94

@aktxyz
Copy link
Author

aktxyz commented Jun 22, 2017

great idea on the article, I will read it and let you know

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