Skip to content

Fix removing a listener shared by multiple remote events - #728

Open
kwy404 wants to merge 1 commit into
Shopify:mainfrom
kwy404:fix-shared-event-listener-removal
Open

kwy404 wants to merge 1 commit into
Shopify:mainfrom
kwy404:fix-shared-event-listener-removal

Conversation

@kwy404

@kwy404 kwy404 commented Sep 25, 2026

Copy link
Copy Markdown

Problem

RemoteElement keeps one listener record per element in a map keyed only by the listener. When the same listener is added for two remote events, the second addEventListener() call replaces the record for the first event:

const Button = createRemoteElement({events: ['press', 'hover']});

element.addEventListener('press', listener);
element.addEventListener('hover', listener);

element.removeEventListener('press', listener);
element.removeEventListener('hover', listener);

On main, the host keeps both press and hover in the element's eventListeners after both calls. The first removal reads the hover record, so no update is sent for press, and it deletes the shared map entry, so the second removal finds no record and returns early.

Change

Store listener records on each event record (remoteEvent.listeners is now a Map instead of a Set) and look them up by event type in removeEventListener(). The per-element listener map is no longer needed and is removed.

Tests

Added a test in packages/core/source/tests/elements.test.ts that adds one listener for two events, removes it from each, and expects an event listener update for each event. It fails on main and passes with this change.

Ran pnpm test (all passing except prepare-wpt.test.ts, which also fails on main on Windows with an EPERM rename error), pnpm type-check, pnpm lint, and the @remote-dom/core build. Added a patch changeset for @remote-dom/core.

RemoteElement stored listener records in a per-element map keyed only by
the listener, so adding the same listener for a second remote event
replaced the record for the first one. Removing it afterwards never told
the host, which kept both event listeners registered.

Track listener records on each event record instead.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant