immer-reducer
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -22,3 +22,3 @@ import { Draft } from "immer"; | ||
}> = ObjectValueTypes<FlattenToReturnTypes<T>>; | ||
/** type constrant for the ImmerReducer class */ | ||
/** type constraint for the ImmerReducer class */ | ||
export interface ImmerReducerClass { | ||
@@ -34,3 +34,3 @@ customName?: string; | ||
} ? V : never; | ||
/** generate reducer function type form the ImmerReducer class */ | ||
/** generate reducer function type from the ImmerReducer class */ | ||
interface ImmerReducerFunction<T extends ImmerReducerClass> { | ||
@@ -37,0 +37,0 @@ (state: ImmerReducerState<T> | undefined, action: ReturnTypeUnion<ActionCreators<T>>): ImmerReducerState<T>; |
@@ -23,3 +23,42 @@ "use strict"; | ||
} | ||
var KNOWN_REDUCER_CLASSES = []; | ||
var DUPLICATE_INCREMENTS = {}; | ||
/** | ||
* Set customName for classes automatically if there is multiple reducers | ||
* classes defined with the same name. This can occur accidentaly when using | ||
* name mangling with minifiers. | ||
* | ||
* @param immerReducerClass | ||
*/ | ||
function setCustomNameForDuplicates(immerReducerClass) { | ||
var hasSetCustomName = KNOWN_REDUCER_CLASSES.find(function (klass) { | ||
return Boolean(klass === immerReducerClass); | ||
}); | ||
if (hasSetCustomName) { | ||
return; | ||
} | ||
var duplicateCustomName = immerReducerClass.customName && | ||
KNOWN_REDUCER_CLASSES.find(function (klass) { | ||
return Boolean(klass.customName && | ||
klass.customName === immerReducerClass.customName); | ||
}); | ||
if (duplicateCustomName) { | ||
throw new Error("There is already customName " + immerReducerClass.customName + " defined for " + duplicateCustomName.name); | ||
} | ||
var duplicate = KNOWN_REDUCER_CLASSES.find(function (klass) { return klass.name === immerReducerClass.name; }); | ||
if (duplicate && !duplicate.customName) { | ||
var number = DUPLICATE_INCREMENTS[immerReducerClass.name]; | ||
if (number) { | ||
number++; | ||
} | ||
else { | ||
number = 1; | ||
} | ||
DUPLICATE_INCREMENTS[immerReducerClass.name] = number; | ||
immerReducerClass.customName = immerReducerClass.name + "_" + number; | ||
} | ||
KNOWN_REDUCER_CLASSES.push(immerReducerClass); | ||
} | ||
function createActionCreators(immerReducerClass) { | ||
setCustomNameForDuplicates(immerReducerClass); | ||
var actionCreators = {}; | ||
@@ -54,26 +93,4 @@ Object.getOwnPropertyNames(immerReducerClass.prototype).forEach(function (key) { | ||
} | ||
var KNOWN_REDUCER_CLASSES = []; | ||
var DUPLICATE_INCREMENTS = {}; | ||
function createReducerFunction(immerReducerClass, initialState) { | ||
var duplicateCustomName = immerReducerClass.customName && | ||
KNOWN_REDUCER_CLASSES.find(function (klass) { | ||
return Boolean(klass.customName && | ||
klass.customName === immerReducerClass.customName); | ||
}); | ||
if (duplicateCustomName) { | ||
throw new Error("There is already customName " + immerReducerClass.customName + " defined for " + duplicateCustomName.name); | ||
} | ||
var duplicate = KNOWN_REDUCER_CLASSES.find(function (klass) { return klass.name === immerReducerClass.name; }); | ||
if (duplicate && !duplicate.customName) { | ||
var number = DUPLICATE_INCREMENTS[immerReducerClass.name]; | ||
if (number) { | ||
number++; | ||
} | ||
else { | ||
number = 1; | ||
} | ||
DUPLICATE_INCREMENTS[immerReducerClass.name] = number; | ||
immerReducerClass.customName = immerReducerClass.name + "_" + number; | ||
} | ||
KNOWN_REDUCER_CLASSES.push(immerReducerClass); | ||
setCustomNameForDuplicates(immerReducerClass); | ||
return function immerReducerFunction(state, action) { | ||
@@ -101,2 +118,9 @@ if (state === undefined) { | ||
}); | ||
// XXX Since Immer 1.10.3 the produce function returns immutable version | ||
// of the object passed to it which is not ok for reducers since | ||
// reducers should always return the same type. So we must cast the | ||
// return value to any to avoid type errors. There is no changes from | ||
// this to the end users because this function return value is | ||
// explicitly typed in the parent function signature. Also there's no | ||
// changes in the runtime behaviour. | ||
}; | ||
@@ -103,0 +127,0 @@ } |
{ | ||
"name": "immer-reducer", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "", | ||
@@ -23,15 +23,14 @@ "main": "lib/immer-reducer.js", | ||
"devDependencies": { | ||
"@types/jest": "^23.3.9", | ||
"@types/react": "^16.4.18", | ||
"@types/react-dom": "^16.0.9", | ||
"@types/react-redux": "^6.0.9", | ||
"dtslint": "^0.3.0", | ||
"@types/jest": "^23.3.12", | ||
"@types/react": "^16.7.18", | ||
"@types/react-dom": "^16.0.11", | ||
"@types/redux": "^3.6.0", | ||
"dtslint": "^0.4.2", | ||
"jest": "^23.6.0", | ||
"prettier": "^1.14.3", | ||
"react": "^16.6.0", | ||
"react-dom": "^16.6.0", | ||
"react-redux": "^5.1.0", | ||
"react-testing-library": "^5.2.3", | ||
"ts-jest": "^23.10.4", | ||
"typescript": "~3.1.6" | ||
"prettier": "^1.15.3", | ||
"react": "^16.7.0", | ||
"react-dom": "^16.7.0", | ||
"react-testing-library": "^5.4.4", | ||
"ts-jest": "^23.10.5", | ||
"typescript": "~3.2.2" | ||
}, | ||
@@ -42,4 +41,4 @@ "peerDependencies": { | ||
"dependencies": { | ||
"immer": "^1.7.4" | ||
"immer": "^1.10.4" | ||
} | ||
} |
@@ -1,5 +0,3 @@ | ||
# Immer Reducer | ||
# immer-reducer | ||
[![Greenkeeper badge](https://badges.greenkeeper.io/epeli/immer-reducer.svg)](https://greenkeeper.io/) | ||
Create Redux reducers using [Immer](https://github.com/mweststrate/immer)! | ||
@@ -24,2 +22,3 @@ | ||
class MyImmerReducer extends ImmerReducer { | ||
// each method becomes an Action Creator | ||
@@ -128,6 +127,5 @@ setFirstName(firstName) { | ||
specifically Typescript usage in mind because I was unable to find any other | ||
libraries that make Redux usage both boilerplate free and 100% type safe. To | ||
be honest it was no easy feat. Pretty advanced Typescript sorcery was | ||
required and so this library requires Typescript 3.1 or later. But the end | ||
results is really simple for the end user. | ||
libraries that make Redux usage both boilerplate free and 100% type safe. | ||
Pretty advanced Typescript sorcery was required and so this library requires | ||
Typescript 3.1 or later. But the end results is really simple for the end user. | ||
@@ -134,0 +132,0 @@ The Typescript usage does not differ that much from the Javascript usage. |
Sorry, the diff of this file is not supported yet
18367
12
7
194
183
Updatedimmer@^1.10.4