Last active
August 29, 2015 14:12
-
-
Save arathunku/5d7026de2b7f7856fbda to your computer and use it in GitHub Desktop.
Request Action, wrapper for all requests
This file contains hidden or 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 NewsfeedStore = Fluxxor.createStore({ | |
initialize () { | |
this.loading = false; | |
this.error = null; | |
PaginatedStoreWrapper(this); | |
_.extend(this, (localStorage.get('NewsfeedStore') || {})); | |
this.bindActions( | |
AppBarConstants.REFRESH, this.onRefresh, | |
NewsfeedConstants.NEWSFEED_LOAD, this.onNewsfeedLoad, | |
NewsfeedConstants.NEWSFEED_SUCCESS, this.onNewsfeedSuccess, | |
NewsfeedConstants.NEWSFEED_ERROR, this.onNewsfeedError | |
); | |
}, | |
onNewsfeedLoad() { | |
if(this.loading) { | |
return; | |
} | |
this.flux.actions.RequestsActions.load({ | |
name: NewsfeedConstants.NEWSFEED, | |
promise: Github.newsfeed(this.getNextPageUrl()) | |
}); | |
this.loading = true; | |
this.error = null; | |
this.emit('change'); | |
} | |
... | |
}; |
This file contains hidden or 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 RequestsContants = require('../constants/RequestsConstants'); | |
var Promise = require("bluebird"); | |
var _ = require('lodash'); | |
module.exports = { | |
load: function(args) { | |
var dispatcher = this; | |
if(_.isEmpty(args.name)) { | |
throw new Error("RequestWrapper requires name to dispatch"); | |
} else if(_.isEmpty(args.promise)) { | |
throw new Error("RequestWrapper requires promise to watch"); | |
} | |
var requestId = _.uniqueId(); | |
// We'd like to track requests but avoid problems with dispatcher | |
setTimeout(function() { | |
dispatcher.dispatch(RequestsContants.REQUEST_LOAD, { requestId: requestId, name: args.name }); | |
}, 0); | |
var finished = false; | |
var request = args | |
.promise | |
.catch(Promise.CancellationError, function() { | |
dispatcher.dispatch(RequestsContants.REQUEST_CANCEL, { requestId: requestId, name: args.name }); | |
}) | |
.then(function(response) { | |
if(finished) { | |
return response; | |
} | |
dispatcher.dispatch(RequestsContants.REQUEST_SUCCESS, { requestId: requestId, name: args.name }); | |
dispatcher.dispatch(args.name + '_SUCCESS', { | |
requestId: requestId, | |
response: response, | |
source: args.source | |
}); | |
}, function(response) { | |
if(finished) { | |
return response; | |
} | |
dispatcher.dispatch(RequestsContants.REQUEST_ERROR, { requestId: requestId, name: args.name }); | |
dispatcher.dispatch(args.name + '_ERROR', { | |
requestId: requestId, | |
response: response, | |
source: args.source | |
}); | |
}); | |
if(args.timeout) { | |
request.timeout(args.timeout) | |
.catch(Promise.TimeoutError, function() { | |
dispatcher.dispatch(RequestsContants.REQUEST_TIMEOUT, { requestId: requestId, name: args.name }); | |
}) | |
} | |
request.finally(function() { | |
dispatcher.dispatch(RequestsContants.REQUEST_FINISH, { requestId: requestId, name: args.name }); | |
finished = true; | |
}) | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment