Skip to content

Commit

Permalink
Fix ShiftCounter to not start until the game does
Browse files Browse the repository at this point in the history
  • Loading branch information
Todd Gardner committed Sep 13, 2024
1 parent 7a6d04f commit 1a895ba
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
46 changes: 25 additions & 21 deletions src/components/ShiftCounter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,33 @@ export class ShiftCounter extends Component<any, ShiftCounterState> {

componentDidMount(): void {

if (isSafari()) {
console.info("approximating layout shift value on Safari");
GameTimer.onTick((tick) => {
let cls = this.state.cls;
cls = cls + ((getRandomInteger(5000, 10000) + (tick)) / 10000);
this.setState({ cls });
GameController.cls = cls;
})
}
else {
let cls = this.state.cls;
this.observer = new PerformanceObserver((list: PerformanceObserverEntryList) => {
Promise.resolve().then(() => {
(list.getEntries() as CustomLayoutShift[]).forEach((entry) => {
cls += entry.value
});
GameController.onStart(() => {

if (isSafari()) {
console.info("approximating layout shift value on Safari");
GameTimer.onTick((tick) => {
let cls = this.state.cls;
cls = cls + ((getRandomInteger(5000, 10000) + (tick)) / 10000);
this.setState({ cls });
GameController.cls = cls;
})
this.setState({ cls });
GameController.cls = cls;
});
this.observer.observe({ type: "layout-shift", buffered: false });
}
}
else {
let cls = this.state.cls;
this.observer = new PerformanceObserver((list: PerformanceObserverEntryList) => {
Promise.resolve().then(() => {
(list.getEntries() as CustomLayoutShift[]).forEach((entry) => {
cls += entry.value
});
this.setState({ cls });
})
this.setState({ cls });
GameController.cls = cls;
});
this.observer.observe({ type: "layout-shift", buffered: false });
}
})

}

componentWillUnmount(): void {
Expand Down
6 changes: 6 additions & 0 deletions src/controllers/GameController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class _GameController {
setState!: (state: any) => void;
cls: number = 0;
countdownWarning: boolean = false;
onStartCbs: (() => void)[] = [];

init(getState: () => GameState, setState: (state: any) => void) {

Expand Down Expand Up @@ -77,6 +78,7 @@ class _GameController {
}
})
GameTimer.start();
this.onStartCbs.forEach(cb => cb());
}

stop() {
Expand Down Expand Up @@ -113,6 +115,10 @@ class _GameController {
setLocalStorage(PLAYER_STORAGE_KEY, savedPlayerData);
}

onStart(cb: () => void): void {
this.onStartCbs.push(cb);
}

private countdown(i: number): Promise<void> {
return new Promise((res) => {
this.setState({ countdown: i });
Expand Down

0 comments on commit 1a895ba

Please sign in to comment.