-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
2,148 additions
and
108 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,159 @@ | ||
(function(global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define([ 'exports' ], factory) : factory(global.cooldux = {}); | ||
})(this, function(exports) { | ||
'use strict'; | ||
var typeIndex = 0; | ||
function createRandomType() { | ||
typeIndex++; | ||
return 'RAND_' + typeIndex + '_' + Math.random(); | ||
'use strict'; | ||
|
||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
|
||
var typeIndex = 0; | ||
|
||
function createRandomType() { | ||
typeIndex++; | ||
return 'RAND_' + typeIndex + '_' + Math.random(); | ||
} | ||
|
||
function makeActionCreator(type) { | ||
if (type === void 0) { | ||
type = createRandomType(); | ||
} | ||
function makeActionCreator(type) { | ||
if (type === void 0) { | ||
type = createRandomType(); | ||
} | ||
var actionCreator = function(payload) { | ||
return { | ||
type: type, | ||
payload: payload | ||
}; | ||
var actionCreator = function(payload) { | ||
return { | ||
type: type, | ||
payload: payload | ||
}; | ||
actionCreator.type = type; | ||
return actionCreator; | ||
}; | ||
actionCreator.type = type; | ||
return actionCreator; | ||
} | ||
|
||
var reset = makeActionCreator('cooldux-RESET'); | ||
|
||
function resetReducer(initialState, reducer) { | ||
return function(state, action) { | ||
return reset.type === action.type ? initialState : reducer(state, action); | ||
}; | ||
} | ||
|
||
function promiseHandler(type, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
var reset = makeActionCreator('cooldux-RESET'); | ||
function resetReducer(initialState, reducer) { | ||
return function(state, action) { | ||
return reset.type === action.type ? initialState : reducer(state, action); | ||
if (typeof options === 'string') { | ||
options = { | ||
namespace: options | ||
}; | ||
} | ||
function promiseHandler(type, options) { | ||
if (options === void 0) { | ||
options = {}; | ||
} | ||
if (typeof options === 'string') { | ||
options = { | ||
namespace: options | ||
}; | ||
} | ||
var name = (options.namespace ? options.namespace + '-' : '') + type; | ||
var initialState = {}; | ||
initialState[type] = null; | ||
initialState[type + 'Pending'] = false; | ||
initialState[type + 'Error'] = null; | ||
var creators = {}; | ||
creators[type + 'InitialState'] = initialState; | ||
creators[type + 'Start'] = makeActionCreator(name + '_Start'); | ||
creators[type + 'End'] = makeActionCreator(name + '_End'); | ||
creators[type + 'Error'] = makeActionCreator(name + '_Error'); | ||
creators[type + 'Handler'] = function(promise, dispatch) { | ||
dispatch(creators[type + 'Start']()); | ||
return promise.then(function(result) { | ||
dispatch(creators[type + 'End'](result)); | ||
return result; | ||
}).catch(function(error) { | ||
dispatch(creators[type + 'Error'](error)); | ||
if (options.throwErrors) { | ||
throw error; | ||
} | ||
return null; | ||
}); | ||
}; | ||
creators[type + 'Reducer'] = function(state, action) { | ||
var obj, obj$1, obj$2; | ||
state = state || initialState; | ||
switch (action.type) { | ||
case creators[type + 'Start'].type: | ||
return Object.assign({}, state, (obj = {}, obj[type + 'Pending'] = true, obj[type + 'Error'] = null, | ||
obj)); | ||
|
||
case creators[type + 'End'].type: | ||
return Object.assign({}, state, (obj$1 = {}, obj$1[type + 'Pending'] = false, obj$1[type + 'Error'] = null, | ||
obj$1[type] = action.payload, obj$1)); | ||
|
||
case creators[type + 'Error'].type: | ||
return Object.assign({}, state, (obj$2 = {}, obj$2[type + 'Pending'] = false, obj$2[type + 'Error'] = action.payload, | ||
obj$2)); | ||
|
||
default: | ||
return state; | ||
var name = (options.namespace ? options.namespace + '-' : '') + type; | ||
var initialState = {}; | ||
initialState[type] = null; | ||
initialState[type + 'Pending'] = false; | ||
initialState[type + 'Error'] = null; | ||
var creators = {}; | ||
creators[type + 'InitialState'] = initialState; | ||
creators[type + 'Start'] = makeActionCreator(name + '_Start'); | ||
creators[type + 'End'] = makeActionCreator(name + '_End'); | ||
creators[type + 'Error'] = makeActionCreator(name + '_Error'); | ||
creators[type + 'Handler'] = function(promise, dispatch) { | ||
dispatch(creators[type + 'Start']()); | ||
return promise.then(function(result) { | ||
dispatch(creators[type + 'End'](result)); | ||
return result; | ||
}).catch(function(error) { | ||
dispatch(creators[type + 'Error'](error)); | ||
if (options.throwErrors) { | ||
throw error; | ||
} | ||
return null; | ||
}); | ||
}; | ||
creators[type + 'Action'] = function(promise) { | ||
promise._cooldux = { | ||
name: name, | ||
options: options | ||
}; | ||
return creators; | ||
} | ||
function combinedHandler(types, options) { | ||
var handlers = {}; | ||
var initialState = types.reduce(function(state, type) { | ||
var handler = promiseHandler(type, options); | ||
Object.assign(handlers, handler); | ||
Object.assign(state, handler[type + 'InitialState']); | ||
return promise; | ||
}; | ||
creators[type + 'Reducer'] = function(state, action) { | ||
var obj, obj$1, obj$2, obj$3, obj$4, obj$5; | ||
state = state || initialState; | ||
switch (action.type) { | ||
case creators[type + 'Start'].type: | ||
return Object.assign({}, state, (obj = {}, obj[type + 'Pending'] = true, obj), (obj$1 = {}, | ||
obj$1[type + 'Error'] = null, obj$1)); | ||
|
||
case creators[type + 'End'].type: | ||
return Object.assign({}, state, (obj$2 = {}, obj$2[type + 'Pending'] = false, obj$2), (obj$3 = {}, | ||
obj$3[type] = action.payload, obj$3)); | ||
|
||
case creators[type + 'Error'].type: | ||
return Object.assign({}, state, (obj$4 = {}, obj$4[type + 'Pending'] = false, obj$4), (obj$5 = {}, | ||
obj$5[type + 'Error'] = action.payload, obj$5)); | ||
|
||
default: | ||
return state; | ||
}, {}); | ||
Object.assign(handlers, { | ||
initialStateCombined: Object.assign({}, initialState), | ||
reducerCombined: function(state, action) { | ||
return types.reduce(function(state, type) { | ||
return handlers[type + 'Reducer'](state, action); | ||
}, Object.assign({}, state || initialState)); | ||
} | ||
}); | ||
return handlers; | ||
} | ||
exports.makeActionCreator = makeActionCreator; | ||
exports.reset = reset; | ||
exports.resetReducer = resetReducer; | ||
exports.promiseHandler = promiseHandler; | ||
exports.combinedHandler = combinedHandler; | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
} | ||
}; | ||
return creators; | ||
} | ||
|
||
function combinedHandler(types, options) { | ||
var handlers = {}; | ||
var initialState = types.reduce(function(state, type) { | ||
var handler = promiseHandler(type, options); | ||
Object.assign(handlers, handler); | ||
Object.assign(state, handler[type + 'InitialState']); | ||
return state; | ||
}, {}); | ||
Object.assign(handlers, { | ||
initialStateCombined: Object.assign({}, initialState), | ||
reducerCombined: function(state, action) { | ||
return types.reduce(function(state, type) { | ||
return handlers[type + 'Reducer'](state, action); | ||
}, Object.assign({}, state || initialState)); | ||
} | ||
}); | ||
}); | ||
return handlers; | ||
} | ||
|
||
var promiseMiddleware = function(ref) { | ||
var dispatch = ref.dispatch; | ||
return function(next) { | ||
return function(action) { | ||
if (action.then && action._cooldux) { | ||
var _cooldux = action._cooldux; | ||
dispatch({ | ||
type: _cooldux.name + '_Start' | ||
}); | ||
return action.then(function(payload) { | ||
dispatch({ | ||
type: _cooldux.name + '_End', | ||
payload: payload | ||
}); | ||
return payload; | ||
}).catch(function(err) { | ||
dispatch({ | ||
type: _cooldux.name + '_Error', | ||
payload: err | ||
}); | ||
if (_cooldux.options.throwErrors) { | ||
throw err; | ||
} | ||
return null; | ||
}); | ||
} | ||
next(action); | ||
return action; | ||
}; | ||
}; | ||
}; | ||
|
||
exports.makeActionCreator = makeActionCreator; | ||
|
||
exports.reset = reset; | ||
|
||
exports.resetReducer = resetReducer; | ||
|
||
exports.promiseHandler = promiseHandler; | ||
|
||
exports.combinedHandler = combinedHandler; | ||
|
||
exports.promiseMiddleware = promiseMiddleware; |
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
Oops, something went wrong.