redux-interactions
Advanced tools
Comparing version 1.5.1 to 2.0.0
@@ -14,2 +14,8 @@ import * as types from './types'; | ||
}; | ||
_instanceInteractionReducers: { | ||
[key: string]: types.Reducer; | ||
}; | ||
_actionTypes: { | ||
[key: string]: string; | ||
}; | ||
constructor(); | ||
@@ -16,0 +22,0 @@ /** |
@@ -13,2 +13,8 @@ "use strict"; | ||
this.initialState = Object.create(null); | ||
// This instance's reducers map, separate from _interactionReducer | ||
// so it can have a different type (or key in this map) than a base | ||
// class's reducer | ||
this._instanceInteractionReducers = Object.create(null); | ||
// Maintains a mapping from @reducer function name to unique action type | ||
this._actionTypes = Object.create(null); | ||
// Register the class as a property of the instance so it is "exported" | ||
@@ -28,2 +34,11 @@ // under normal use. | ||
this.reducer = _bind(this, this.reducer); | ||
// Add the class name to the action type, which we now know because it's | ||
// getting instantiated (vs a base class name) | ||
for (var key in this._interactionReducers) { | ||
// Make globally unique | ||
var type = this._actionTypes[key] = uniqueType(this.constructor.name + ":" + key); | ||
// Define a constant containing the complete action type as ALL_CAPS. | ||
this[_.snakeCase(key).toUpperCase()] = type; | ||
this._instanceInteractionReducers[type] = this._interactionReducers[key]; | ||
} | ||
} | ||
@@ -35,5 +50,5 @@ /** | ||
state = state === undefined ? this.initialState : state; | ||
if (!this._interactionReducers) | ||
if (!this._instanceInteractionReducers) | ||
return state; | ||
var interactionReducer = this._interactionReducers[action.type]; | ||
var interactionReducer = this._instanceInteractionReducers[action.type]; | ||
if (!interactionReducer) | ||
@@ -64,7 +79,3 @@ return state; | ||
Interactions._registerInteractionReducer = function (name, reducer, type) { | ||
if (!type) { | ||
type = uniqueType(this.prototype.constructor.name + ":" + name); | ||
} | ||
// Define a constant containing the complete action type as ALL_CAPS. | ||
this.prototype[_.snakeCase(name).toUpperCase()] = type; | ||
if (type === void 0) { type = name; } | ||
// Allow inheritance of interaction reducers. | ||
@@ -81,3 +92,3 @@ if (!Object.getOwnPropertyDescriptor(this.prototype, '_interactionReducers')) { | ||
} | ||
return { type: type, args: args }; | ||
return { type: this._actionTypes[type], args: args }; | ||
}; | ||
@@ -84,0 +95,0 @@ }; |
@@ -88,6 +88,45 @@ "use strict"; | ||
var add = instance.add; | ||
expect(add().type).to.eql('FOO_BAR'); | ||
expect(add().type).to.eql('Counter:FOO_BAR'); | ||
expect(instance.reducer(1, add())).to.eql(2); | ||
expect(instance.reducer(1, add(5))).to.eql(6); | ||
}); | ||
it("Enables inheritance of interactions", function () { | ||
var Counter = (function (_super) { | ||
__extends(Counter, _super); | ||
function Counter() { | ||
_super.apply(this, arguments); | ||
} | ||
Counter.prototype.add = function (scopedState, amount) { | ||
if (amount === void 0) { amount = 1; } | ||
return scopedState + amount; | ||
}; | ||
__decorate([ | ||
src_1.reducer | ||
], Counter.prototype, "add", null); | ||
return Counter; | ||
}(src_1.Interactions)); | ||
var SpecificCounter = (function (_super) { | ||
__extends(SpecificCounter, _super); | ||
function SpecificCounter() { | ||
_super.apply(this, arguments); | ||
} | ||
SpecificCounter.prototype.subtract = function (scopedState, amount) { | ||
if (amount === void 0) { amount = 1; } | ||
return scopedState - amount; | ||
}; | ||
__decorate([ | ||
src_1.reducer | ||
], SpecificCounter.prototype, "subtract", null); | ||
return SpecificCounter; | ||
}(Counter)); | ||
var instance = new SpecificCounter; | ||
var add = instance.add.bind(instance); | ||
var subtract = instance.subtract.bind(instance); | ||
expect(add().type).to.eql(instance.ADD); | ||
expect(subtract().type).to.eql(instance.SUBTRACT); | ||
expect(instance.reducer(1, add())).to.eql(2); | ||
expect(instance.reducer(1, add(5))).to.eql(6); | ||
expect(instance.reducer(1, subtract())).to.eql(0); | ||
expect(instance.reducer(1, subtract(5))).to.eql(-4); | ||
}); | ||
}); |
{ | ||
"name": "redux-interactions", | ||
"version": "1.5.1", | ||
"version": "2.0.0", | ||
"description": "A streamlined approach to managing your Redux action creators and reducers.", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/convoyinc/redux-interactions", |
@@ -265,2 +265,3 @@ # redux-interactions | ||
```js | ||
import { combineInteractions } from 'redux-interactions'; | ||
import * as interactions from './interactions'; | ||
@@ -267,0 +268,0 @@ |
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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
46697
937
282
1