pause module when page doesn't have focus

This commit is contained in:
2022-04-10 23:24:04 +02:00
parent 37f12f5a2c
commit 9ebb6b6d34
4 changed files with 24 additions and 4 deletions

2
Cargo.lock generated
View File

@@ -499,7 +499,7 @@ checksum = "b365fabc795046672053e29c954733ec3b05e4be654ab130fe8f1f94d7051f35"
[[package]] [[package]]
name = "curlywas" name = "curlywas"
version = "0.1.0" version = "0.1.0"
source = "git+https://github.com/exoticorn/curlywas.git?rev=557c3a84#557c3a842675a1ebd77e84021f9f5236d4fa5648" source = "git+https://github.com/exoticorn/curlywas.git?rev=ebc701e#ebc701e2f2a5849a11ac94950b56803d70f4341d"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"ariadne", "ariadne",

View File

@@ -16,7 +16,7 @@ anyhow = "1"
minifb = { version = "0.20", default-features = false, features = ["x11"] } minifb = { version = "0.20", default-features = false, features = ["x11"] }
notify = "4" notify = "4"
pico-args = "0.4" pico-args = "0.4"
curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "557c3a84" } curlywas = { git = "https://github.com/exoticorn/curlywas.git", rev = "ebc701e" }
wat = "1" wat = "1"
uw8-tool = { path = "uw8-tool" } uw8-tool = { path = "uw8-tool" }
same-file = "1" same-file = "1"

File diff suppressed because one or more lines are too long

View File

@@ -99,9 +99,11 @@ export default function MicroW8(screen, config = {}) {
let audioContext = new AudioContext({sampleRate: 44100}); let audioContext = new AudioContext({sampleRate: 44100});
let keepRunning = true; let keepRunning = true;
let abortController = new AbortController();
cancelFunction = () => { cancelFunction = () => {
audioContext.close(); audioContext.close();
keepRunning = false; keepRunning = false;
abortController.abort();
} }
let cartridgeSize = data.byteLength; let cartridgeSize = data.byteLength;
@@ -229,6 +231,24 @@ export default function MicroW8(screen, config = {}) {
let nextFrame = startTime; let nextFrame = startTime;
audioNode.connect(audioContext.destination); audioNode.connect(audioContext.destination);
let isPaused = false;
let pauseTime = startTime;
let updateVisibility = isVisible => {
let now = Date.now();
if(isVisible) {
isPaused = false;
audioContext.resume();
startTime += now - pauseTime;
} else {
isPaused = true;
audioContext.suspend();
pauseTime = now;
}
};
window.addEventListener('focus', () => updateVisibility(true), { signal: abortController.signal });
window.addEventListener('blur', () => updateVisibility(false), { signal: abortController.signal });
updateVisibility(document.hasFocus());
function mainloop() { function mainloop() {
if (!keepRunning) { if (!keepRunning) {
@@ -238,7 +258,7 @@ export default function MicroW8(screen, config = {}) {
try { try {
let now = Date.now(); let now = Date.now();
let restart = false; let restart = false;
if (now >= nextFrame) { if (now >= nextFrame && !isPaused) {
let gamepads = navigator.getGamepads(); let gamepads = navigator.getGamepads();
let gamepad = 0; let gamepad = 0;
for (let i = 0; i < 4; ++i) { for (let i = 0; i < 4; ++i) {