Skip to content

Commit

Permalink
Data: Change resolver signature to fulfill as assigned value
Browse files Browse the repository at this point in the history
Later refactoring to introduce new options still possible by overloading / normalizing value
  • Loading branch information
aduth committed Mar 12, 2018
1 parent ed622c1 commit 7829f7b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 28 deletions.
18 changes: 9 additions & 9 deletions core-data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ const MODULE_KEY = 'core';

const store = registerStore( MODULE_KEY, {
reducer,
selectors: { getCategories },
selectors: {
getCategories,
},
resolvers: {
getCategories: {
fulfill() {
wp.apiRequest( { path: '/wp/v2/categories' } ).then( categories => {
store.dispatch( {
type: 'RECEIVE_CATEGORIES',
categories,
} );
getCategories() {
wp.apiRequest( { path: '/wp/v2/categories' } ).then( ( categories ) => {
store.dispatch( {
type: 'RECEIVE_CATEGORIES',
categories,
} );
},
} );
},
},
} );
Expand Down
7 changes: 3 additions & 4 deletions data/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import isEqualShallow from 'is-equal-shallow';
import { createStore } from 'redux';
import { flowRight, without, mapValues, get } from 'lodash';
import { flowRight, without, mapValues } from 'lodash';
import memize from 'memize';

/**
Expand Down Expand Up @@ -119,13 +119,12 @@ export function registerSelectors( reducerKey, newSelectors ) {
export function registerResolvers( reducerKey, newResolvers ) {
const createResolver = ( selector, key ) => {
// Don't modify selector behavior if no resolver exists.
let fulfill = get( newResolvers, [ key, 'fulfill' ] );
if ( typeof fulfill !== 'function' ) {
if ( ! newResolvers.hasOwnProperty( key ) ) {
return selector;
}

// Ensure single invocation per argument set via memoization.
fulfill = memize( fulfill );
const fulfill = memize( newResolvers[ key ] );

return ( ...args ) => {
fulfill( ...args );
Expand Down
16 changes: 1 addition & 15 deletions data/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,18 +88,6 @@ describe( 'registerResolvers', () => {
expect( select( 'demo' ).getValue() ).toBe( 'OK' );
} );

it( 'should not do anything if registered without fulfill', () => {
registerReducer( 'demo', ( state = 'OK' ) => state );
registerSelectors( 'demo', {
getValue: ( state ) => state,
} );
registerResolvers( 'demo', {
getValue: {},
} );

expect( select( 'demo' ).getValue() ).toBe( 'OK' );
} );

it( 'should behave as a side effect for the given selector, with arguments', () => {
const resolver = jest.fn();

Expand All @@ -108,9 +96,7 @@ describe( 'registerResolvers', () => {
getValue: ( state ) => state,
} );
registerResolvers( 'demo', {
getValue: {
fulfill: resolver,
},
getValue: resolver,
} );

const value = select( 'demo' ).getValue( 'arg1', 'arg2' );
Expand Down

0 comments on commit 7829f7b

Please sign in to comment.