redux-optimist
Advanced tools
Comparing version 0.0.2 to 1.0.0
@@ -54,3 +54,3 @@ 'use strict'; | ||
if (!committed) { | ||
console.error('Cannot commit transaction with id "my-transaction" because it does not exist'); | ||
console.error('Cannot commit transaction with id "' + action.optimist.id + '" because it does not exist'); | ||
} | ||
@@ -96,3 +96,3 @@ optimist = newOptimist; | ||
if (!gotInitialState) { | ||
console.error('Cannot revert transaction with id "my-transaction" because it does not exist'); | ||
console.error('Cannot revert transaction with id "' + action.optimist.id + '" because it does not exist'); | ||
} | ||
@@ -121,4 +121,17 @@ optimist = newOptimist; | ||
} | ||
var separated = separateState(state); | ||
return baseReducer(separated.optimist, separated.innerState, action); | ||
var _separateState4 = separateState(state); | ||
var optimist = _separateState4.optimist; | ||
var innerState = _separateState4.innerState; | ||
if (state && !optimist.length) { | ||
var nextState = fn(innerState, action); | ||
if (nextState === innerState) { | ||
return state; | ||
} | ||
validateState(nextState, action); | ||
return _extends({ optimist: optimist }, nextState); | ||
} | ||
return baseReducer(optimist, innerState, action); | ||
}; | ||
@@ -125,0 +138,0 @@ } |
{ | ||
"name": "redux-optimist", | ||
"version": "0.0.2", | ||
"version": "1.0.0", | ||
"description": "Optimistically apply actions that can be later commited or reverted.", | ||
@@ -5,0 +5,0 @@ "keywords": [], |
@@ -6,5 +6,9 @@ # redux-optimist | ||
[![Build Status](https://img.shields.io/travis/ForbesLindesay/redux-optimist/master.svg)](https://travis-ci.org/ForbesLindesay/redux-optimist) | ||
[![Dependency Status](https://img.shields.io/gemnasium/ForbesLindesay/redux-optimist.svg)](https://gemnasium.com/ForbesLindesay/redux-optimist) | ||
[![Dependency Status](https://img.shields.io/david/ForbesLindesay/redux-optimist.svg)](https://david-dm.org/ForbesLindesay/redux-optimist) | ||
[![NPM version](https://img.shields.io/npm/v/redux-optimist.svg)](https://www.npmjs.org/package/redux-optimist) | ||
<a target='_blank' rel='nofollow' href='https://app.codesponsor.io/link/gg9sZwctSLxyov1sJwW6pfyS/ForbesLindesay/redux-optimist'> | ||
<img alt='Sponsor' width='888' height='68' src='https://app.codesponsor.io/embed/gg9sZwctSLxyov1sJwW6pfyS/ForbesLindesay/redux-optimist.svg' /> | ||
</a> | ||
## Installation | ||
@@ -70,3 +74,3 @@ | ||
```js | ||
import {BEGIN, COMMIT, REVERT} from 'optimist'; | ||
import {BEGIN, COMMIT, REVERT} from 'redux-optimist'; | ||
import request from 'then-request'; | ||
@@ -118,3 +122,4 @@ | ||
let store = applyMiddleware(api)(createStore)(reducer); | ||
// Note: passing middleware as the last argument to createStore requires redux@>=3.1.0 | ||
let store = createStore(reducer, applyMiddleware(api)); | ||
console.log(store.getState()); | ||
@@ -121,0 +126,0 @@ // { |
@@ -267,2 +267,13 @@ 'use strict'; | ||
test('omits optimist from original reducer', () => { | ||
function originalReducer(state = {value: 0}, action) { | ||
assert(state.value === 0); | ||
assert(!state.hasOwnProperty('optimist')); | ||
return state; | ||
} | ||
let reducer = optimist(originalReducer); | ||
let state; | ||
state = reducer(state, {type: 'foo'}); | ||
state = reducer(state, {type: 'foo'}); | ||
}); | ||
@@ -391,3 +402,24 @@ test('real world example', () => { | ||
test('calls original reducer max of one time per action', () => { | ||
let calls = 0; | ||
function originalReducer(state) { | ||
calls++; | ||
return {}; | ||
} | ||
let reducer = optimist(originalReducer); | ||
let state; | ||
state = reducer(state, {type: '@@init'}); | ||
state = reducer(state, {type: 'foo'}); | ||
assert.equal(calls, 2); | ||
}); | ||
test('unhandled action state reference', () => { | ||
let originalReducer = (state = {}) => state; | ||
let reducer = optimist(originalReducer); | ||
let initState = reducer(undefined, {type: '@@init'}); | ||
let originalState = reducer(initState, {type: 'foo'}); | ||
let nextState = reducer(originalState, {type: 'foo'}); | ||
assert.strictEqual(originalState, nextState); | ||
}); | ||
function basic(name, {reducer, before, action, after}) { | ||
@@ -394,0 +426,0 @@ test(name, () => { |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
1
162
0
26033
8
569