Skip to content

Commit

Permalink
Fixed bug when requesting termination before pause
Browse files Browse the repository at this point in the history
  • Loading branch information
drpepper committed Aug 29, 2024
1 parent 13b6587 commit f2f409f
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 172 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "booyah",
"version": "4.0.2",
"version": "4.0.3",
"description": "HTML5 game engine",
"scripts": {
"build": "tsc",
Expand Down
10 changes: 10 additions & 0 deletions src/chip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,16 @@ export abstract class Composite extends ChipBase {
public pause(tickInfo: TickInfo): void {
super.pause(tickInfo);

// Terminate any child chips that requested it
for (const [childId, childChipInfo] of Object.entries(
this._childChipInfos,
)) {
if (childChipInfo.chip.chipState === "requestedTermination") {
this._terminateChildChip(childId);
}
}

// Tell child chips to pause
for (const childChipInfo of Object.values(this._childChipInfos)) {
childChipInfo.chip.pause(tickInfo);
}
Expand Down
18 changes: 9 additions & 9 deletions src/running.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Runner {
*/
constructor(
private readonly _rootChipResolvable: chip.ChipResolvable,
options?: Partial<RunnerOptions>
options?: Partial<RunnerOptions>,
) {
this._options = chip.fillInOptions(options, new RunnerOptions());
}
Expand All @@ -51,14 +51,14 @@ export class Runner {
if (document.visibilityState === "hidden") {
if (this._runningStatus !== "running") return;

console.log("Runner: pausing");
console.debug("Booyah Runner: pausing");
this._runningStatus = "paused";

this._rootChip.pause(this._makeTickInfo());
} else {
if (this._runningStatus !== "paused") return;

console.log("Runner: resuming");
console.debug("Booyah Runner: resuming");

this._runningStatus = "running";
this._rootChip.resume(this._makeTickInfo());
Expand All @@ -68,7 +68,7 @@ export class Runner {
};
document.addEventListener(
"visibilitychange",
this._visibilityChangeHandler
this._visibilityChangeHandler,
);

this._runningStatus = "running";
Expand All @@ -85,7 +85,7 @@ export class Runner {
this._rootChip.activate(
tickInfo,
this._rootContext,
this._options.inputSignal
this._options.inputSignal,
);

requestAnimationFrame(() => this._onTick());
Expand All @@ -98,7 +98,7 @@ export class Runner {

const timeStamp = performance.now();
const timeSinceLastTick = this._clampTimeSinceLastTick(
timeStamp - this._lastTimeStamp
timeStamp - this._lastTimeStamp,
);

const tickInfo: chip.TickInfo = {
Expand All @@ -110,7 +110,7 @@ export class Runner {

document.removeEventListener(
"visibilitychange",
this._visibilityChangeHandler
this._visibilityChangeHandler,
);
delete this._visibilityChangeHandler;
}
Expand Down Expand Up @@ -161,7 +161,7 @@ export class Runner {
tickInfo,
this._rootContext,
chip.makeSignal("afterReload"),
reloadMemento
reloadMemento,
);
});
}
Expand All @@ -181,7 +181,7 @@ export class Runner {
if (this._options.minFps >= 0) {
timeSinceLastTick = Math.min(
timeSinceLastTick,
1000 / this._options.minFps
1000 / this._options.minFps,
);
}

Expand Down
Loading

0 comments on commit f2f409f

Please sign in to comment.