fix(core): keep window listeners registered after removeAllListeners

BR
2mo ago
+23-1

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