forked from Blendman974/faceclaw
Optimize phone CPU usage
This commit is contained in:
1 parent
449cb0bf73
commit
45d2ece0d2
19 files changed
+485
-103
No files matched your search
@@ -51,9 +51,19 @@ export function installedEvenHubPackageId(appId: string): string | null {
|
||||
}
|
||||
}
|
||||
|
||||
// The parsed index for the last stored value: the launcher and the watch
|
||||
// state both list installed apps on every refresh, and the sort's
|
||||
// localeCompare dominated the cost.
|
||||
let parsedIndex: { raw: string; apps: InstalledEvenHubApp[] } | null = null;
|
||||
|
||||
export function getInstalledEvenHubApps(): InstalledEvenHubApp[] {
|
||||
const raw = getStringSetting(STORAGE_KEY, "");
|
||||
if (!raw) return [];
|
||||
if (parsedIndex?.raw !== raw) parsedIndex = { raw, apps: parseInstalledIndex(raw) };
|
||||
return parsedIndex.apps.map((app) => ({ ...app }));
|
||||
}
|
||||
|
||||
function parseInstalledIndex(raw: string): InstalledEvenHubApp[] {
|
||||
try {
|
||||
const parsed = JSON.parse(raw);
|
||||
if (!Array.isArray(parsed)) return [];
|
||||
|
||||
@@ -2515,6 +2515,13 @@ class DashboardController {
|
||||
return;
|
||||
}
|
||||
const fingerprint = frameTimings.span(frameId, "fingerprint", () => planesFingerprint(planes));
|
||||
// An identical repaint (e.g. every in-process window re-renders when the
|
||||
// switcher brings it to the foreground) would flatten, encode and
|
||||
// composite only to be deduped before sending.
|
||||
if (display.isSurfaceCurrent(surfaceId, fingerprint)) {
|
||||
frameTimings.finishFrame(frameId, "discarded: surface unchanged");
|
||||
return;
|
||||
}
|
||||
const { image, draws } = frameTimings.span(frameId, "flatten", () => flattenPlanesWithDraws(planes));
|
||||
const buffer = frameTimings.span(frameId, "to8bpp", () => image.to8bppBuffer());
|
||||
const preparedDraws = frameTimings.span(frameId, "prepareFrameDraws", () => prepareFrameDraws(draws));
|
||||
|
||||
@@ -36,14 +36,34 @@ export type DisplayList = {
|
||||
};
|
||||
export type PlacedDisplayList = { displayList: DisplayList; x: number; y: number; width: number; height: number; depth: number };
|
||||
|
||||
/**
|
||||
* Growable byte buffer. Lists are encoded on every frame that carries one, and
|
||||
* twice more for its fingerprint and the preview; resources (whole menu rows,
|
||||
* scroll strips) are tens of KB, so they go in with one set() rather than a
|
||||
* push per byte.
|
||||
*/
|
||||
class Writer {
|
||||
bytes: number[] = [];
|
||||
u8(n: number): void { this.bytes.push(integer(n, 0, 255)); }
|
||||
private buffer = new Uint8Array(256);
|
||||
length = 0;
|
||||
private reserve(count: number): void {
|
||||
const needed = this.length + count;
|
||||
if (needed <= this.buffer.length) return;
|
||||
let size = this.buffer.length * 2;
|
||||
while (size < needed) size *= 2;
|
||||
const grown = new Uint8Array(size);
|
||||
grown.set(this.buffer.subarray(0, this.length));
|
||||
this.buffer = grown;
|
||||
}
|
||||
u8(n: number): void { integer(n, 0, 255); this.reserve(1); this.buffer[this.length++] = n; }
|
||||
i8(n: number): void { this.u8(integer(n, -128, 127) & 255); }
|
||||
u16(n: number): void { integer(n, 0, 65535); this.bytes.push(n & 255, n >>> 8); }
|
||||
u16(n: number): void { integer(n, 0, 65535); this.reserve(2); this.buffer[this.length++] = n & 255; this.buffer[this.length++] = n >>> 8; }
|
||||
i16(n: number): void { this.u16(integer(n, -32768, 32767) & 65535); }
|
||||
u32(n: number): void { integer(n, 0, 4294967295); this.u16(n & 65535); this.u16(n >>> 16); }
|
||||
raw(bytes: ArrayLike<number>): void { for (let i = 0; i < bytes.length; i++) this.bytes.push(bytes[i]); }
|
||||
raw(bytes: ArrayLike<number>): void { this.reserve(bytes.length); this.buffer.set(bytes, this.length); this.length += bytes.length; }
|
||||
/** The written bytes, in a buffer of their own. */
|
||||
bytes(): Uint8Array { return this.buffer.slice(0, this.length); }
|
||||
/** A view of the written bytes; valid until the next write. */
|
||||
view(): Uint8Array { return this.buffer.subarray(0, this.length); }
|
||||
}
|
||||
|
||||
/** Length-delimited bridge envelope; call operands use fixed widths except extended x/y. */
|
||||
@@ -85,9 +105,9 @@ export function encodeDisplayList(placed: PlacedDisplayList, now = Date.now()):
|
||||
}
|
||||
}
|
||||
}
|
||||
if (out.bytes.length > 4 * 1024 * 1024) throw new Error('Display list exceeds bridge limit');
|
||||
const header = new Writer(); header.u8(DISPLAY_LIST_RECORD); header.u32(out.bytes.length); header.raw(out.bytes);
|
||||
return Uint8Array.from(header.bytes);
|
||||
if (out.length > 4 * 1024 * 1024) throw new Error('Display list exceeds bridge limit');
|
||||
const header = new Writer(); header.u8(DISPLAY_LIST_RECORD); header.u32(out.length); header.raw(out.view());
|
||||
return header.bytes();
|
||||
}
|
||||
|
||||
/** Replay-record sizes by tag, as in glyph-wire's frame draw buffer. */
|
||||
|
||||
+47
-32
@@ -277,17 +277,28 @@ export class GrayImage {
|
||||
const destX = dx | 0;
|
||||
const destY = dy | 0;
|
||||
|
||||
for (let row = 0; row < copyHeight; row++) {
|
||||
const sy = srcY + row;
|
||||
const ty = destY + row;
|
||||
if (sy < 0 || sy >= source.height || ty < 0 || ty >= this.height) continue;
|
||||
for (let col = 0; col < copyWidth; col++) {
|
||||
const sx = srcX + col;
|
||||
const tx = destX + col;
|
||||
if (sx < 0 || sx >= source.width || tx < 0 || tx >= this.width) continue;
|
||||
const value = source.pixels[sy * source.width + sx] ?? 0;
|
||||
// Clip once, then copy row spans: flattening a frame blits every plane,
|
||||
// and per-pixel bounds checks made that a sizeable share of a paint.
|
||||
const colStart = Math.max(0, -destX);
|
||||
const colEnd = Math.min(copyWidth, source.width - srcX, this.width - destX);
|
||||
const rowStart = Math.max(0, -destY);
|
||||
const rowEnd = Math.min(copyHeight, source.height - srcY, this.height - destY);
|
||||
const span = colEnd - colStart;
|
||||
const src = source.pixels;
|
||||
const dst = this.pixels;
|
||||
// A blit within one image copies pixel by pixel, forwards, as it always has.
|
||||
const bulk = !opts.transparentZero && source !== this;
|
||||
for (let row = rowStart; span > 0 && row < rowEnd; row++) {
|
||||
const from = (srcY + row) * source.width + srcX + colStart;
|
||||
const to = (destY + row) * this.width + destX + colStart;
|
||||
if (bulk) {
|
||||
dst.set(src.subarray(from, from + span), to);
|
||||
continue;
|
||||
}
|
||||
for (let i = 0; i < span; i++) {
|
||||
const value = src[from + i]!;
|
||||
if (opts.transparentZero && value === 0) continue;
|
||||
this.pixels[ty * this.width + tx] = value;
|
||||
dst[to + i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -398,7 +409,7 @@ export class GrayImage {
|
||||
hash = mixInt(hash, placed.source.height);
|
||||
hash = mixInt(hash, placed.source.sourceContentHash32());
|
||||
if (placed.presentation) { const p = placed.presentation; for (const value of [p.radius, p.background, p.border, p.depth, p.mode === "image" ? 1 : p.mode === "masked-image" ? 2 : 0]) hash = mixInt(hash, value);
|
||||
if (p.displayList) for (const byte of encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)) hash = mixInt(hash, byte);
|
||||
if (p.displayList) hash = mixInt(hash, bytesHash32(encodeDisplayList({ displayList: p.displayList, x: 0, y: 0, width: placed.source.width, height: placed.source.height, depth: p.depth }, p.displayList.timeline?.startedAt ?? 0)));
|
||||
}
|
||||
} else {
|
||||
hash = mixInt(hash, 0xf17e);
|
||||
@@ -415,28 +426,9 @@ export class GrayImage {
|
||||
return hash >>> 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* FNV-1a over the raster pixels. Four bytes at a time where alignment
|
||||
* allows: this runs on every frame's fingerprint, over the whole screen, on
|
||||
* the thread that is about to hand the frame to the transport.
|
||||
*/
|
||||
/** Hash of the raster pixels; see bytesHash32. */
|
||||
private pixelsHash32(): number {
|
||||
const pixels = this.pixels;
|
||||
let hash = 2166136261;
|
||||
let i = 0;
|
||||
if ((pixels.byteOffset & 3) === 0) {
|
||||
const words = new Uint32Array(pixels.buffer, pixels.byteOffset, pixels.length >>> 2);
|
||||
for (let w = 0; w < words.length; w++) {
|
||||
hash ^= words[w]!;
|
||||
hash = Math.imul(hash, 16777619);
|
||||
}
|
||||
i = words.length << 2;
|
||||
}
|
||||
for (; i < pixels.length; i++) {
|
||||
hash ^= pixels[i]!;
|
||||
hash = Math.imul(hash, 16777619);
|
||||
}
|
||||
return hash >>> 0;
|
||||
return bytesHash32(this.pixels);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -744,6 +736,29 @@ function clampByte(value: number): number {
|
||||
}
|
||||
|
||||
/** Fold a 32-bit int into an FNV-style running hash. */
|
||||
/**
|
||||
* FNV-1a over bytes. Four bytes at a time where alignment allows: this runs on
|
||||
* every frame's fingerprint, over the whole screen, on the thread that is
|
||||
* about to hand the frame to the transport.
|
||||
*/
|
||||
function bytesHash32(bytes: Uint8Array): number {
|
||||
let hash = 2166136261;
|
||||
let i = 0;
|
||||
if ((bytes.byteOffset & 3) === 0) {
|
||||
const words = new Uint32Array(bytes.buffer, bytes.byteOffset, bytes.length >>> 2);
|
||||
for (let w = 0; w < words.length; w++) {
|
||||
hash ^= words[w]!;
|
||||
hash = Math.imul(hash, 16777619);
|
||||
}
|
||||
i = words.length << 2;
|
||||
}
|
||||
for (; i < bytes.length; i++) {
|
||||
hash ^= bytes[i]!;
|
||||
hash = Math.imul(hash, 16777619);
|
||||
}
|
||||
return hash >>> 0;
|
||||
}
|
||||
|
||||
function mixInt(hash: number, value: number): number {
|
||||
hash ^= value | 0;
|
||||
return Math.imul(hash, 16777619);
|
||||
|
||||
@@ -536,6 +536,14 @@ export class FaceclawCommunicatorBridge {
|
||||
await this.enqueueNativeCall(() => { this.communicator.setScreenBlankedBlanked(Boolean(blanked)); });
|
||||
}
|
||||
|
||||
/**
|
||||
* See the Android bridge. Not wired to the shared compositor's check on iOS
|
||||
* yet, so every repaint is submitted as before.
|
||||
*/
|
||||
isSurfaceCurrent(_surfaceId: string, _fingerprint: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
async submitSurfaceFrame(
|
||||
surfaceId: string,
|
||||
pixels8bpp: Uint8Array,
|
||||
|
||||
@@ -133,6 +133,8 @@ export class FaceclawCommunicatorBridge {
|
||||
private javaCallQueue: Promise<void> = Promise.resolve();
|
||||
/** Calls waiting on javaCallQueue; 0 means enqueueJavaCall's fast path is safe. */
|
||||
private queuedJavaCalls = 0;
|
||||
/** Queued calls that may change what a surface holds; see isSurfaceCurrent. */
|
||||
private queuedSurfaceChanges = 0;
|
||||
// Reused Java-side buffers for byte payloads; passing a JS ArrayBuffer to
|
||||
// Java leaks it (see java-direct-buffer.ts). Loaded inside the queued call,
|
||||
// immediately before the synchronous Java call that consumes them.
|
||||
@@ -267,8 +269,12 @@ export class FaceclawCommunicatorBridge {
|
||||
* the frame timings), which is pure latency on a call the caller is already
|
||||
* awaiting. Off by default: a call that might block for a while should keep
|
||||
* yielding to the main looper first.
|
||||
*
|
||||
* preservesSurfaces marks a call that neither changes a surface's retained
|
||||
* content nor leaves a change waiting for a later composite (it composites
|
||||
* itself), so having it queued does not make isSurfaceCurrent answer false.
|
||||
*/
|
||||
private enqueueJavaCall<T>(operation: () => T, inlineWhenIdle = false): Promise<T> {
|
||||
private enqueueJavaCall<T>(operation: () => T, inlineWhenIdle = false, preservesSurfaces = false): Promise<T> {
|
||||
if (inlineWhenIdle && this.queuedJavaCalls === 0) {
|
||||
try {
|
||||
return Promise.resolve(operation());
|
||||
@@ -277,10 +283,12 @@ export class FaceclawCommunicatorBridge {
|
||||
}
|
||||
}
|
||||
this.queuedJavaCalls++;
|
||||
if (!preservesSurfaces) this.queuedSurfaceChanges++;
|
||||
const run = () =>
|
||||
new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
this.queuedJavaCalls--;
|
||||
if (!preservesSurfaces) this.queuedSurfaceChanges--;
|
||||
try {
|
||||
resolve(operation());
|
||||
} catch (error) {
|
||||
@@ -549,6 +557,7 @@ export class FaceclawCommunicatorBridge {
|
||||
await this.enqueueJavaCall(
|
||||
() => this.communicator.submitShellScene(this.shellSceneBuffer.load(snapshot), paintMs, frameId),
|
||||
true,
|
||||
true,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -562,7 +571,19 @@ export class FaceclawCommunicatorBridge {
|
||||
async setSurfaceVisible(id: string, visible: boolean): Promise<void> {
|
||||
await this.enqueueJavaCall(() => {
|
||||
this.communicator.setSurfaceVisible(id, Boolean(visible));
|
||||
});
|
||||
}, false, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether submitting a full-surface frame with this fingerprint would
|
||||
* change nothing: Java already holds that content for the surface, and no
|
||||
* queued or not-yet-composited change is waiting on a composite. Lets a
|
||||
* repaint that came out identical (a window re-rendered when it comes to
|
||||
* the foreground, say) skip flattening and submitting its frame.
|
||||
*/
|
||||
isSurfaceCurrent(surfaceId: string, fingerprint: string): boolean {
|
||||
if (this.queuedSurfaceChanges > 0) return false;
|
||||
return Boolean(this.communicator.isSurfaceCurrent(surfaceId, fingerprint));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -7,7 +7,25 @@ export type PhoneBatteryState = {
|
||||
charging: boolean | null;
|
||||
};
|
||||
|
||||
// The Android read is a sticky-broadcast binder round trip, and the top bar
|
||||
// asks on every shell paint; the level moves far slower than that.
|
||||
const ANDROID_CACHE_MS = 5000;
|
||||
let cachedState: PhoneBatteryState | null = null;
|
||||
let cachedAtMs = 0;
|
||||
|
||||
export function readPhoneBatteryState(): PhoneBatteryState {
|
||||
if (global.isAndroid) {
|
||||
const now = Date.now();
|
||||
if (cachedState === null || now - cachedAtMs >= ANDROID_CACHE_MS || now < cachedAtMs) {
|
||||
cachedState = readPhoneBatteryStateUncached();
|
||||
cachedAtMs = now;
|
||||
}
|
||||
return { ...cachedState };
|
||||
}
|
||||
return readPhoneBatteryStateUncached();
|
||||
}
|
||||
|
||||
function readPhoneBatteryStateUncached(): PhoneBatteryState {
|
||||
if (global.isIOS) {
|
||||
const device = UIDevice.currentDevice
|
||||
const level = device.batteryLevel
|
||||
|
||||
@@ -18,6 +18,7 @@ export type DisplayTarget = Pick<
|
||||
| "setUnderlayDim"
|
||||
| "setScreenBlanked"
|
||||
| "submitSurfaceFrame"
|
||||
| "isSurfaceCurrent"
|
||||
| "submitShellScene"
|
||||
| "waitForFrameFinished"
|
||||
| "getCompositePreview"
|
||||
@@ -109,6 +110,11 @@ export class PreviewDisplayTarget implements DisplayTarget {
|
||||
this.composited();
|
||||
}
|
||||
|
||||
/** Never skips: preview frames are cheap and nothing is transmitted. */
|
||||
isSurfaceCurrent(_surfaceId: string, _fingerprint: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
async submitShellScene(bytes: Uint8Array, _paintMs = 0, _frameId = 0): Promise<void> {
|
||||
this.compositor.setShellScene(bytes);
|
||||
this.composited();
|
||||
|
||||
@@ -24,6 +24,7 @@ export type DisplayTarget = Pick<
|
||||
| "setUnderlayDim"
|
||||
| "setScreenBlanked"
|
||||
| "submitSurfaceFrame"
|
||||
| "isSurfaceCurrent"
|
||||
| "submitShellScene"
|
||||
| "waitForFrameFinished"
|
||||
| "getCompositePreview"
|
||||
@@ -109,6 +110,11 @@ export class PreviewDisplayTarget implements DisplayTarget {
|
||||
this.compositor.setSurfaceDepth(id, Math.round(depth));
|
||||
}
|
||||
|
||||
/** Never skips: preview frames are cheap and nothing is transmitted. */
|
||||
isSurfaceCurrent(_surfaceId: string, _fingerprint: string): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
async submitShellScene(bytes: Uint8Array, paintMs = 0, frameId = 0): Promise<void> {
|
||||
this.compositor.submitShellScene(this.shellSceneBuffer.load(bytes), paintMs, frameId);
|
||||
}
|
||||
|
||||
@@ -8,17 +8,35 @@
|
||||
import { Utils } from "@nativescript/core";
|
||||
|
||||
declare const com: any;
|
||||
declare const android: any;
|
||||
|
||||
let javaInstance: any = null;
|
||||
// The Java-side listener proxy must stay referenced or it gets GC'd.
|
||||
let retainedListenerProxy: any = null;
|
||||
const changeListeners = new Set<(key: string) => void>();
|
||||
|
||||
// Values already read through JNI, which cost a bridge call each and are read
|
||||
// many times per paint (display mode, fonts, clock format). Only used while
|
||||
// this isolate's Java listener is registered on a thread with a Looper: every
|
||||
// write, from any isolate, notifies that listener, which drops the key before
|
||||
// any other change listener runs. A write from another isolate is visible here
|
||||
// once its notification has been dispatched (one message-loop tick), the same
|
||||
// point at which change listeners hear about it.
|
||||
// Strings are cached without a default (null = unset). Booleans are cached per
|
||||
// default, since the Java getter cannot report "unset", except after a write
|
||||
// from this isolate (defaultValue null: stored, so any default reads it).
|
||||
const stringCache = new Map<string, string | null>();
|
||||
const booleanCache = new Map<string, { defaultValue: boolean | null; value: boolean }>();
|
||||
let cachingEnabled: boolean | null = null;
|
||||
|
||||
/** Release this isolate's native observer before its worker is terminated. */
|
||||
export function disposeSettingsStore(): void {
|
||||
changeListeners.clear();
|
||||
if (retainedListenerProxy !== null) getJava().unregisterListener(retainedListenerProxy);
|
||||
retainedListenerProxy = null;
|
||||
cachingEnabled = false;
|
||||
stringCache.clear();
|
||||
booleanCache.clear();
|
||||
}
|
||||
|
||||
function getJava(): any {
|
||||
@@ -31,20 +49,80 @@ function getJava(): any {
|
||||
return javaInstance;
|
||||
}
|
||||
|
||||
function canCache(): boolean {
|
||||
if (cachingEnabled === null) {
|
||||
cachingEnabled = false;
|
||||
try {
|
||||
// FaceclawSettings dispatches notifications through the registering
|
||||
// thread's Looper; without one this isolate would never hear of writes.
|
||||
if (android.os.Looper.myLooper() !== null) {
|
||||
ensureJavaListener();
|
||||
cachingEnabled = true;
|
||||
}
|
||||
} catch {
|
||||
cachingEnabled = false;
|
||||
}
|
||||
}
|
||||
return cachingEnabled;
|
||||
}
|
||||
|
||||
export function getStringSetting(key: string, defaultValue: string): string {
|
||||
return String(getJava().getString(key, defaultValue));
|
||||
if (!canCache()) return String(getJava().getString(key, defaultValue));
|
||||
let value = stringCache.get(key);
|
||||
if (value === undefined) {
|
||||
const raw = getJava().getString(key, null);
|
||||
value = raw === null || raw === undefined ? null : String(raw);
|
||||
stringCache.set(key, value);
|
||||
}
|
||||
return value ?? defaultValue;
|
||||
}
|
||||
|
||||
export function setStringSetting(key: string, value: string): void {
|
||||
getJava().setString(key, value);
|
||||
if (cachingEnabled) stringCache.set(key, value === null || value === undefined ? null : String(value));
|
||||
}
|
||||
|
||||
export function getBooleanSetting(key: string, defaultValue: boolean): boolean {
|
||||
return Boolean(getJava().getBoolean(key, defaultValue));
|
||||
if (!canCache()) return Boolean(getJava().getBoolean(key, defaultValue));
|
||||
const cached = booleanCache.get(key);
|
||||
if (cached !== undefined && (cached.defaultValue === null || cached.defaultValue === defaultValue)) {
|
||||
return cached.value;
|
||||
}
|
||||
const value = Boolean(getJava().getBoolean(key, defaultValue));
|
||||
booleanCache.set(key, { defaultValue, value });
|
||||
return value;
|
||||
}
|
||||
|
||||
export function setBooleanSetting(key: string, value: boolean): void {
|
||||
getJava().setBoolean(key, value);
|
||||
if (cachingEnabled) booleanCache.set(key, { defaultValue: null, value: Boolean(value) });
|
||||
}
|
||||
|
||||
function forgetCachedSetting(key: string | null | undefined): void {
|
||||
if (key === null || key === undefined) {
|
||||
stringCache.clear();
|
||||
booleanCache.clear();
|
||||
return;
|
||||
}
|
||||
stringCache.delete(key);
|
||||
booleanCache.delete(key);
|
||||
}
|
||||
|
||||
function ensureJavaListener(): void {
|
||||
if (retainedListenerProxy !== null) return;
|
||||
retainedListenerProxy = new com.faceclaw.app.FaceclawSettingsListener({
|
||||
onSettingChanged: (key: string) => {
|
||||
forgetCachedSetting(key === null || key === undefined ? key : String(key));
|
||||
for (const registered of Array.from(changeListeners)) {
|
||||
try {
|
||||
registered(String(key));
|
||||
} catch (error) {
|
||||
console.warn("settings change listener failed", error);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
getJava().registerListener(retainedListenerProxy);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -53,20 +131,7 @@ export function setBooleanSetting(key: string, value: boolean): void {
|
||||
* change.
|
||||
*/
|
||||
export function onSettingsStoreChanged(listener: (key: string) => void): () => void {
|
||||
if (retainedListenerProxy === null) {
|
||||
retainedListenerProxy = new com.faceclaw.app.FaceclawSettingsListener({
|
||||
onSettingChanged: (key: string) => {
|
||||
for (const registered of Array.from(changeListeners)) {
|
||||
try {
|
||||
registered(String(key));
|
||||
} catch (error) {
|
||||
console.warn("settings change listener failed", error);
|
||||
}
|
||||
}
|
||||
},
|
||||
});
|
||||
getJava().registerListener(retainedListenerProxy);
|
||||
}
|
||||
ensureJavaListener();
|
||||
changeListeners.add(listener);
|
||||
return () => {
|
||||
changeListeners.delete(listener);
|
||||
|
||||
@@ -14,8 +14,9 @@ export type PersistedOpenApps = {
|
||||
|
||||
const STATE_VERSION = 1;
|
||||
const FILE_NAME = "open-apps.json";
|
||||
// Coalesces bursts (restore, sidebar scrolling) into one write; short enough
|
||||
// that a build-reinstall kill right after a change rarely loses it.
|
||||
// Coalesces bursts (restore, sidebar scrolling) into one write once they
|
||||
// settle; short enough that a build-reinstall kill right after a change
|
||||
// rarely loses it.
|
||||
const WRITE_DELAY_MS = 1000;
|
||||
|
||||
function openAppsFilePath(): string {
|
||||
@@ -50,7 +51,10 @@ let pendingState: PersistedOpenApps | null = null;
|
||||
/** Save the open-app state (debounced; the latest state wins). */
|
||||
export function savePersistedOpenApps(state: PersistedOpenApps): void {
|
||||
pendingState = state;
|
||||
if (writeTimer !== null) return;
|
||||
// Restarted on every change: scrolling through the switcher changes the
|
||||
// foreground app a few times a second, and each synchronous write costs
|
||||
// several milliseconds of main thread.
|
||||
if (writeTimer !== null) clearTimeout(writeTimer);
|
||||
writeTimer = setTimeout(() => {
|
||||
writeTimer = null;
|
||||
const state = pendingState;
|
||||
|
||||
Reference in new issue
Block a user