Skip to content

Commit

Permalink
Merge pull request #90 from WordPress/fix/action-undef
Browse files Browse the repository at this point in the history
Hooks: Fix undefined first argument on consecutive action callbacks
  • Loading branch information
aduth authored Mar 21, 2018
2 parents 95dc85c + ade460f commit 6be1af4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/hooks/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**1.1.5 (2018-03-21)**

- Fix: Resolves issue where action argument would be undefined on all but the first action callback.
7 changes: 6 additions & 1 deletion packages/hooks/src/createRunHook.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,12 @@ function createRunHook( hooks, returnFirstArg ) {

while ( hookInfo.currentIndex < handlers.length ) {
const handler = handlers[ hookInfo.currentIndex ];
args[ 0 ] = handler.callback.apply( null, args );

const result = handler.callback.apply( null, args );
if ( returnFirstArg ) {
args[ 0 ] = result;
}

hookInfo.currentIndex++;
}

Expand Down
26 changes: 26 additions & 0 deletions packages/hooks/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,32 @@ test( 'adding and removing filters with recursion', () => {
expect( applyFilters( 'remove_and_add', '' ) ).toBe( '1-134-234' );
} );

test( 'actions preserve arguments across handlers without return value', () => {
const arg1 = { a: 10 };
const arg2 = { b: 20 };

addAction( 'test.action', 'my_callback1', ( a, b ) => {
expect( a ).toBe( arg1 );
expect( b ).toBe( arg2 );
} );

addAction( 'test.action', 'my_callback2', ( a, b ) => {
expect( a ).toBe( arg1 );
expect( b ).toBe( arg2 );
} );

doAction( 'test.action', arg1, arg2 );
} );

test( 'filters pass first argument across handlers', () => {
addFilter( 'test.filter', 'my_callback1', ( count ) => count + 1 );
addFilter( 'test.filter', 'my_callback2', ( count ) => count + 1 );

const result = applyFilters( 'test.filter', 0 );

expect( result ).toBe( 2 );
} );

// Test adding via composition.
test( 'adding hooks via composition', () => {
const testObject = {};
Expand Down

0 comments on commit 6be1af4

Please sign in to comment.