Clean up unused worker threads for closed apps; other performance optimizations

This commit is contained in:
James Babcock committed 2026-09-22 14:56:54 -07:00
1 parent 9560f3ac8d
commit 2e4d20e130
20 files changed
+360 -13

No files matched your search

+7
View File
@@ -11,6 +11,7 @@
* focus, tap-then-hold opens the window menu.
*/
import "@nativescript/core/globals";
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
import { GrayImage } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
@@ -187,6 +188,12 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "check-idle":
post({ type: "worker-idle" });
break;
case "shutdown":
finishWorkerShutdown();
break;
case "open-window": {
const window: BlocksWindow = {
windowId: message.windowId,
+7
View File
@@ -25,6 +25,7 @@
* focus to the sidebar) pauses, as does backgrounding or screen-off.
*/
import "@nativescript/core/globals";
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
import { GrayImage } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
@@ -180,6 +181,12 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "check-idle":
post({ type: "worker-idle" });
break;
case "shutdown":
finishWorkerShutdown();
break;
case "open-window": {
const window: FlappyWindow = {
windowId: message.windowId,
+7
View File
@@ -18,6 +18,7 @@
* Safe cards auto-play to the foundations after every move.
*/
import "@nativescript/core/globals";
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
import { GrayImage } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
@@ -144,6 +145,12 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "check-idle":
post({ type: "worker-idle" });
break;
case "shutdown":
finishWorkerShutdown();
break;
case "open-window": {
const window: FreecellWindow = {
windowId: message.windowId,
+11
View File
@@ -0,0 +1,11 @@
import { getStringSetting } from "../../native/settings-store";
import { QUADRANT_LAYOUT, SIX_SLOT_LAYOUT } from "./layout";
/** Configuration-only query usable by workers without loading widget/UI modules. */
export function isTerminalWidgetConfigured(): boolean {
const layout = getStringSetting("glanceboard.layout", "2x2") === "2x3" ? SIX_SLOT_LAYOUT : QUADRANT_LAYOUT;
// Both layouts store their slots under the original quadrants key. None
// of the default slots needs a worker, so absent keys cannot retain one.
return layout.slots.some((_, index) =>
getStringSetting(`glanceboard.${QUADRANT_LAYOUT.id}.slot.${index}`, "none") === "terminal");
}
@@ -18,6 +18,7 @@
* launches.
*/
import "@nativescript/core/globals";
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
import { GrayImage } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
@@ -152,6 +153,12 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "check-idle":
post({ type: "worker-idle" });
break;
case "shutdown":
finishWorkerShutdown();
break;
case "open-window": {
const window: MinesweeperWindow = {
windowId: message.windowId,
+7
View File
@@ -7,6 +7,7 @@
* guidance text (right pane) updates per GPS fix and ships as small deltas.
*/
import "@nativescript/core/globals";
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
import { GrayImage } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
@@ -275,6 +276,12 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "check-idle":
post({ type: "worker-idle" });
break;
case "shutdown":
finishWorkerShutdown();
break;
case "navigation-sensors":
handleNavigationSensorEvent(message.event);
break;
+7
View File
@@ -21,6 +21,7 @@
* sidebar) pauses, as does backgrounding or screen-off.
*/
import "@nativescript/core/globals";
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
import { GrayImage } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
@@ -290,6 +291,12 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "check-idle":
post({ type: "worker-idle" });
break;
case "shutdown":
finishWorkerShutdown();
break;
case "open-window": {
const window: PinballWindow = {
windowId: message.windowId,
+7
View File
@@ -8,6 +8,7 @@
* menu, via the phone app's text editor.
*/
import "@nativescript/core/globals";
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
import { GrayImage } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, singlePlane, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
@@ -165,6 +166,12 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "check-idle":
post({ type: "worker-idle" });
break;
case "shutdown":
finishWorkerShutdown();
break;
case "open-window":
window = {
windowId: message.windowId,
+8
View File
@@ -0,0 +1,8 @@
import { isTerminalWidgetConfigured } from "../glanceboard/configuration";
import { loadConnections, parseConnectionString } from "./connections";
/** Keep session-list connections alive for a configured widget, even while it is hidden. */
export function hasTerminalBackgroundWork(): boolean {
return isTerminalWidgetConfigured() &&
loadConnections().some(connection => connection.enabled && parseConnectionString(connection.url) !== null);
}
+25 -7
View File
@@ -31,6 +31,8 @@
* re-attach snapshot resyncs contents and scrollback only once it's needed.
*/
import "@nativescript/core/globals";
import { hasTerminalBackgroundWork } from "./background";
import { finishWorkerShutdown } from "../../ui/shell/worker-lifecycle";
import { GrayImage, type UiFont } from "../../graphics/image";
import { flattenPlanesWithDraws, planesFingerprint, type Plane } from "../../graphics/plane";
import { prepareFrameDraws } from "../../graphics/glyph-wire";
@@ -311,6 +313,14 @@ post({ type: "worker-ready" });
global.onmessage = (event: { data: WorkerAppMessage }) => {
const message = event.data;
switch (message.type) {
case "check-idle":
reportIdle();
break;
case "shutdown":
for (const control of controls.values()) stopControl(control);
controls.clear();
finishWorkerShutdown();
break;
case "open-window":
openWindow(message.windowId, message.surfaceId, message.title, message.viewport);
break;
@@ -424,12 +434,17 @@ global.onmessage = (event: { data: WorkerAppMessage }) => {
// toggles edited in the Settings app, the Add-connection draft typed on the
// phone).
onSettingsStoreChanged((key) => {
if (key.startsWith("glanceboard.")) {
reportIdle();
return;
}
if (!key.startsWith("terminal.")) return;
switch (key) {
case TERMINAL_CONNECTIONS_KEY:
// Only once the app has actually been opened.
if (controlsInitialized) syncControlsFromSettings();
renderHubWindows();
reportIdle();
return;
case NEW_CONNECTION_DRAFT_KEY:
// Live keystrokes from the phone editor; repaint the add screen.
@@ -438,7 +453,7 @@ onSettingsStoreChanged((key) => {
case "terminal.autoReconnect":
if (!terminalAutoReconnectSetting.get()) {
cancelPendingReconnects();
} else if (windows.size > 0) {
} else if (windows.size > 0 || hasTerminalBackgroundWork()) {
for (const control of controls.values()) {
if ((control.state?.phase ?? "idle") === "failed") scheduleControlReconnect(control);
}
@@ -450,6 +465,10 @@ onSettingsStoreChanged((key) => {
}
});
function reportIdle(): void {
if (windows.size === 0 && !hasTerminalBackgroundWork()) post({ type: "worker-idle" });
}
/** Cancel scheduled retries (auto-reconnect turned off); stale views stay marked. */
function cancelPendingReconnects(): void {
for (const control of controls.values()) {
@@ -521,8 +540,8 @@ function closeWindow(windowId: string): void {
windows.delete(windowId);
windowIconActivity.delete(windowId);
updateHubAnimation();
// Auto-reconnect only runs while at least one terminal window is open.
if (windows.size === 0) {
// A configured widget owns the control connections after the last window closes.
if (windows.size === 0 && !hasTerminalBackgroundWork()) {
for (const control of controls.values()) {
cancelControlReconnect(control);
}
@@ -676,19 +695,18 @@ function noteSessionListRecency(control: ControlConnection, state: G2MirrorState
/**
* Retry a control connection after a backoff delay. The delay doubles per
* scheduled attempt and resets when a connection reaches "connected" (or on
* a manual Connect). No-op when auto-reconnect is off, no terminal window is
* open, or the connection was disabled/removed meanwhile.
* a manual Connect). Windows and a configured widget can both own the client.
*/
function scheduleControlReconnect(control: ControlConnection): void {
if (control.reconnectTimer) return;
if (!terminalAutoReconnectSetting.get()) return;
if (windows.size === 0) return;
if (windows.size === 0 && !hasTerminalBackgroundWork()) return;
if (!control.config.enabled) return;
const delayMs = control.reconnectDelayMs;
control.reconnectDelayMs = Math.min(control.reconnectDelayMs * 2, RECONNECT_MAX_DELAY_MS);
control.reconnectTimer = setTimeout(() => {
control.reconnectTimer = null;
if (!terminalAutoReconnectSetting.get() || windows.size === 0) return;
if (!terminalAutoReconnectSetting.get() || (windows.size === 0 && !hasTerminalBackgroundWork())) return;
if (controls.get(control.config.id) !== control || !control.config.enabled) return;
if ((control.state?.phase ?? "idle") === "failed") startControl(control);
}, delayMs);