-
Notifications
You must be signed in to change notification settings - Fork 4.7k
components: Add mergeEventHandlers #33205
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a415ab6
components: Add mergeEventHandlers
sarayourfriend a41f480
Preserve the right hand sides non-interescting properties
sarayourfriend bccb68a
Simplify merging logic
sarayourfriend ee92df9
Update packages/components/src/utils/events.ts
sarayourfriend File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 two sets of event handlers together. | ||
| * | ||
| * @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 { | ||
| // @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; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 ); | ||
| } ); | ||
| } ); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.