Skip to content

feat(main): add WindowManager.owns() for O(1) ownership checks - #18

Merged
loc merged 1 commit into
mainfrom
claude/owns-window-check
May 21, 2026
Merged

loc merged 1 commit into
mainfrom
claude/owns-window-check

Conversation

@loc

@loc loc commented May 12, 2026

Copy link
Copy Markdown
Owner

Demo

N/A — main-process API addition, no UI. Behavior is fully covered by the new unit tests.

Summary

Consumers that filter app-level events (browser-window-focus, -blur, will-navigate) by "is this BrowserWindow one of the manager's?" currently scan:

getWindowManager()?.getAllWindows().some((w) => w.window === win)

per event. That's O(n) per focus/blur tick and re-implements the optional-chain dance for the no-manager case at every callsite.

This PR adds windowManager.owns(win: BrowserWindow): boolean, backed by a WeakSet<BrowserWindow> maintained on every register/unregister path so it's O(1). It also extracts addManagedWindow/removeManagedWindow private helpers so the windows map and the WeakSet can never drift, and stores the BrowserWindow ref in the map entry so removal works after the WindowInstance has been destroyed (instance.window is null by then).

Stack

This is 1 of 4 stacked PRs absorbing consumer workarounds:

  1. ➡️ owns() (this PR)
  2. prepareForQuit() — quit-veto cleanup
  3. childWindowOpenHandler / onChildWindowCreated
  4. reapOnOwnerNavigate

Tests

6 new unit tests covering true/false across all lifecycle exits (DestroyWindow, UnregisterWindow, parent destroyed, manager.destroy()).

Consumers that filter app-level events (browser-window-focus, will-navigate,
etc.) by "is this one of the window manager's windows?" currently scan
getAllWindows().some(w => w.window === win) per event. Add owns() backed by
a WeakSet maintained at every register/unregister site so the check is O(1).

Also extract addManagedWindow/removeManagedWindow helpers so the windows map
and the WeakSet can never drift, and store the BrowserWindow ref in the map
entry so removal works after the WindowInstance has been destroyed (its
.window getter is null by then).
@loc
loc force-pushed the claude/owns-window-check branch from 332b6dd to 962d2bc Compare May 12, 2026 22:22
@loc
loc merged commit ac3b1cd into main May 21, 2026
9 checks passed
@loc
loc deleted the claude/owns-window-check branch May 21, 2026 04:42
@github-actions

github-actions Bot commented Jul 6, 2026

Copy link
Copy Markdown

🎉 This PR is included in version 1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant