Skip to content

Commit

Permalink
Merge pull request #52 from WordPress/feature/events-on-add-remove
Browse files Browse the repository at this point in the history
Feature/events on add remove
  • Loading branch information
gziolo authored Dec 21, 2017
2 parents c58b48a + cceb16f commit cce12c7
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 2 deletions.
11 changes: 9 additions & 2 deletions packages/hooks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ A lightweight & efficient filter and action manager.
* `addAction( 'hookName', 'functionName', callback, priority )`
* `addFilter( 'hookName', 'functionName', callback, priority )`
* `removeAction( 'hookName', 'functionName' )`
* `removeFilter( 'hookName', 'functionName' )`
* `removeFilter( 'hookName', 'functionName' )`
* `removeAllActions( 'hookName' )`
* `removeAllFilters( 'hookName' )`
* `doAction( 'hookName', arg1, arg2, moreArgs, finalArg )`
Expand All @@ -41,4 +41,11 @@ Hooks can be added to an object via composition:

`myObject.hooks = createHooks();`

API functions are then be called: `myObject.hooks.addAction()`...
API functions are then be called: `myObject.hooks.addAction()`.

### Events on action/filter add or remove

Whenever an action or filter is added or removed, a matching `hookAdded` or `hookRemoved` action is triggered.

* `hookAdded` action is triggered when `addFilter()` or `addAction()` method is called, passing values for `hookName`, `functionName`, `callback` and `priority`.
* `hookRemoved` action is triggered when `removeFilter()` or `removeAction()` method is called, passing values for `hookName` and `functionName`.
5 changes: 5 additions & 0 deletions packages/hooks/src/createAddHook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import validateNamespace from './validateNamespace.js';
import validateHookName from './validateHookName.js';
import { doAction } from './';

/**
* Returns a function which, when invoked, will add a hook.
Expand Down Expand Up @@ -68,6 +69,10 @@ function createAddHook( hooks ) {
runs: 0,
};
}

if ( hookName !== 'hookAdded' ) {
doAction( 'hookAdded', hookName, namespace, callback, priority );
}
};
}

Expand Down
4 changes: 4 additions & 0 deletions packages/hooks/src/createRemoveHook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import validateNamespace from './validateNamespace.js';
import validateHookName from './validateHookName.js';
import { doAction } from './';

/**
* Returns a function which, when invoked, will remove a specified hook or all
Expand Down Expand Up @@ -65,6 +66,9 @@ function createRemoveHook( hooks, removeAll ) {
}
}
}
if ( hookName !== 'hookRemoved' ) {
doAction( 'hookRemoved', hookName, namespace );
}

return handlersRemoved;
};
Expand Down
63 changes: 63 additions & 0 deletions packages/hooks/src/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,66 @@ test( 'Test `this` context via composition', () => {
const testObject2 = {};
Object.assign( testObject2, createHooks() );
} );

const setupActionListener = ( hookName, callback ) =>
addAction( hookName, 'my_callback', callback );

test( 'adding an action triggers a hookAdded action passing all callback details', () => {
const hook_added_spy = jest.fn();

setupActionListener( 'hookAdded', hook_added_spy );

addAction( 'testAction', 'my_callback2', action_a, 9 );
expect( hook_added_spy ).toHaveBeenCalledTimes( 1 );
expect( hook_added_spy ).toHaveBeenCalledWith(
'testAction',
'my_callback2',
action_a,
9
);
} );

test( 'adding a filter triggers a hookAdded action passing all callback details', () => {
const hook_added_spy = jest.fn();

setupActionListener( 'hookAdded', hook_added_spy );

addFilter( 'testFilter', 'my_callback3', filter_a, 8 );
expect( hook_added_spy ).toHaveBeenCalledTimes( 1 );
expect( hook_added_spy ).toHaveBeenCalledWith(
'testFilter',
'my_callback3',
filter_a,
8
);
} );

test( 'removing an action triggers a hookRemoved action passing all callback details', () => {
const hook_removed_spy = jest.fn();

setupActionListener( 'hookRemoved', hook_removed_spy );

addAction( 'testAction', 'my_callback2', action_a, 9 );
removeAction( 'testAction', 'my_callback2' );

expect( hook_removed_spy ).toHaveBeenCalledTimes( 1 );
expect( hook_removed_spy ).toHaveBeenCalledWith(
'testAction',
'my_callback2'
);
} );

test( 'removing a filter triggers a hookRemoved action passing all callback details', () => {
const hook_removed_spy = jest.fn();

setupActionListener( 'hookRemoved', hook_removed_spy );

addFilter( 'testFilter', 'my_callback3', filter_a, 8 );
removeFilter( 'testFilter', 'my_callback3' );

expect( hook_removed_spy ).toHaveBeenCalledTimes( 1 );
expect( hook_removed_spy ).toHaveBeenCalledWith(
'testFilter',
'my_callback3'
);
} );

0 comments on commit cce12c7

Please sign in to comment.