Last active
August 29, 2015 14:04
-
-
Save yurynix/3c7c4aeaa6fcbb7b9fac to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var AsyncStateManager = require('../managers/asyncStateManager'); | |
var AsyncComponentMixin = { | |
componentWillMount: function AsyncComponentMixin_componentWillMount() { | |
var self = this; | |
var asyncStateGetter = this.constructor.getInitialAsyncState; | |
var asyncStateKey = asyncStateGetter.name; | |
// ====> AsyncStateManager should not be module / global, it should be tied to request | |
if ( AsyncStateManager.hasData(asyncStateKey) ) { | |
self.setState(AsyncStateManager.getData(asyncStateKey)); | |
} else { | |
AsyncStateManager.queueAsyncData(asyncStateKey, function() { | |
return self.constructor.getInitialAsyncState(self.props.params, self.props.query); | |
}); | |
} | |
} | |
}; | |
module.exports = AsyncComponentMixin; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = App(); | |
function getMarkup() { | |
var deferred = { | |
resolve: null, | |
reject: null | |
}, | |
promise = new Promise(function(resolve, reject) { | |
deferred.resolve = resolve; | |
deferred.reject = reject; | |
}); | |
singleRun(deferred); | |
return promise; | |
} | |
function singleRun(deferred) { | |
var markup = React.renderComponentToString(app); | |
if (AsyncStateManager.hasRunningAsyncs()) { | |
AsyncStateManager.onQueueDrain(function() { | |
singleRun(deferred); | |
}); | |
} else { | |
deferred.resolve(markup); | |
} | |
} | |
getMarkup().then(function(markup) { | |
console.log(markup); | |
}, function(err) { | |
console.log("Errror", err); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment