diff --git a/ui/ceval/src/ctrl.ts b/ui/ceval/src/ctrl.ts index 4a2c3b3a67..308f459b9e 100644 --- a/ui/ceval/src/ctrl.ts +++ b/ui/ceval/src/ctrl.ts @@ -46,6 +46,12 @@ function median(values: number[]): number { return values.length % 2 ? values[half] : (values[half - 1] + values[half]) / 2.0; } +function enabledAfterDisable() { + const enabledAfter = lichess.tempStorage.get('ceval.enabled-after'); + const disable = lichess.storage.get('ceval.disable'); + return !disable || enabledAfter === disable; +} + export default function(opts: CevalOpts): CevalCtrl { const storageKey = (k: string) => { return opts.storageKeyPrefix ? `${opts.storageKeyPrefix}.${k}` : k; @@ -81,7 +87,7 @@ export default function(opts: CevalOpts): CevalCtrl { const infinite = storedProp('ceval.infinite', false); let curEval: Tree.ClientEval | null = null; const allowed = prop(true); - const enabled = prop(false); + const enabled = prop(opts.possible && allowed() && enabledAfterDisable()); let started: Started | false = false; let lastStarted: Started | false = false; // last started object (for going deeper even if stopped) const hovering = prop(null); @@ -135,7 +141,7 @@ export default function(opts: CevalOpts): CevalCtrl { npsRecorder(ev); curEval = ev; opts.emit(ev, work); - if (ev.fen !== lastEmitFen) { + if (ev.fen !== lastEmitFen && enabledAfterDisable()) { // amnesty while auto disable not processed lastEmitFen = ev.fen; lichess.storage.fire('ceval.fen', ev.fen); } @@ -150,7 +156,7 @@ export default function(opts: CevalOpts): CevalCtrl { const start = (path: Tree.Path, steps: Step[], threatMode: boolean, deeper: boolean) => { - if (!enabled() || !opts.possible) return; + if (!enabled() || !opts.possible || !enabledAfterDisable()) return; isDeeper(deeper); const maxD = effectiveMaxDepth(); @@ -190,6 +196,10 @@ export default function(opts: CevalOpts): CevalCtrl { } } + // Notify all other tabs to disable ceval. + lichess.storage.fire('ceval.disable'); + lichess.tempStorage.set('ceval.enabled-after', lichess.storage.get('ceval.disable')!); + pool.start(work); started = { @@ -238,7 +248,14 @@ export default function(opts: CevalOpts): CevalCtrl { toggle() { if (!opts.possible || !allowed()) return; stop(); - enabled(!enabled()); + if (!enabled() && !document.hidden) { + const disable = lichess.storage.get('ceval.disable'); + if (disable) lichess.tempStorage.set('ceval.enabled-after', disable); + enabled(true); + } else { + lichess.tempStorage.set('ceval.enabled-after', ''); + enabled(false); + } }, curDepth: () => curEval ? curEval.depth : 0, effectiveMaxDepth, diff --git a/ui/ceval/src/main.ts b/ui/ceval/src/main.ts index ea4dabe3eb..1added2a3c 100644 --- a/ui/ceval/src/main.ts +++ b/ui/ceval/src/main.ts @@ -9,7 +9,7 @@ export { ctrl, view, winningChances }; // stop when another tab starts. Listen only once here, // as the ctrl can be instantiated several times. // gotta do the click on the toggle to have it visually change. -lichess.storage.make('ceval.disable').listen(_ => { +lichess.storage.make('ceval.disable').listen(() => { const toggle = document.getElementById('analyse-toggle-ceval') as HTMLInputElement | undefined; if (toggle?.checked) toggle.click(); }); diff --git a/ui/ceval/src/pool.ts b/ui/ceval/src/pool.ts index 4f72dd3eae..70ff44a788 100644 --- a/ui/ceval/src/pool.ts +++ b/ui/ceval/src/pool.ts @@ -144,7 +144,6 @@ export class Pool { } start(work: Work): void { - lichess.storage.fire('ceval.disable'); // disable on all other tabs this.getWorker().then(function(worker) { worker.start(work); }).catch(function(error) { diff --git a/ui/round/src/cevalSub.ts b/ui/round/src/cevalSub.ts index fdd01aa575..afd355e277 100644 --- a/ui/round/src/cevalSub.ts +++ b/ui/round/src/cevalSub.ts @@ -17,9 +17,9 @@ export function subscribe(ctrl: RoundController): void { // bots can cheat alright if (ctrl.data.player.user && ctrl.data.player.user.title === 'BOT') return; - // Disable ceval. Unless this game is loaded directly on a position being - // analysed, there is plenty of time (7 moves, in most cases) for this to - // take effect. + // Notify tabs to disable ceval. Unless this game is loaded directly on a + // position being analysed, there is plenty of time (7 moves, in most cases) + // for this to take effect. lichess.storage.fire('ceval.disable'); lichess.storage.make('ceval.fen').listen(e => {