Last active
November 5, 2015 20:52
-
-
Save goatslacker/900623f50310406d7c57 to your computer and use it in GitHub Desktop.
dataSources.js
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
import isPromise from 'is-promise' | |
import { generateActions } from 'create-actions' | |
// get the action creators that will be created given a namespace | |
export const getActionCreators = (namespace) => { | |
return generateActions(namespace, [ | |
'begin', | |
'success', | |
'failure', | |
'end', | |
]) | |
} | |
// convenience function so you can use function call form instead of creating | |
// the object yourself | |
export const send = (id, value) => { | |
return { id, value } | |
} | |
// Creates a function that when called with a dispatcher will | |
// create all the necessary actions for you and dispatch them in the correct | |
// order. Basic caching is also provided by the utility | |
const createAsyncDispatch = (namespace, fn, opts) => { | |
const { begin, success, failure, end } = { | |
...getActionCreators(namespace), | |
...opts, | |
} | |
const store = { pending: {}, requests: {} } | |
return (...args) => dispatcher => { | |
const { | |
id = namespace, | |
value, | |
} = fn(...args) | |
const beginDispatch = () => dispatcher.dispatch(begin(id)) | |
const endDispatch = () => { | |
delete store.pending[id] | |
if (!opts.cache) delete store.requests[id] | |
dispatcher.dispatch(end(id)) | |
} | |
const asyncDispatch = (value) => { | |
value.then(beginDispatch, beginDispatch) | |
const result = value.then((res) => { | |
dispatcher.dispatch(success(res)) | |
return res | |
}, (err) => { | |
dispatcher.dispatch({ | |
...failure(err), | |
error: true, | |
}) | |
return Promise.reject(err) | |
}) | |
store.pending[id] = true | |
store.requests[id] = result | |
value.then(endDispatch, endDispatch) | |
return result | |
} | |
const result = value({ | |
state: opts.getState ? opts.getState(dispatcher) : null, | |
isLoading: () => store.pending.hasOwnProperty(id), | |
getRequest: () => store.requests[id], | |
}, dispatcher) | |
// remote request | |
if (isPromise(result)) { | |
return asyncDispatch(result) | |
// a function, handle your own async dispatches | |
} else if (typeof result === 'function') { | |
return result(asyncDispatch) | |
} | |
// local value | |
return result | |
} | |
} | |
export default createAsyncDispatch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage