Skip to content

Commit

Permalink
Fix some small bugs (#915)
Browse files Browse the repository at this point in the history
The APIs that are not supported by every browser or may not always be
allowed (Battery and Wake Lock) are requested in try catch blocks. The
problem was that both of them actually yield promises. If the promises
fail, then those exceptions would not be caught properly, because we
forgot to await them. So especially for the wake lock you would
sometimes see an error message when it gets hot reloaded. This should be
fixed now.

Additionally this changes the window title for when there are unsaved
changes. Apparently when LiveSplit One is used as a PWA, then
at least Chrome wants to always keep the app name in the title. If the
title itself is changed, then it gets shown as `LiveSplit One - The new
title`. By us putting an asterisk in the front of the title, the PWA
then shows `LiveSplit One - *LiveSplit One`, which is pretty bad. I
noticed that if the app name is also the prefix of the title, Chrome
recognizes that and does not end up showing the app name twice.

There also was a small bug in the event sink where the
`togglePauseOrStart` event did not update the splits modified state.
  • Loading branch information
CryZe authored Jun 5, 2024
1 parent fe1878b commit 1351da3
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ try {
generalSettings,
} = await LiveSplit.loadStoredData();

function requestWakeLock() {
async function requestWakeLock() {
try {
(navigator as any)?.wakeLock?.request("screen");
await (navigator as any)?.wakeLock?.request();
} catch {
// It's fine if it fails.
}
Expand Down
1 change: 1 addition & 0 deletions src/ui/LSOEventSink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export class LSOEventSink {

this.currentPhaseChanged();
this.currentSplitChanged();
this.splitsModifiedChanged();
}

public pause(): void {
Expand Down
8 changes: 7 additions & 1 deletion src/ui/LiveSplit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -828,7 +828,13 @@ export class LiveSplit extends React.Component<Props, State> {
} catch {
// It's fine if this fails.
}
document.title = "*LiveSplit One";

// It's important that any change is at the end of the title,
// because at least Chrome then recognizes that it's an extension of
// the PWA name. Otherwise it would show:
// LiveSplit One - Window Title
// which would repeat LiveSplit One.
document.title = "LiveSplit One ●";
} else {
try {
navigator?.clearAppBadge();
Expand Down
55 changes: 37 additions & 18 deletions src/util/FrameRate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,30 @@ switch (navigator.platform) {

export let batteryAwareFrameRate: FrameRate = batteryFrameRate;

let computePressure: FrameRate = FRAME_RATE_MATCH_SCREEN;

if ('PressureObserver' in window) {
try {
const observer = new (window as any).PressureObserver((records: any) => {
const state = records[0].state;
switch (state) {
case "serious":
batteryAwareFrameRate = FRAME_RATE_SERIOUS;
break;
case "critical":
batteryAwareFrameRate = FRAME_RATE_CRITICAL;
break;
default:
batteryAwareFrameRate = batteryFrameRate;
}
});
observer.observe("cpu", { sampleInterval: 2_000 });
} catch {
// The Compute Pressure API is not supported by the browser.
}
(async () => {
try {
const observer = new (window as any).PressureObserver((records: any) => {
const state = records[0].state;
switch (state) {
case "serious":
computePressure = FRAME_RATE_SERIOUS;
break;
case "critical":
computePressure = FRAME_RATE_CRITICAL;
break;
default:
computePressure = FRAME_RATE_MATCH_SCREEN;
}
updateBatteryAwareFrameRate();
});
await observer.observe("cpu", { sampleInterval: 2_000 });
} catch {
// The Compute Pressure API is not supported by every browser.
}
})();
}

(async () => {
Expand All @@ -48,8 +53,22 @@ if ('PressureObserver' in window) {
batteryFrameRate = batteryApi.charging === true
? FRAME_RATE_MATCH_SCREEN
: FRAME_RATE_LOW_POWER;
updateBatteryAwareFrameRate();
};
} catch {
// The battery API is not supported by every browser.
}
})();

function updateBatteryAwareFrameRate() {
// Choose the lower of the two frame rates. If one of them is a string, it
// is "Match Screen", which has the lowest priority.

if (typeof batteryFrameRate === "string") {
batteryAwareFrameRate = computePressure;
} else if (typeof computePressure === "string") {
batteryAwareFrameRate = batteryFrameRate;
} else {
batteryAwareFrameRate = Math.min(batteryFrameRate, computePressure);
}
}

0 comments on commit 1351da3

Please sign in to comment.