VulpesOS/services/screen-power.mjs
OverSu b38dea0597 Publish Vulpes Preview 15 sources for desktop, Android and Tundra
Share Gaia assets and adapters, keep private profiles and development documents local, and update both public READMEs.
2026-09-30 23:13:03 +02:00

28 lines
911 B
JavaScript

// Preserve power transitions; coalesce brightness steps while hardware is busy.
export class ScreenPower {
constructor(state, apply) {
this.state = state;
this.apply = apply;
this.pending = Promise.resolve();
this.tail = null;
}
set(name, value) {
if (name === 'screenBrightness' && this.tail?.name === name && !this.tail.started) {
this.tail.value = value;
return this.tail.promise;
}
const queued = {name, value, started:false};
const operation = this.pending.then(async () => {
queued.started = true;
if (this.tail === queued) this.tail = null;
const next = {...this.state, [name]:queued.value};
await this.apply(next, name);
Object.assign(this.state, next);
return {...this.state};
});
queued.promise = operation;
this.tail = queued;
this.pending = operation.catch(() => {});
return operation;
}
}