@eknkc/dux
Advanced tools
Comparing version 3.0.8 to 3.1.0
28
build.js
@@ -145,7 +145,7 @@ 'use strict'; | ||
var nilAction = createAction("NIL")(); | ||
var scopedAction = createAction("scopedAction"); | ||
function scopeReducer(scope, reducer) { | ||
return function (state, action) { | ||
if (!action || !action.meta || action.meta.scope !== scope) return reducer(state, nilAction); | ||
if (action.type === scopedAction.type && action.meta.scope === scope) action = action.payload; | ||
@@ -157,5 +157,23 @@ return reducer(state, action); | ||
function scopeAction(scope, action) { | ||
return function (payload) { | ||
var meta = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
return action(payload, Object.assign({}, meta, { scope: scope })); | ||
function wrapResult(result) { | ||
if (typeof result === 'function') return function (dispatch, getState) { | ||
return result(function (action) { | ||
return dispatch(wrapResult(action)); | ||
}, getState); | ||
}; | ||
if (typeof result.then === 'function') return result.then(function (r) { | ||
return wrapResult(r); | ||
}); | ||
return scopedAction(result, { | ||
scope: scope, | ||
logTransform: function logTransform() { | ||
return Object.assign({}, result, { type: '[' + scope + '] ' + String(result.type) }); | ||
} | ||
}); | ||
} | ||
return function (payload, meta) { | ||
return wrapResult(action(payload, meta)); | ||
}; | ||
@@ -162,0 +180,0 @@ } |
21
index.js
@@ -100,8 +100,8 @@ import Immutable from 'seamless-immutable' | ||
const nilAction = createAction("NIL")(); | ||
const scopedAction = createAction("scopedAction"); | ||
export function scopeReducer(scope, reducer) { | ||
return (state, action) => { | ||
if (!action || !action.meta || action.meta.scope !== scope) | ||
return reducer(state, nilAction); | ||
if (action.type === scopedAction.type && action.meta.scope === scope) | ||
action = action.payload | ||
@@ -113,3 +113,16 @@ return reducer(state, action); | ||
export function scopeAction(scope, action) { | ||
return (payload, meta = {}) => action(payload, Object.assign({}, meta, { scope })) | ||
function wrapResult(result) { | ||
if (typeof result === 'function') | ||
return (dispatch, getState) => result(action => dispatch(wrapResult(action)), getState) | ||
if (typeof result.then === 'function') | ||
return result.then(r => wrapResult(r)); | ||
return scopedAction(result, { | ||
scope, | ||
logTransform: () => Object.assign({}, result, { type: `[${scope}] ${String(result.type)}` }) | ||
}); | ||
} | ||
return (payload, meta) => wrapResult(action(payload, meta)); | ||
} | ||
@@ -116,0 +129,0 @@ |
{ | ||
"name": "@eknkc/dux", | ||
"version": "3.0.8", | ||
"version": "3.1.0", | ||
"description": "Redux helpers", | ||
@@ -5,0 +5,0 @@ "main": "build.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
11374
267