I'm trying to learn how Reflux actions and stores work so I wrote a small test program. When I run the application, the code that calls
toggleGem();
(which causes the Reflux Action to fire) does not immediately run. These action events are queued as can be shown by the output of the program below.
The output is:
myObj=lastUpdate=undefined
myObj=lastUpdate=undefined
myObj=lastUpdate=undefined
ENTER: gemStore.handleToggleGem: isGemActivated:false
MyObj.onGemChange: newVal=true
EXIT: gemStore.handleToggleGem: isGemActivated:true
ENTER: gemStore.handleToggleGem: isGemActivated:true
MyObj.onGemChange: newVal=false
EXIT: gemStore.handleToggleGem: isGemActivated:false
Notice that the console output
ENTER: gemStore.handleToggleGem: isGemActivated:false
doesn't appear until the program is exiting. This (to me) means that the action events are bundled and fired/run later by some "housekeeping" that Reflux is doing.
Is there a function I can call to tell Reflux to fire the action events that have been queued?
To recreate this example, simply save the code to RefluxTest.js and run these commands:
mkdir RefluxTest
npm init
npm install --save reflux
npm install --save react
node RefluxTest.js
Thanks.
NOTE: I know that this is not representative of how Reflux would normally be used because it is outside of the browser but I was curious if anyone had an answer.
Based on code from http://spoike.ghost.io/deconstructing-reactjss-flux/
// Code for file RefluxTest.js
// KWS: Learning how Reflux works outside of the browser
var React = require("react/addons");
var TestUtils = React.addons.TestUtils;
var Reflux = require("reflux");
// Creating action
var toggleGem = Reflux.createAction();
// Creates a DataStore
var gemStore = Reflux.createStore({
// Initial setup
init: function() {
this.isGemActivated = false;
// Register statusUpdate action
this.listenTo(toggleGem, this.handleToggleGem);
},
// Callback
handleToggleGem: function() {
console.log('ENTER: gemStore.handleToggleGem: isGemActivated:' + this.isGemActivated);
this.isGemActivated = !this.isGemActivated;
// Pass on to listeners through
// the DataStore.trigger function
this.trigger(this.isGemActivated);
console.log('EXIT: gemStore.handleToggleGem: isGemActivated:' + this.isGemActivated);
}
});
function MyObj() {
gemStore.listen(this.onGemChange);
this.lastUpdate = undefined;
}
MyObj.prototype.toString = function() {
return "lastUpdate=" + this.lastUpdate;
}
MyObj.prototype.onGemChange = function(newVal){
console.log("MyObj.onGemChange: newVal=" + newVal);
this.lastUpdate = newVal;
}
var myObj = new MyObj();
console.log("myObj=" + myObj);
toggleGem();
console.log("myObj=" + myObj);
toggleGem();
console.log("myObj=" + myObj);