immer-reducer
Advanced tools
Comparing version 0.1.5 to 0.2.0
@@ -37,8 +37,13 @@ import { Draft } from "immer"; | ||
} | ||
/** ActionCreator function interface with actual action type name */ | ||
interface ActionCreator<ActionTypeType, Payload extends any[]> { | ||
readonly type: ActionTypeType; | ||
(...args: Payload): { | ||
type: ActionTypeType; | ||
payload: Payload; | ||
}; | ||
} | ||
/** generate ActionCreators types from the ImmerReducer class */ | ||
export declare type ActionCreators<ClassActions extends ImmerReducerClass> = { | ||
[K in keyof Methods<InstanceType<ClassActions>>]: (...args: ArgumentsType<InstanceType<ClassActions>[K]>) => { | ||
type: K; | ||
payload: ArgumentsType<InstanceType<ClassActions>[K]>; | ||
}; | ||
[K in keyof Methods<InstanceType<ClassActions>>]: ActionCreator<K, ArgumentsType<InstanceType<ClassActions>[K]>>; | ||
}; | ||
@@ -45,0 +50,0 @@ /** The actual ImmerReducer class */ |
@@ -33,3 +33,4 @@ "use strict"; | ||
} | ||
actionCreators[key] = function () { | ||
var type = PREFIX + ":" + getReducerName(immerReducerClass) + "#" + key; | ||
var actionCreator = function () { | ||
var args = []; | ||
@@ -40,6 +41,8 @@ for (var _i = 0; _i < arguments.length; _i++) { | ||
return { | ||
type: PREFIX + ":" + getReducerName(immerReducerClass) + "#" + key, | ||
type: type, | ||
payload: args, | ||
}; | ||
}; | ||
actionCreator.type = type; | ||
actionCreators[key] = actionCreator; | ||
}); | ||
@@ -52,7 +55,7 @@ return actionCreators; // typed in the function signature | ||
} | ||
var KNOWN_REDUCES_CLASSES = []; | ||
var KNOWN_REDUCER_CLASSES = []; | ||
var DUPLICATE_INCREMENTS = {}; | ||
function createReducerFunction(immerReducerClass, initialState) { | ||
var duplicateCustomName = immerReducerClass.customName && | ||
KNOWN_REDUCES_CLASSES.find(function (klass) { | ||
KNOWN_REDUCER_CLASSES.find(function (klass) { | ||
return Boolean(klass.customName && | ||
@@ -64,3 +67,3 @@ klass.customName === immerReducerClass.customName); | ||
} | ||
var duplicate = KNOWN_REDUCES_CLASSES.find(function (klass) { return klass.name === immerReducerClass.name; }); | ||
var duplicate = KNOWN_REDUCER_CLASSES.find(function (klass) { return klass.name === immerReducerClass.name; }); | ||
if (duplicate && !duplicate.customName) { | ||
@@ -77,3 +80,3 @@ var number = DUPLICATE_INCREMENTS[immerReducerClass.name]; | ||
} | ||
KNOWN_REDUCES_CLASSES.push(immerReducerClass); | ||
KNOWN_REDUCER_CLASSES.push(immerReducerClass); | ||
return function immerReducerFunction(state, action) { | ||
@@ -108,5 +111,5 @@ if (state === undefined) { | ||
function _clearKnownClasses() { | ||
KNOWN_REDUCES_CLASSES = []; | ||
KNOWN_REDUCER_CLASSES = []; | ||
} | ||
exports._clearKnownClasses = _clearKnownClasses; | ||
//# sourceMappingURL=immer-reducer.js.map |
{ | ||
"name": "immer-reducer", | ||
"version": "0.1.5", | ||
"version": "0.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "lib/immer-reducer.js", |
@@ -38,3 +38,3 @@ # Immer Reducer | ||
this.setFirstName(firstName); | ||
this.setLastName(firstName); | ||
this.setLastName(lastName); | ||
} | ||
@@ -76,3 +76,3 @@ } | ||
Under the hood the class is desconstructed to following actions: | ||
Under the hood the class is deconstructed to following actions: | ||
@@ -92,6 +92,6 @@ ```js | ||
become the action payloads. The reducer function will then match these | ||
actions against the class and calls the approciate methods with the payload | ||
array spread to the arguments. But do note that the action format is not part of | ||
the public API so don't write any code relying on it. The actions are handled | ||
by the generated reducer function. | ||
actions against the class and calls the appropriate methods with the payload | ||
array spread to the arguments. But do note that the action format is not part | ||
of the public API so don't write any code relying on it. The actions are | ||
handled by the generated reducer function. | ||
@@ -101,2 +101,27 @@ The generated reducer function executes the methods inside the `produce()` | ||
# Integrating with the Redux ecosystem | ||
To integrate for example with the side effects libraries such as | ||
[redux-observable](https://github.com/redux-observable/redux-observable/) and | ||
[redux-saga](https://github.com/redux-saga/redux-saga), you can access the | ||
generated action type using the `type` property of the action creator | ||
function: | ||
```ts | ||
// Get the action name to subscribe to | ||
const setFirstNameActionTypeName = ActionCreators.setFirstName.type; | ||
// Get the action type to have a type safe Epic | ||
type SetFirstNameAction = ReturnType<typeof ActionCreators.setFirstName>; | ||
const setFirstNameEpic: Epic<SetFirstNameAction> = action$ => | ||
action$ | ||
.ofType(setFirstNameActionTypeName) | ||
.pipe( | ||
// action.payload - recognized as string | ||
map(action => action.payload.toUpperCase()), | ||
... | ||
); | ||
``` | ||
# 100% Type Safety with Typescript | ||
@@ -103,0 +128,0 @@ |
Sorry, the diff of this file is not supported yet
17037
170
185