-
Notifications
You must be signed in to change notification settings - Fork 10.6k
Make registerCustomEventType idempotent for duplicate registrations.
#66324
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
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -17,10 +17,13 @@ export function registerCustomEventType(eventName: string, options: EventTypeOpt | |
| throw new Error('The options parameter is required.'); | ||
| } | ||
|
|
||
| // There can't be more than one registration for the same event name because then we wouldn't | ||
| // know which eventargs data to supply. | ||
| // Duplicate registrations can occur legitimately when JS initializers (lib.module.js) | ||
| // re-execute after enhanced navigation or circuit reconnection. | ||
| if (eventTypeRegistry.has(eventName)) { | ||
| throw new Error(`The event '${eventName}' is already registered.`); | ||
| console.warn(`The event '${eventName}' is being registered more than once. ` + | ||
| 'Duplicate registrations will be ignored. ' + | ||
| 'This may indicate that a JS initializer is running multiple times.'); | ||
|
Comment on lines
+23
to
+25
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we have an estimate on how often would you see this in the legitimate cases? Just so we don't spam the console so much that people would view it as the app being buggy. |
||
| return; | ||
| } | ||
|
|
||
| // When aliasing a browser event, the custom event name must be different from the browser event name | ||
|
|
||
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,127 @@ | ||
| import { expect, test, describe, afterEach, jest } from '@jest/globals'; | ||
| import { registerCustomEventType, getEventTypeOptions, getEventNameAliases } from '../src/Rendering/Events/EventTypes'; | ||
|
|
||
| // The eventTypeRegistry is module-scoped and persists across tests, so we need | ||
| // unique event names per test to avoid interference from built-in registrations | ||
| // and other tests. | ||
|
|
||
| describe('registerCustomEventType duplicate registration', () => { | ||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| test('duplicate registration with same browserEventName warns and is ignored', () => { | ||
| const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const options1 = { | ||
|
ilonatommy marked this conversation as resolved.
|
||
| browserEventName: 'change', | ||
| createEventArgs: (e: Event) => ({ checked: true }), | ||
| }; | ||
|
|
||
| const options2 = { | ||
| browserEventName: 'change', | ||
| createEventArgs: (e: Event) => ({ checked: false, extra: 'data' }), | ||
| }; | ||
|
|
||
| // First registration succeeds | ||
| registerCustomEventType('mycheckedchange', options1); | ||
|
|
||
| // Second registration with same browserEventName — should NOT throw | ||
| registerCustomEventType('mycheckedchange', options2); | ||
|
|
||
| // Should have warned | ||
| expect(warnSpy).toHaveBeenCalledWith( | ||
| expect.stringContaining('mycheckedchange') | ||
| ); | ||
|
|
||
| // First registration wins (the registry entry is not overwritten) | ||
| const registered = getEventTypeOptions('mycheckedchange'); | ||
| expect(registered).toBe(options1); | ||
|
|
||
| // The alias array should only have one entry, not two | ||
| const aliases = getEventNameAliases('change'); | ||
| const count = aliases!.filter(a => a === 'mycheckedchange').length; | ||
| expect(count).toBe(1); | ||
| }); | ||
|
|
||
| test('duplicate registration with different browserEventName warns and is ignored', () => { | ||
| const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| registerCustomEventType('myevent-diffbrowser', { | ||
| browserEventName: 'click', | ||
| createEventArgs: (e: Event) => ({}), | ||
| }); | ||
|
|
||
| // Different browserEventName — still should NOT throw, just warn | ||
| registerCustomEventType('myevent-diffbrowser', { | ||
| browserEventName: 'mouseover', | ||
| createEventArgs: (e: Event) => ({}), | ||
| }); | ||
|
|
||
| expect(warnSpy).toHaveBeenCalled(); | ||
|
|
||
| // First registration wins | ||
| const registered = getEventTypeOptions('myevent-diffbrowser'); | ||
| expect(registered!.browserEventName).toBe('click'); | ||
| }); | ||
|
|
||
| test('duplicate registration does not cause double alias entries', () => { | ||
| jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| registerCustomEventType('mytabchange', { | ||
| browserEventName: 'change', | ||
| createEventArgs: (e: Event) => ({ activeId: 'tab1' }), | ||
| }); | ||
|
|
||
| // Register again — simulates JS initializer re-firing | ||
| registerCustomEventType('mytabchange', { | ||
| browserEventName: 'change', | ||
| createEventArgs: (e: Event) => ({ activeId: 'tab2' }), | ||
| }); | ||
|
|
||
| // Register a third time | ||
| registerCustomEventType('mytabchange', { | ||
| browserEventName: 'change', | ||
| createEventArgs: (e: Event) => ({ activeId: 'tab3' }), | ||
| }); | ||
|
|
||
| // The alias for 'change' should only contain 'mytabchange' once | ||
| const aliases = getEventNameAliases('change'); | ||
| const count = aliases!.filter(a => a === 'mytabchange').length; | ||
| expect(count).toBe(1); | ||
| }); | ||
|
|
||
| test('simulates FluentUI re-initialization scenario', () => { | ||
| const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {}); | ||
|
|
||
| const fluentEvents = [ | ||
| { name: 'flu-checkedchange', browserEventName: 'change' }, | ||
| { name: 'flu-switchcheckedchange', browserEventName: 'change' }, | ||
| { name: 'flu-sliderchange', browserEventName: 'change' }, | ||
| { name: 'flu-accordionchange', browserEventName: 'change' }, | ||
| { name: 'flu-tabchange', browserEventName: 'change' }, | ||
| ]; | ||
|
|
||
| // First initialization — all should register successfully | ||
| for (const evt of fluentEvents) { | ||
| registerCustomEventType(evt.name, { | ||
| browserEventName: evt.browserEventName, | ||
| createEventArgs: (e: Event) => ({}), | ||
| }); | ||
| } | ||
|
|
||
| expect(warnSpy).not.toHaveBeenCalled(); | ||
|
|
||
| // Second initialization (simulates enhanced nav / circuit reconnect) | ||
| // — should NOT throw, just warn | ||
| for (const evt of fluentEvents) { | ||
| registerCustomEventType(evt.name, { | ||
| browserEventName: evt.browserEventName, | ||
| createEventArgs: (e: Event) => ({}), | ||
| }); | ||
| } | ||
|
|
||
| // Should have warned for each duplicate | ||
| expect(warnSpy).toHaveBeenCalledTimes(fluentEvents.length); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've been looking more at this. And we shouldn't change this to a warning.
There was already an issue we fixed with the event being dispatched twice. Now we are converting something deterministic and invalid into something "valid" that produces nondeterministic behavior (first registration wins). And the only way to find it is if you happen to look at the console errors, which is not something we can generally rely on people doing.