Skip to content

Commit

Permalink
[add] Intercepting response
Browse files Browse the repository at this point in the history
  • Loading branch information
goatslacker committed May 26, 2015
1 parent cea29a4 commit 6f074a3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
7 changes: 4 additions & 3 deletions src/alt/store/StoreMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,21 @@ const StoreMixin = {
const state = this.getInstance().getState()
const value = spec.local && spec.local(state, ...args)
const shouldFetch = spec.shouldFetch ? spec.shouldFetch(state, ...args) : !value
const intercept = spec.interceptResponse || (x => x)

// if we don't have it in cache then fetch it
if (shouldFetch) {
loadCounter += 1
/* istanbul ignore else */
if (spec.loading) spec.loading()
if (spec.loading) spec.loading(intercept(null, spec.loading))
spec.remote(state, ...args)
.then((v) => {
loadCounter -= 1
spec.success(v)
spec.success(intercept(v, spec.success))
})
.catch((v) => {
loadCounter -= 1
spec.error(v)
spec.error(intercept(v, spec.error))
})
} else {
// otherwise emit the change now
Expand Down
9 changes: 6 additions & 3 deletions test/async-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,12 @@ const StargazerSource = {

fetchRepos: {
remote() {
return new Promise((resolve, reject) => {
setTimeout(() => resolve('TESTTEST'), 200)
})
return Promise.resolve('batman')
},
interceptResponse(x, action) {
assert(x === 'batman')
assert(action === StargazerActions.usersReceived)
return 'TESTTEST'
},
success: StargazerActions.usersReceived,
error: StargazerActions.failed
Expand Down

0 comments on commit 6f074a3

Please sign in to comment.