Skip to content

fix(core): keep window listeners registered after removeAllListeners - #8628

Open
breken-ai wants to merge 1 commit into
ionic-team:mainfrom
breken-ai:fix/web-plugin-remove-all-listeners
Open

breken-ai wants to merge 1 commit into
ionic-team:mainfrom
breken-ai:fix/web-plugin-remove-all-listeners

Conversation

@breken-ai

Copy link
Copy Markdown

Description

WebPlugin.removeAllListeners() unregisters the window listeners and then also clears the map built by registerWindowListener():

for (const listener in this.windowListeners) {
  this.removeWindowListener(this.windowListeners[listener]);
}
this.windowListeners = {};

Plugins call registerWindowListener() once, in their constructor (for example MotionWeb registers devicemotion → accel and deviceorientation → orientation). After removeAllListeners() the map is empty, so a later addListener() finds no window listener to attach and the event never fires again on web.

removeWindowListener() already sets registered = false on each handle, which is what addListener() checks before attaching it again. This PR keeps the map and only unregisters the handles.

Change Type

  • Fix
  • Feature
  • Refactor
  • Breaking Change
  • Documentation
  • Other (CI, chores, etc.)

Rationale / Problems Fixed

On web:

await Motion.removeAllListeners();
await Motion.addListener('accel', (event) => console.log(event)); // never called

A common pattern is clearing listeners when a page or component unmounts and adding them again when it mounts. On the second mount window-backed events stop arriving, while the same code works on iOS and Android.

Tests or Reproductions

Added Should re-register window listeners after removeAllListeners to core/src/tests/web-plugin.spec.ts: register a window listener, add a listener, call removeAllListeners(), add a new listener, dispatch the window event.

  • Before the fix: fails, getWindowListeners()['test'] is undefined and the new listener is not called.
  • After the fix: passes. npm test in core: all suites pass.

eslint and prettier --check are clean on the changed files.

Screenshots / Media

N/A

Platforms Affected

  • Android
  • iOS
  • Web

Notes / Comments

I found this while reading the code with an AI assistant (Claude); I checked the failing test and the fix myself.

🤖 Generated with Claude Code

removeAllListeners() cleared the map filled by registerWindowListener(), so window-backed events (e.g. Motion accel) never fired again after a later addListener() on web. Only unregister the handles.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

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