Skip to content

Commit

Permalink
Data: Return result of middleware chain in resolvers cache middleware (
Browse files Browse the repository at this point in the history
…#14711)

* Data: Return result of middleware chain in resolvers cache middleware

* Data: Avoid returning result of store.dispatch in public interface
  • Loading branch information
aduth committed Apr 4, 2019
1 parent a6ff3a0 commit 1f8f7d7
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
5 changes: 4 additions & 1 deletion packages/data/src/namespace-store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ function mapSelectors( selectors, store, registry ) {
* @return {Object} Actions mapped to the redux store provided.
*/
function mapActions( actions, store ) {
const createBoundAction = ( action ) => ( ...args ) => store.dispatch( action( ...args ) );
const createBoundAction = ( action ) => ( ...args ) => {
store.dispatch( action( ...args ) );
};

return mapValues( actions, createBoundAction );
}

Expand Down
41 changes: 41 additions & 0 deletions packages/data/src/namespace-store/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,45 @@ describe( 'controls', () => {
expect( action1 ).toBeCalled();
} );
} );

it( 'resolves in expected order', ( done ) => {
const actions = {
wait: () => ( { type: 'WAIT' } ),
receive: ( items ) => ( { type: 'RECEIVE', items } ),
};

registry.registerStore( 'store', {
reducer: ( state = null, action ) => {
if ( action.type === 'RECEIVE' ) {
return action.items;
}

return state;
},
selectors: {
getItems: ( state ) => state,
},
resolvers: {
* getItems() {
yield actions.wait();
yield actions.receive( [ 1, 2, 3 ] );
},
},
controls: {
WAIT() {
return new Promise( ( resolve ) => process.nextTick( resolve ) );
},
},
} );

registry.subscribe( () => {
const isFinished = registry.select( 'store' ).hasFinishedResolution( 'getItems' );
if ( isFinished ) {
expect( registry.select( 'store' ).getItems() ).toEqual( [ 1, 2, 3 ] );
done();
}
} );

registry.select( 'store' ).getItems();
} );
} );
2 changes: 1 addition & 1 deletion packages/data/src/resolvers-cache-middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const createResolversCacheMiddleware = ( registry, reducerKey ) => () => ( next
registry.dispatch( 'core/data' ).invalidateResolution( reducerKey, selectorName, args );
} );
} );
next( action );
return next( action );
};

export default createResolversCacheMiddleware;
3 changes: 2 additions & 1 deletion packages/data/src/test/registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -555,7 +555,8 @@ describe( 'createRegistry', () => {
},
} );

registry.dispatch( 'counter' ).increment(); // state = 1
const dispatchResult = registry.dispatch( 'counter' ).increment(); // state = 1
expect( dispatchResult ).toBe( undefined ); // Actions are implementation detail.
registry.dispatch( 'counter' ).increment( 4 ); // state = 5
expect( store.getState() ).toBe( 5 );
} );
Expand Down

0 comments on commit 1f8f7d7

Please sign in to comment.