-
Notifications
You must be signed in to change notification settings - Fork 308
chore: add eventListenerService [WPB-23186] #21000
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
Draft
e-maad
wants to merge
2
commits into
dev
Choose a base branch
from
chore/add-event-listener-service-WPB-23186
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+445
−0
Draft
Changes from all commits
Commits
Show all changes
2 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
171 changes: 171 additions & 0 deletions
171
apps/webapp/src/script/eventListener/deterministicEventListenerService.test.ts
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,171 @@ | ||
| /* | ||
| * Wire | ||
| * Copyright (C) 2026 Wire Swiss GmbH | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see http://www.gnu.org/licenses/. | ||
| * | ||
| */ | ||
|
|
||
| import {createDeterministicEventListenerService} from './deterministicEventListenerService'; | ||
|
|
||
| describe('createDeterministicEventListenerService', () => { | ||
| it('dispatches an event to a registered listener', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const listener = jest.fn(); | ||
| const event = new Event('click'); | ||
|
|
||
| service.addEventListener('click', listener); | ||
| service.dispatch('click', event); | ||
|
|
||
| expect(listener).toHaveBeenCalledTimes(1); | ||
| expect(listener).toHaveBeenCalledWith(event); | ||
| }); | ||
|
|
||
| it('does not dispatch to listeners of a different event type', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const clickListener = jest.fn(); | ||
| const keydownListener = jest.fn(); | ||
|
|
||
| service.addEventListener('click', clickListener); | ||
| service.addEventListener('keydown', keydownListener); | ||
| service.dispatch('click', new Event('click')); | ||
|
|
||
| expect(clickListener).toHaveBeenCalledTimes(1); | ||
| expect(keydownListener).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('dispatches to all listeners registered for the same type in registration order', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const executionOrder: string[] = []; | ||
|
|
||
| service.addEventListener('click', () => executionOrder.push('first')); | ||
| service.addEventListener('click', () => executionOrder.push('second')); | ||
| service.addEventListener('click', () => executionOrder.push('third')); | ||
| service.dispatch('click', new Event('click')); | ||
|
|
||
| expect(executionOrder).toEqual(['first', 'second', 'third']); | ||
| }); | ||
|
|
||
| it('does not dispatch after removeEventListener', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const listener = jest.fn(); | ||
|
|
||
| service.addEventListener('click', listener); | ||
| service.removeEventListener('click', listener); | ||
| service.dispatch('click', new Event('click')); | ||
|
|
||
| expect(listener).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('only removes the listener matching the provided capture flag', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const listener = jest.fn(); | ||
|
|
||
| service.addEventListener('click', listener, {capture: false}); | ||
| service.removeEventListener('click', listener, {capture: true}); | ||
| service.dispatch('click', new Event('click')); | ||
|
|
||
| expect(listener).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('removes only the first matching listener when duplicates are registered', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const listener = jest.fn(); | ||
|
|
||
| service.addEventListener('click', listener); | ||
| service.addEventListener('click', listener); | ||
| service.removeEventListener('click', listener); | ||
| service.dispatch('click', new Event('click')); | ||
|
|
||
| expect(listener).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('does nothing when dispatching a type with no registered listeners', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
|
|
||
| expect(() => service.dispatch('click', new Event('click'))).not.toThrow(); | ||
| }); | ||
|
|
||
| it('does nothing when removing a listener for a type that was never registered', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const listener = jest.fn(); | ||
|
|
||
| expect(() => service.removeEventListener('click', listener)).not.toThrow(); | ||
| }); | ||
|
|
||
| it('ignores null listeners in addEventListener', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
|
|
||
| expect(() => service.addEventListener('click', null)).not.toThrow(); | ||
| expect(() => service.dispatch('click', new Event('click'))).not.toThrow(); | ||
| }); | ||
|
|
||
| it('ignores null listeners in removeEventListener', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
|
|
||
| expect(() => service.removeEventListener('click', null)).not.toThrow(); | ||
| }); | ||
|
|
||
| it('calls handleEvent on an EventListenerObject', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const event = new Event('click'); | ||
| const listenerObject: EventListenerObject = {handleEvent: jest.fn()}; | ||
|
|
||
| service.addEventListener('click', listenerObject); | ||
| service.dispatch('click', event); | ||
|
|
||
| expect(listenerObject.handleEvent).toHaveBeenCalledTimes(1); | ||
| expect(listenerObject.handleEvent).toHaveBeenCalledWith(event); | ||
| }); | ||
|
|
||
| it('fires a once listener only once then auto-removes it', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const listener = jest.fn(); | ||
|
|
||
| service.addEventListener('click', listener, {once: true}); | ||
| service.dispatch('click', new Event('click')); | ||
| service.dispatch('click', new Event('click')); | ||
|
|
||
| expect(listener).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('keeps non-once listeners after dispatch when a once listener is also registered', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const onceListener = jest.fn(); | ||
| const persistentListener = jest.fn(); | ||
|
|
||
| service.addEventListener('click', onceListener, {once: true}); | ||
| service.addEventListener('click', persistentListener); | ||
| service.dispatch('click', new Event('click')); | ||
| service.dispatch('click', new Event('click')); | ||
|
|
||
| expect(onceListener).toHaveBeenCalledTimes(1); | ||
| expect(persistentListener).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('treats boolean capture option as the capture flag', () => { | ||
| const service = createDeterministicEventListenerService(); | ||
| const captureListener = jest.fn(); | ||
| const bubbleListener = jest.fn(); | ||
|
|
||
| service.addEventListener('click', captureListener, true); | ||
| service.addEventListener('click', bubbleListener, false); | ||
|
|
||
| service.removeEventListener('click', captureListener, true); | ||
| service.dispatch('click', new Event('click')); | ||
|
|
||
| expect(captureListener).not.toHaveBeenCalled(); | ||
| expect(bubbleListener).toHaveBeenCalledTimes(1); | ||
| }); | ||
| }); |
119 changes: 119 additions & 0 deletions
119
apps/webapp/src/script/eventListener/deterministicEventListenerService.ts
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,119 @@ | ||
| /* | ||
| * Wire | ||
| * Copyright (C) 2026 Wire Swiss GmbH | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the GNU General Public License as published by | ||
| * the Free Software Foundation, either version 3 of the License, or | ||
| * (at your option) any later version. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * GNU General Public License for more details. | ||
| * | ||
| * You should have received a copy of the GNU General Public License | ||
| * along with this program. If not, see http://www.gnu.org/licenses/. | ||
| * | ||
| */ | ||
|
|
||
| import is from '@sindresorhus/is'; | ||
|
|
||
| import {EventListenerService} from './eventListenerService'; | ||
|
|
||
| type ListenerEntry = { | ||
| readonly listener: EventListenerOrEventListenerObject; | ||
| readonly capture: boolean; | ||
| readonly once: boolean; | ||
| }; | ||
|
|
||
| export type DeterministicEventListenerService = EventListenerService & { | ||
| dispatch(type: string, event: Event): void; | ||
| }; | ||
|
|
||
| function normalizeCapture(options?: boolean | AddEventListenerOptions | EventListenerOptions): boolean { | ||
| if (is.boolean(options)) { | ||
| return options; | ||
| } | ||
|
|
||
| return options?.capture ?? false; | ||
| } | ||
|
|
||
| function normalizeOnce(options?: boolean | AddEventListenerOptions): boolean { | ||
| if (is.boolean(options)) { | ||
| return false; | ||
| } | ||
|
|
||
| return options?.once ?? false; | ||
| } | ||
|
|
||
| export function createDeterministicEventListenerService(): DeterministicEventListenerService { | ||
| const listenerMap = new Map<string, ListenerEntry[]>(); | ||
|
|
||
| return { | ||
| addEventListener(type, listener, options) { | ||
| if (is.nullOrUndefined(listener)) { | ||
| return; | ||
| } | ||
|
|
||
| const entries = listenerMap.get(type) ?? []; | ||
|
|
||
| if (!listenerMap.has(type)) { | ||
| listenerMap.set(type, entries); | ||
| } | ||
|
|
||
| entries.push({ | ||
| capture: normalizeCapture(options), | ||
| listener, | ||
| once: normalizeOnce(options), | ||
| }); | ||
| }, | ||
|
|
||
| removeEventListener(type, listener, options) { | ||
| if (is.nullOrUndefined(listener)) { | ||
| return; | ||
| } | ||
|
|
||
| const capture = normalizeCapture(options); | ||
| const entries = listenerMap.get(type); | ||
|
|
||
| if (is.undefined(entries)) { | ||
| return; | ||
| } | ||
|
|
||
| const index = entries.findIndex(entry => entry.listener === listener && entry.capture === capture); | ||
|
|
||
| if (index !== -1) { | ||
| entries.splice(index, 1); | ||
| } | ||
| }, | ||
|
|
||
| dispatch(type, event) { | ||
| const entries = listenerMap.get(type); | ||
|
|
||
| if (is.undefined(entries)) { | ||
| return; | ||
| } | ||
|
|
||
| for (const entry of entries) { | ||
| if (entry.once) { | ||
| const current = listenerMap.get(type); | ||
|
|
||
| if (!is.undefined(current)) { | ||
| const index = current.indexOf(entry); | ||
|
|
||
| if (index !== -1) { | ||
| current.splice(index, 1); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (is.function_(entry.listener)) { | ||
| entry.listener(event); | ||
| } else { | ||
| entry.listener.handleEvent(event); | ||
| } | ||
| } | ||
| }, | ||
| }; | ||
| } | ||
Oops, something went wrong.
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.
Mutating the same array inside a loop will skip the some array elements.
[eventOnce, event1, event2]
if you remove eventOnce after the first iteration then event1 will be skipped in the second iteration as it will be on index 0 and on index 1 it will be event2.
So we can store the events to be removed on another array and delete it after the loop.