@wordpress/redux-routine
Advanced tools
Comparing version 2.0.0 to 3.0.0
@@ -5,3 +5,3 @@ /** | ||
import isGenerator from './is-generator'; | ||
import castError from './cast-error'; | ||
import createRuntime from './runtime'; | ||
/** | ||
@@ -17,3 +17,3 @@ * Creates a Redux middleware, given an object of controls where each key is an | ||
* | ||
* @return {Function} Redux middleware function. | ||
* @return {Function} Co-routine runtime | ||
*/ | ||
@@ -24,2 +24,3 @@ | ||
return function (store) { | ||
var runtime = createRuntime(controls, store.dispatch); | ||
return function (next) { | ||
@@ -31,31 +32,3 @@ return function (action) { | ||
function step(nextAction) { | ||
if (!nextAction) { | ||
return; | ||
} | ||
var control = controls[nextAction.type]; | ||
if (typeof control === 'function') { | ||
var routine = control(nextAction); | ||
if (routine instanceof Promise) { | ||
// Async control routine awaits resolution. | ||
routine.then(function (result) { | ||
return step(action.next(result).value); | ||
}, function (error) { | ||
return action.throw(castError(error)); | ||
}); | ||
} else if (routine !== undefined) { | ||
// Sync control routine steps synchronously. | ||
step(action.next(routine).value); | ||
} | ||
} else { | ||
// Uncontrolled action is dispatched. | ||
store.dispatch(nextAction); | ||
step(action.next().value); | ||
} | ||
} | ||
step(action.next().value); | ||
return runtime(action); | ||
}; | ||
@@ -62,0 +35,0 @@ }; |
@@ -12,3 +12,3 @@ "use strict"; | ||
var _castError = _interopRequireDefault(require("./cast-error")); | ||
var _runtime = _interopRequireDefault(require("./runtime")); | ||
@@ -29,3 +29,3 @@ /** | ||
* | ||
* @return {Function} Redux middleware function. | ||
* @return {Function} Co-routine runtime | ||
*/ | ||
@@ -35,2 +35,3 @@ function createMiddleware() { | ||
return function (store) { | ||
var runtime = (0, _runtime.default)(controls, store.dispatch); | ||
return function (next) { | ||
@@ -42,31 +43,3 @@ return function (action) { | ||
function step(nextAction) { | ||
if (!nextAction) { | ||
return; | ||
} | ||
var control = controls[nextAction.type]; | ||
if (typeof control === 'function') { | ||
var routine = control(nextAction); | ||
if (routine instanceof Promise) { | ||
// Async control routine awaits resolution. | ||
routine.then(function (result) { | ||
return step(action.next(result).value); | ||
}, function (error) { | ||
return action.throw((0, _castError.default)(error)); | ||
}); | ||
} else if (routine !== undefined) { | ||
// Sync control routine steps synchronously. | ||
step(action.next(routine).value); | ||
} | ||
} else { | ||
// Uncontrolled action is dispatched. | ||
store.dispatch(nextAction); | ||
step(action.next().value); | ||
} | ||
} | ||
step(action.next().value); | ||
return runtime(action); | ||
}; | ||
@@ -73,0 +46,0 @@ }; |
@@ -1,5 +0,12 @@ | ||
## 2.0.0 (Unreleased) | ||
## 3.0.0 (Unreleased) | ||
### Breaking change | ||
- The middleware returns a promise resolving once the runtime finishes iterating over the generator. | ||
- It's not possible to kill the execution of the runtime anymore by returning `undefined` | ||
## 2.0.0 (2018-09-05) | ||
### Breaking Change | ||
- Change how required built-ins are polyfilled with Babel 7 ([#9171](https://github.com/WordPress/gutenberg/pull/9171)). If you're using an environment that has limited or no support for ES2015+ such as lower versions of IE then using [core-js](https://github.com/zloirock/core-js) or [@babel/polyfill](https://babeljs.io/docs/en/next/babel-polyfill) will add support for these methods. |
{ | ||
"name": "@wordpress/redux-routine", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Redux middleware for generator coroutines.", | ||
@@ -25,3 +25,4 @@ "author": "The WordPress Contributors", | ||
"dependencies": { | ||
"@babel/runtime": "^7.0.0" | ||
"@babel/runtime": "^7.0.0", | ||
"rungen": "^0.3.2" | ||
}, | ||
@@ -34,3 +35,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "df6f8da7b557894e4364c17db2dd4af0d1e20252" | ||
"gitHead": "7b17d5777076896fb25170b23d6e83e8c049240d" | ||
} |
@@ -25,5 +25,5 @@ # @wordpress/redux-routine | ||
import { combineReducers, createStore, applyMiddleware } from 'redux'; | ||
import createRoutineMiddleware from '@wordpress/redux-routine'; | ||
import createMiddleware from '@wordpress/redux-routine'; | ||
const middleware = createRoutineMiddleware( { | ||
const middleware = createMiddleware( { | ||
async FETCH_JSON( action ) { | ||
@@ -30,0 +30,0 @@ const response = await window.fetch( action.url ); |
@@ -5,3 +5,3 @@ /** | ||
import isGenerator from './is-generator'; | ||
import castError from './cast-error'; | ||
import createRuntime from './runtime'; | ||
@@ -18,38 +18,15 @@ /** | ||
* | ||
* @return {Function} Redux middleware function. | ||
* @return {Function} Co-routine runtime | ||
*/ | ||
export default function createMiddleware( controls = {} ) { | ||
return ( store ) => ( next ) => ( action ) => { | ||
if ( ! isGenerator( action ) ) { | ||
return next( action ); | ||
} | ||
function step( nextAction ) { | ||
if ( ! nextAction ) { | ||
return; | ||
return ( store ) => { | ||
const runtime = createRuntime( controls, store.dispatch ); | ||
return ( next ) => ( action ) => { | ||
if ( ! isGenerator( action ) ) { | ||
return next( action ); | ||
} | ||
const control = controls[ nextAction.type ]; | ||
if ( typeof control === 'function' ) { | ||
const routine = control( nextAction ); | ||
if ( routine instanceof Promise ) { | ||
// Async control routine awaits resolution. | ||
routine.then( | ||
( result ) => step( action.next( result ).value ), | ||
( error ) => action.throw( castError( error ) ), | ||
); | ||
} else if ( routine !== undefined ) { | ||
// Sync control routine steps synchronously. | ||
step( action.next( routine ).value ); | ||
} | ||
} else { | ||
// Uncontrolled action is dispatched. | ||
store.dispatch( nextAction ); | ||
step( action.next().value ); | ||
} | ||
} | ||
step( action.next().value ); | ||
return runtime( action ); | ||
}; | ||
}; | ||
} |
@@ -38,5 +38,5 @@ /** | ||
it( 'should continue only once control condition resolves', ( done ) => { | ||
it( 'should continue only once control condition resolves', async () => { | ||
const middleware = createMiddleware( { | ||
WAIT: () => new Promise( ( resolve ) => setTimeout( resolve, 0 ) ), | ||
WAIT: () => new Promise( ( resolve ) => resolve() ), | ||
} ); | ||
@@ -49,19 +49,11 @@ const store = createStoreWithMiddleware( middleware ); | ||
store.dispatch( createAction() ); | ||
expect( store.getState() ).toBe( null ); | ||
jest.runAllTimers(); | ||
// Promise resolution occurs on next tick. | ||
process.nextTick( () => { | ||
expect( store.getState() ).toBe( 1 ); | ||
done(); | ||
} ); | ||
await store.dispatch( createAction() ); | ||
expect( store.getState() ).toBe( 1 ); | ||
} ); | ||
it( 'should throw if promise rejects', ( done ) => { | ||
it( 'should throw if promise rejects', async () => { | ||
const middleware = createMiddleware( { | ||
WAIT_FAIL: () => new Promise( ( resolve, reject ) => { | ||
setTimeout( () => reject( 'Message' ), 0 ); | ||
} ), | ||
WAIT_FAIL: () => new Promise( ( resolve, reject ) => | ||
reject( 'Message' ) | ||
), | ||
} ); | ||
@@ -74,12 +66,9 @@ const store = createStoreWithMiddleware( middleware ); | ||
expect( error.message ).toBe( 'Message' ); | ||
done(); | ||
} | ||
} | ||
store.dispatch( createAction() ); | ||
jest.runAllTimers(); | ||
await store.dispatch( createAction() ); | ||
} ); | ||
it( 'should throw if promise throws', ( done ) => { | ||
it( 'should throw if promise throws', () => { | ||
const middleware = createMiddleware( { | ||
@@ -96,9 +85,6 @@ WAIT_FAIL: () => new Promise( () => { | ||
expect( error.message ).toBe( 'Message' ); | ||
done(); | ||
} | ||
} | ||
store.dispatch( createAction() ); | ||
jest.runAllTimers(); | ||
return store.dispatch( createAction() ); | ||
} ); | ||
@@ -121,8 +107,6 @@ | ||
it( 'assigns async controlled return value into yield assignment', ( done ) => { | ||
it( 'assigns async controlled return value into yield assignment', async () => { | ||
const middleware = createMiddleware( { | ||
WAIT: ( action ) => new Promise( ( resolve ) => { | ||
setTimeout( () => { | ||
resolve( action.value ); | ||
}, 0 ); | ||
resolve( action.value ); | ||
} ), | ||
@@ -136,27 +120,6 @@ } ); | ||
store.dispatch( createAction() ); | ||
expect( store.getState() ).toBe( null ); | ||
await store.dispatch( createAction() ); | ||
jest.runAllTimers(); | ||
process.nextTick( () => { | ||
expect( store.getState() ).toBe( 2 ); | ||
done(); | ||
} ); | ||
expect( store.getState() ).toBe( 2 ); | ||
} ); | ||
it( 'kills continuation if control returns undefined', () => { | ||
const middleware = createMiddleware( { | ||
KILL: () => {}, | ||
} ); | ||
const store = createStoreWithMiddleware( middleware ); | ||
function* createAction() { | ||
yield { type: 'KILL' }; | ||
return { type: 'CHANGE', nextState: 1 }; | ||
} | ||
store.dispatch( createAction() ); | ||
expect( store.getState() ).toBe( null ); | ||
} ); | ||
} ); |
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
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
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
53485
27
504
2
+ Addedrungen@^0.3.2
+ Addedrungen@0.3.2(transitive)