apeman-react-mixins
Version:
React mixin set of apeman.
54 lines (43 loc) • 1.06 kB
JSX
/**
* Mixin for cycle.
* @mixin ApCycleMixin
*/
import React, {PropTypes as types} from 'react'
import defaults from 'defaults'
let noop = (values) => values
let reject = (err) => Promise.reject(err)
/** @lends ApCycleMixin */
let ApCycleMixin = {
// --------------------
// Custom
// --------------------
$apCycleMixed: true,
/**
* Execute something with cycle.
* @param {string|number|object} params - Parameters.
* @param {object} handlers - Fetch handlers.
*/
cycle (params, handlers) {
const s = this
defaults(handlers, {
will: noop,
do: noop,
did: noop,
catch: reject
})
return Promise
.resolve(params)
.then((params) => handlers.will(params))
.then((params) => handlers.do(params))
.then((result) => handlers.did(result))
.catch((err) => handlers.catch(err))
}
// --------------------
// Specs
// --------------------
// --------------------
// Lifecycle
// --------------------
}
export default Object.freeze(ApCycleMixin)