arqex / freezer

A tree data structure that emits events on updates, even if the modification is triggered by one of the leaves, making it easier to think in a reactive way.
MIT License
1.28k stars 56 forks source link

Option to manually handle the update #74

Closed danfma closed 7 years ago

danfma commented 8 years ago

Hey,

It would be good in some scenarios, like async calls, that we could control when the state should update manually (disabling the auto sync on next tick).

kuraga commented 8 years ago

Transactions?

arqex commented 8 years ago

Hi, disablin state updates temporarily would be tge start of a bunch of 'why is my app not updating' problems.

If you need to make a bunch of updates that may affect to your app performance use transactions (there should be a really big number of updates to see a performance penalty) as @kuraga recommended.

For async operations just update the state when the call finish:

var f = new Freezer({
  loading: false,
  whatever: '',
  other: ''
});

f.on('startAsync', () => {
 // We set up a flag to show some loading message
 // in our app
 f.get().set({loading: true});

 asyncCall( () => {
   // Here you will update the state
   // f.get().set({whatever, 'yeah'});
   f.get().set({loading: false});

   // If you need to update more than one
   // domain of your state you can trigger a new
   // event
   f.trigger('endAsync', 'yeah');
 });
});

// This handles whatever
f.on('endAssign', result => {
 f.get().set({whatever: result});
});

// This handles other
f.on('endAssign', result => {
 f.get().set({other: 'Other ' + result});
});