Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

components: Add mergeEventHandlers #33205

Merged
merged 4 commits into from
Jul 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions packages/components/src/utils/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
type EventHandler< T extends Event > = ( event: T ) => void;

/**
* Merges event handlers together.
*
* @template TEvent
* @param handler
* @param otherHandler
*/
function mergeEvent< TEvent extends Event >(
handler: EventHandler< TEvent >,
otherHandler: EventHandler< TEvent >
): EventHandler< TEvent > {
return ( event: TEvent ) => {
if ( typeof handler === 'function' ) {
handler( event );
}
if ( typeof otherHandler === 'function' ) {
otherHandler( event );
}
};
}

/**
* Merges a set of event handlers together.
sarayourfriend marked this conversation as resolved.
Show resolved Hide resolved
*
* @template TEvent
* @param handlers
* @param extraHandlers
*/
export function mergeEventHandlers<
TEvent extends Event,
TLeft extends Record< string, EventHandler< TEvent > >,
TRight extends Record< string, EventHandler< TEvent > >
>( handlers: TLeft, extraHandlers: TRight ): TLeft & TRight {
ciampo marked this conversation as resolved.
Show resolved Hide resolved
// @ts-ignore We'll fill in all the properties below
const mergedHandlers: TLeft & TRight = {
...handlers,
};

for ( const [ key, handler ] of Object.entries( extraHandlers ) ) {
// @ts-ignore
mergedHandlers[ key as keyof typeof mergedHandlers ] =
key in mergedHandlers
? mergeEvent( mergedHandlers[ key ], handler )
: handler;
}

return mergedHandlers;
}
63 changes: 63 additions & 0 deletions packages/components/src/utils/test/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/**
* Internal dependencies
*/
import { mergeEventHandlers } from '../events';

describe( 'mergeEventHandlers', () => {
ciampo marked this conversation as resolved.
Show resolved Hide resolved
it( 'should merge the two event handler objects', () => {
const left = {
onclick: jest.fn(),
};

const right = {
onclick: jest.fn(),
};

const merged = mergeEventHandlers( left, right );

merged.onclick();

expect( left.onclick ).toHaveBeenCalled();
expect( right.onclick ).toHaveBeenCalled();
} );

it( 'should preserve all handlers from the left hand side that do not overlap with the right', () => {
const left = {
ArrowUp: jest.fn(),
ArrowDown: jest.fn(),
};

const right = {
ArrowUp: jest.fn(),
};

const merged = mergeEventHandlers( left, right );

merged.ArrowUp();

expect( left.ArrowUp ).toHaveBeenCalled();
expect( right.ArrowUp ).toHaveBeenCalled();

expect( merged.ArrowDown ).toBe( left.ArrowDown );
} );

it( 'should preserve all handlers form the right hand side that do not overlap with the left', () => {
const right = {
ArrowUp: jest.fn(),
ArrowDown: jest.fn(),
};

const left = {
ArrowUp: jest.fn(),
};

const merged = mergeEventHandlers( left, right );

merged.ArrowUp();

expect( left.ArrowUp ).toHaveBeenCalled();
expect( right.ArrowUp ).toHaveBeenCalled();

expect( merged.ArrowDown ).toBe( right.ArrowDown );
} );
} );