babel-plugin-transform-pattern-matching
Advanced tools
Comparing version 0.0.7 to 0.0.8
@@ -7,5 +7,5 @@ 'use strict'; | ||
var _decorators = require('./decorators'); | ||
var _expr = require('./expr'); | ||
for (var _key in _decorators) { | ||
for (var _key in _expr) { | ||
if (_key === "default") continue; | ||
@@ -15,3 +15,3 @@ Object.defineProperty(exports, _key, { | ||
get: function get() { | ||
return _decorators[_key]; | ||
return _expr[_key]; | ||
} | ||
@@ -18,0 +18,0 @@ }); |
{ | ||
"name": "babel-plugin-transform-pattern-matching", | ||
"version": "0.0.7", | ||
"version": "0.0.8", | ||
"description": "transform pattern mathing", | ||
@@ -44,2 +44,3 @@ "main": "index.js", | ||
"babel-core": "^6.1.21", | ||
"babel-plugin-espower": "^2.0.0", | ||
"babel-plugin-syntax-flow": "^6.1.18", | ||
@@ -50,4 +51,5 @@ "babel-plugin-transform-flow-strip-types": "^6.1.18", | ||
"glob": "^6.0.1", | ||
"mocha": "^2.3.3" | ||
"mocha": "^2.3.3", | ||
"power-assert": "^1.2.0" | ||
} | ||
} |
@@ -5,24 +5,80 @@ 'use strict'; | ||
var _decorators = require('../../../decorators'); | ||
var _expr = require('../../../expr'); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var TodoAddMultipleAction = function TodoAddMultipleAction() { | ||
_classCallCheck(this, TodoAddMultipleAction); | ||
var Action = function Action() { | ||
_classCallCheck(this, Action); | ||
}; | ||
var TodoAddMultipleErrorAction = function TodoAddMultipleErrorAction() { | ||
_classCallCheck(this, TodoAddMultipleErrorAction); | ||
}; | ||
var TodoAddMultipleAction = (function (_Action) { | ||
_inherits(TodoAddMultipleAction, _Action); | ||
var TodoAddErrorAction = function TodoAddErrorAction() { | ||
_classCallCheck(this, TodoAddErrorAction); | ||
}; | ||
function TodoAddMultipleAction() { | ||
_classCallCheck(this, TodoAddMultipleAction); | ||
var TodoCompleteErrorAction = function TodoCompleteErrorAction() { | ||
_classCallCheck(this, TodoCompleteErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoAddMultipleAction).apply(this, arguments)); | ||
} | ||
return TodoAddMultipleAction; | ||
})(Action); | ||
var TodoAddMultipleErrorAction = (function (_Action2) { | ||
_inherits(TodoAddMultipleErrorAction, _Action2); | ||
function TodoAddMultipleErrorAction() { | ||
_classCallCheck(this, TodoAddMultipleErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoAddMultipleErrorAction).apply(this, arguments)); | ||
} | ||
return TodoAddMultipleErrorAction; | ||
})(Action); | ||
var TodoAddErrorAction = (function (_Action3) { | ||
_inherits(TodoAddErrorAction, _Action3); | ||
function TodoAddErrorAction() { | ||
_classCallCheck(this, TodoAddErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoAddErrorAction).apply(this, arguments)); | ||
} | ||
return TodoAddErrorAction; | ||
})(Action); | ||
var TodoCompleteErrorAction = (function (_Action4) { | ||
_inherits(TodoCompleteErrorAction, _Action4); | ||
function TodoCompleteErrorAction() { | ||
_classCallCheck(this, TodoCompleteErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoCompleteErrorAction).apply(this, arguments)); | ||
} | ||
return TodoCompleteErrorAction; | ||
})(Action); | ||
var TodoDestroyErrorAction = (function (_Action5) { | ||
_inherits(TodoDestroyErrorAction, _Action5); | ||
function TodoDestroyErrorAction() { | ||
_classCallCheck(this, TodoDestroyErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoDestroyErrorAction).apply(this, arguments)); | ||
} | ||
return TodoDestroyErrorAction; | ||
})(Action); | ||
var A = function A() { | ||
_classCallCheck(this, A); | ||
}; | ||
var TodoDestroyErrorAction = function TodoDestroyErrorAction() { | ||
_classCallCheck(this, TodoDestroyErrorAction); | ||
var B = function B() { | ||
_classCallCheck(this, B); | ||
}; | ||
@@ -36,12 +92,18 @@ | ||
_createClass(ExampleReducer, [{ | ||
key: 'reduce', | ||
value: function reduce(a, action, b) { | ||
(0, _expr.babelPatternMatch)(action); | ||
return a; | ||
} | ||
}, { | ||
key: 'addMultiple', | ||
value: function addMultiple(todos, action) { | ||
return todos; | ||
value: function addMultiple(a, action, b) { | ||
return a; | ||
} | ||
}, { | ||
key: 'addError', | ||
value: function addError(todos, _ref) { | ||
value: function addError(a, _ref, b) { | ||
var error = _ref.error; | ||
return todos; | ||
return a; | ||
} | ||
@@ -48,0 +110,0 @@ }]); |
@@ -5,24 +5,80 @@ 'use strict'; | ||
var _decorators = require('../../../decorators'); | ||
var _expr = require('../../../expr'); | ||
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } | ||
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
var TodoAddMultipleAction = function TodoAddMultipleAction() { | ||
_classCallCheck(this, TodoAddMultipleAction); | ||
var Action = function Action() { | ||
_classCallCheck(this, Action); | ||
}; | ||
var TodoAddMultipleErrorAction = function TodoAddMultipleErrorAction() { | ||
_classCallCheck(this, TodoAddMultipleErrorAction); | ||
}; | ||
var TodoAddMultipleAction = (function (_Action) { | ||
_inherits(TodoAddMultipleAction, _Action); | ||
var TodoAddErrorAction = function TodoAddErrorAction() { | ||
_classCallCheck(this, TodoAddErrorAction); | ||
}; | ||
function TodoAddMultipleAction() { | ||
_classCallCheck(this, TodoAddMultipleAction); | ||
var TodoCompleteErrorAction = function TodoCompleteErrorAction() { | ||
_classCallCheck(this, TodoCompleteErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoAddMultipleAction).apply(this, arguments)); | ||
} | ||
return TodoAddMultipleAction; | ||
})(Action); | ||
var TodoAddMultipleErrorAction = (function (_Action2) { | ||
_inherits(TodoAddMultipleErrorAction, _Action2); | ||
function TodoAddMultipleErrorAction() { | ||
_classCallCheck(this, TodoAddMultipleErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoAddMultipleErrorAction).apply(this, arguments)); | ||
} | ||
return TodoAddMultipleErrorAction; | ||
})(Action); | ||
var TodoAddErrorAction = (function (_Action3) { | ||
_inherits(TodoAddErrorAction, _Action3); | ||
function TodoAddErrorAction() { | ||
_classCallCheck(this, TodoAddErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoAddErrorAction).apply(this, arguments)); | ||
} | ||
return TodoAddErrorAction; | ||
})(Action); | ||
var TodoCompleteErrorAction = (function (_Action4) { | ||
_inherits(TodoCompleteErrorAction, _Action4); | ||
function TodoCompleteErrorAction() { | ||
_classCallCheck(this, TodoCompleteErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoCompleteErrorAction).apply(this, arguments)); | ||
} | ||
return TodoCompleteErrorAction; | ||
})(Action); | ||
var TodoDestroyErrorAction = (function (_Action5) { | ||
_inherits(TodoDestroyErrorAction, _Action5); | ||
function TodoDestroyErrorAction() { | ||
_classCallCheck(this, TodoDestroyErrorAction); | ||
return _possibleConstructorReturn(this, Object.getPrototypeOf(TodoDestroyErrorAction).apply(this, arguments)); | ||
} | ||
return TodoDestroyErrorAction; | ||
})(Action); | ||
var A = function A() { | ||
_classCallCheck(this, A); | ||
}; | ||
var TodoDestroyErrorAction = function TodoDestroyErrorAction() { | ||
_classCallCheck(this, TodoDestroyErrorAction); | ||
var B = function B() { | ||
_classCallCheck(this, B); | ||
}; | ||
@@ -33,9 +89,10 @@ | ||
_classCallCheck(this, ExampleReducer); | ||
} | ||
var _this = this; | ||
this.reducer = function _ExampleReducer(state, action) { | ||
_createClass(ExampleReducer, [{ | ||
key: 'reduce', | ||
value: function reduce(a, action, b) { | ||
switch (action.constructor) { | ||
case TodoAddMultipleAction: | ||
return _this.addMultiple(state, action); | ||
return this.addMultiple(a, action, b); | ||
@@ -46,21 +103,21 @@ case TodoAddMultipleErrorAction: | ||
case TodoDestroyErrorAction: | ||
return _this.addError(state, action); | ||
return this.addError(a, action, b); | ||
default: | ||
return state; | ||
break; | ||
} | ||
}; | ||
} | ||
_createClass(ExampleReducer, [{ | ||
return a; | ||
} | ||
}, { | ||
key: 'addMultiple', | ||
value: function addMultiple(todos, action) { | ||
return todos; | ||
value: function addMultiple(a, action, b) { | ||
return a; | ||
} | ||
}, { | ||
key: 'addError', | ||
value: function addError(todos, _ref) { | ||
value: function addError(a, _ref, b) { | ||
var error = _ref.error; | ||
return todos; | ||
return a; | ||
} | ||
@@ -67,0 +124,0 @@ }]); |
@@ -13,5 +13,5 @@ 'use strict'; | ||
var _assert = require('assert'); | ||
var _powerAssert = require('power-assert'); | ||
var _assert2 = _interopRequireDefault(_assert); | ||
var _powerAssert2 = _interopRequireDefault(_powerAssert); | ||
@@ -23,4 +23,5 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
var exampleFile = _fs2.default.readFileSync(_path2.default.join(__dirname, 'data', 'ExampleReducer.js')).toString(); | ||
var exampleTranspiledFile = _fs2.default.readFileSync(_path2.default.join(__dirname, 'data', 'ExampleReducerTranspiled.js')).toString(); | ||
var exampleTranspiledFileName = _path2.default.join(__dirname, 'data', 'ExampleReducerTranspiled.js'); | ||
var pluginPath = _path2.default.join(__dirname, '..', '..', 'index.js'); | ||
@@ -37,7 +38,12 @@ | ||
var code = _transform.code; | ||
// fs.writeFileSync(path.join(__dirname, 'data', 'ExampleReducerTranspiled.js'), code) | ||
// fs.writeFileSync(exampleTranspiledFileName, code) | ||
(0, _assert2.default)(code === exampleTranspiledFile); | ||
var exampleTranspiledFile = _fs2.default.readFileSync(exampleTranspiledFileName).toString(); | ||
(0, _powerAssert2.default)(_powerAssert2.default._expr(_powerAssert2.default._capt(_powerAssert2.default._capt(code, 'arguments/0/left') === _powerAssert2.default._capt(exampleTranspiledFile, 'arguments/0/right'), 'arguments/0'), { | ||
content: 'assert(code === exampleTranspiledFile)', | ||
filepath: 'src/plugin/__tests__/patternMatchingBabelPluginTest.js', | ||
line: 28 | ||
})); | ||
}); | ||
}); | ||
//# sourceMappingURL=patternMatchingBabelPluginTest.js.map |
@@ -6,8 +6,8 @@ 'use strict'; | ||
}); | ||
var findReducerDecoratorExpressionVisitor = { | ||
CallExpression: function CallExpression(path, state) { | ||
var node = path.node; | ||
if (node.callee.name === state.reducerLabel && node.arguments && node.arguments[0]) { | ||
state.argNum = node.arguments[0].value; | ||
var findReducerExpressionVisitor = { | ||
ExpressionStatement: function ExpressionStatement(path, state) { | ||
var expr = path.node.expression; | ||
if (expr.callee.name === state.reducerLabel) { | ||
state.expressionParentPath = path; | ||
state.expression = expr; | ||
} | ||
@@ -17,32 +17,15 @@ } | ||
var findReducerDecoratorVisitor = { | ||
Decorator: function Decorator(path, state) { | ||
var exprState = { | ||
reducerLabel: state.reducerLabel, | ||
argNum: null | ||
}; | ||
path.traverse(findReducerDecoratorExpressionVisitor, exprState); | ||
if (exprState.argNum) { | ||
state.argNum = exprState.argNum; | ||
state.decoratorPath = path; | ||
} | ||
} | ||
}; | ||
var findReducerPropertyVisitor = { | ||
'ClassProperty|ClassMethod': function ClassPropertyClassMethod(path, state) { | ||
'ClassMethod': function ClassMethod(path, state) { | ||
var propState = { | ||
reducerLabel: state.reducerLabel, | ||
argNum: null, | ||
decoratorPath: null | ||
expressionParentPath: null, | ||
expression: null | ||
}; | ||
path.traverse(findReducerDecoratorVisitor, propState); | ||
if (propState.argNum) { | ||
state.argNum = propState.argNum; | ||
path.traverse(findReducerExpressionVisitor, propState); | ||
if (propState.expression) { | ||
state.prop = path.node; | ||
state.decoratorPath = propState.decoratorPath; | ||
state.expressionParentPath = propState.expressionParentPath; | ||
state.expression = propState.expression; | ||
} | ||
if (path.node.kind === 'constructor') { | ||
state.constr = path.node; | ||
} | ||
} | ||
@@ -49,0 +32,0 @@ }; |
@@ -8,8 +8,7 @@ 'use strict'; | ||
function generateCase(_ref) { | ||
var args = _ref.args; | ||
var method = _ref.method; | ||
var t = _ref.t; | ||
var thisRef = _ref.thisRef; | ||
var types = _ref.types; | ||
var actionId = _ref.actionId; | ||
var stateId = _ref.stateId; | ||
var thisRef = _ref.thisRef; | ||
var method = _ref.method; | ||
@@ -19,3 +18,3 @@ return types.map(function (type, index) { | ||
if (index === types.length - 1) { | ||
ret.push(t.returnStatement(t.callExpression(t.memberExpression(thisRef, t.identifier(method)), [stateId, actionId]))); | ||
ret.push(t.returnStatement(t.callExpression(t.memberExpression(thisRef, t.identifier(method)), args))); | ||
} | ||
@@ -27,9 +26,9 @@ return t.switchCase(t.identifier(type), ret); | ||
function generateSwitchCase(_ref2) { | ||
var args = _ref2.args; | ||
var switchArg = _ref2.switchArg; | ||
var t = _ref2.t; | ||
var thisRef = _ref2.thisRef; | ||
var typesMap = _ref2.typesMap; | ||
var thisRef = _ref2.thisRef; | ||
var stateId = _ref2.stateId; | ||
var actionId = _ref2.actionId; | ||
return t.blockStatement([t.switchStatement(t.memberExpression(actionId, t.identifier('constructor')), typesMap.reduce(function (cases, _ref3) { | ||
return t.switchStatement(t.memberExpression(switchArg, t.identifier('constructor')), typesMap.reduce(function (cases, _ref3) { | ||
var types = _ref3.types; | ||
@@ -39,11 +38,10 @@ var method = _ref3.method; | ||
return cases.concat(generateCase({ | ||
args: args, | ||
method: method, | ||
t: t, | ||
actionId: actionId, | ||
stateId: stateId, | ||
types: types, | ||
method: method, | ||
thisRef: thisRef | ||
thisRef: thisRef, | ||
types: types | ||
})); | ||
}, []).concat([t.switchCase(null, [t.returnStatement(stateId)])]))]); | ||
}, []).concat([t.switchCase(null, [t.breakStatement()])])); | ||
} | ||
//# sourceMappingURL=generateSwitchCase.js.map |
@@ -26,2 +26,5 @@ 'use strict'; | ||
var node = path.node; | ||
if (node === state.skipProp) { | ||
return; | ||
} | ||
var params = node.params; | ||
@@ -28,0 +31,0 @@ var key = node.key; |
'use strict'; | ||
var _slicedToArray = (function () { function sliceIterator(arr, i) { var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"]) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } return function (arr, i) { if (Array.isArray(arr)) { return arr; } else if (Symbol.iterator in Object(arr)) { return sliceIterator(arr, i); } else { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } }; })(); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -16,10 +18,6 @@ value: true | ||
var _createConstructorBlock = require('./createConstructorBlock'); | ||
var _generateSwitchCase = require('./generateSwitchCase'); | ||
var _createConstructorBlock2 = _interopRequireDefault(_createConstructorBlock); | ||
var _generateSwitchCase2 = _interopRequireDefault(_generateSwitchCase); | ||
var _createNewConstructor = require('./createNewConstructor'); | ||
var _createNewConstructor2 = _interopRequireDefault(_createNewConstructor); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -37,39 +35,38 @@ | ||
var state = { | ||
reducerLabel: 'generateReducer', | ||
argNum: null, | ||
reducerLabel: 'babelPatternMatch', | ||
prop: null, | ||
decoratorPath: null, | ||
constr: null | ||
expressionParentPath: null, | ||
expression: null | ||
}; | ||
path.traverse(_findReducerPropertyVisitor2.default, state); | ||
if (!state.prop) { | ||
if (!state.prop || !state.expression) { | ||
return; | ||
} | ||
var _state$expression$arg = _slicedToArray(state.expression.arguments, 2); | ||
var switchArg = _state$expression$arg[0]; | ||
var thisRef = _state$expression$arg[1]; | ||
var args = state.prop.params; | ||
var argNum = args.findIndex(function (_ref2) { | ||
var name = _ref2.name; | ||
return name === switchArg.name; | ||
}); | ||
var typesState = { | ||
types: [], | ||
argNum: state.argNum | ||
skipProp: state.prop, | ||
argNum: argNum | ||
}; | ||
path.traverse(_getTypesFromClassMethodsVisitor2.default, typesState); | ||
if (typesState.types.length) { | ||
var constructorBlock = (0, _createConstructorBlock2.default)({ | ||
t: t, | ||
typesMap: typesState.types, | ||
reducerRef: state.prop.key, | ||
newReducerFnId: scope.generateUidIdentifier(node.id.name), | ||
thisRef: scope.generateUidIdentifier('this') | ||
}); | ||
if (state.constr) { | ||
var ConstrBody = state.constr.body; | ||
ConstrBody.body = ConstrBody.body.concat(constructorBlock); | ||
} else { | ||
var body = node.body; | ||
body.body = [(0, _createNewConstructor2.default)({ t: t, constructorBlock: constructorBlock })].concat(body.body); | ||
} | ||
} | ||
state.decoratorPath.remove(); | ||
var switchCase = (0, _generateSwitchCase2.default)({ | ||
args: args, | ||
switchArg: switchArg, | ||
t: t, | ||
thisRef: thisRef || t.thisExpression(), | ||
typesMap: typesState.types | ||
}); | ||
state.expressionParentPath.replaceWith(switchCase); | ||
} | ||
@@ -76,0 +73,0 @@ } |
104
README.md
@@ -9,26 +9,37 @@ babel-plugin-transform-pattern-matching | ||
```js | ||
import {generateReducer} from 'babel-plugin-transform-pattern-matching/decorators' | ||
import type {ReducerFn} from 'babel-plugin-transform-pattern-matching/decorators' | ||
import {babelPatternMatch} from 'babel-plugin-transform-pattern-matching/expr' | ||
class Todos { | ||
items: Array<{id: string, title: string}>; | ||
error: string; | ||
} | ||
class Action {} | ||
class TodoAddMultipleAction extends Action {} | ||
class TodoAddMultipleErrorAction extends Action {} | ||
class TodoAddErrorAction extends Action {} | ||
class TodoCompleteErrorAction extends Action {} | ||
class TodoDestroyErrorAction extends Action {} | ||
class TodoReducer { | ||
@generateReducer(1) | ||
reducer: ReducerFn<Todos>; | ||
class A {} | ||
class B {} | ||
add(todos: Todos, {id, title}: TodoAddAction): Todos { | ||
todos.items.push({id, title}) | ||
return todos | ||
type AnyAction = Object & Action | ||
class ExampleReducer { | ||
reduce(a: A, action: AnyAction, b: B): A { | ||
babelPatternMatch(action) | ||
return a | ||
} | ||
error(todos: Todos, {error}: TodoAddErrorAction | TodoRemoveErrorAction): Todos { | ||
todos.error = error | ||
return todos | ||
addMultiple(a: A, action: TodoAddMultipleAction, b: B): A { | ||
return a | ||
} | ||
addError( | ||
a: A, | ||
{error}: TodoAddMultipleErrorAction | ||
| TodoAddErrorAction | ||
| TodoCompleteErrorAction | ||
| TodoDestroyErrorAction, | ||
b: B | ||
): A { | ||
return a | ||
} | ||
} | ||
export default new TodoReducer().reducer | ||
``` | ||
@@ -39,40 +50,43 @@ | ||
```js | ||
import {generateReducer} from 'babel-plugin-transform-pattern-matching/decorators' | ||
import type {ReducerFn} from 'babel-plugin-transform-pattern-matching/decorators' | ||
import { babelPatternMatch } from 'babel-plugin-transform-pattern-matching/expr'; | ||
class Todos { | ||
items: Array<{id: string, title: string}>; | ||
error: string; | ||
} | ||
class Action {} | ||
class TodoAddMultipleAction extends Action {} | ||
class TodoAddMultipleErrorAction extends Action {} | ||
class TodoAddErrorAction extends Action {} | ||
class TodoCompleteErrorAction extends Action {} | ||
class TodoDestroyErrorAction extends Action {} | ||
class TodoReducer { | ||
reducer: ReducerFn<Todos>; | ||
class A {} | ||
class B {} | ||
constructor() { | ||
const _this = this | ||
this.reducer = function _TodoReducer(state, action) { | ||
switch (action.constructor) { | ||
case TodoAddAction: | ||
return _this.add(state, action) | ||
case TodoAddErrorAction: | ||
case TodoRemoveErrorAction: | ||
return _this.error(state, action) | ||
default: | ||
return state | ||
} | ||
type AnyAction = Object & Action; | ||
class ExampleReducer { | ||
reduce(a: A, action: AnyAction, b: B): A { | ||
switch (action.constructor) { | ||
case TodoAddMultipleAction: | ||
return this.addMultiple(a, action, b); | ||
case TodoAddMultipleErrorAction: | ||
case TodoAddErrorAction: | ||
case TodoCompleteErrorAction: | ||
case TodoDestroyErrorAction: | ||
return this.addError(a, action, b); | ||
default: | ||
break; | ||
} | ||
return a; | ||
} | ||
add(todos: Todos, {id, title}: TodoAddAction): Todos { | ||
todos.items.push({id, title}) | ||
return todos | ||
addMultiple(a: A, action: TodoAddMultipleAction, b: B): A { | ||
return a; | ||
} | ||
error(todos: Todos, {error}: TodoAddErrorAction | TodoRemoveErrorAction): Todos { | ||
todos.error = error | ||
return todos | ||
addError(a: A, { error }: TodoAddMultipleErrorAction | TodoAddErrorAction | TodoCompleteErrorAction | TodoDestroyErrorAction, b: B): A { | ||
return a; | ||
} | ||
} | ||
export default new TodoReducer().reducer | ||
``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
388
91
38730
10
21