Skip to content

Commit 6322d5a

Browse files
authored
Merge pull request #2893 from Brain-up/fix-audio-pause
UI: fix pause in task audio playing (with glm-5.1 model help)
2 parents 92b150e + 96363f0 commit 6322d5a

2 files changed

Lines changed: 41 additions & 3 deletions

File tree

frontend/app/services/studying-timer.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import Service from '@ember/service';
1+
import Service, { service } from '@ember/service';
22
import { action } from '@ember/object';
33
import config from 'brn/config/environment';
44
import { tracked } from '@glimmer/tracking';
55
import { isTesting } from '@embroider/macros';
66
import type IdleJs from 'idle-js';
7+
import type AudioService from './audio';
78

89
export interface TimerInstance {
910
isStarted: boolean;
@@ -13,6 +14,7 @@ export interface TimerInstance {
1314
}
1415

1516
export default class StudyingTimerService extends Service {
17+
@service('audio') declare audio: AudioService;
1618
willDestroy() {
1719
super.willDestroy();
1820
this.idleWatcher && this.idleWatcher.stop();
@@ -63,6 +65,15 @@ export default class StudyingTimerService extends Service {
6365
this.isPaused = false;
6466
}
6567
@action
68+
maybeIdlePause() {
69+
// Pause cascades into audio.stop() via task-player.onPauseStateChanged,
70+
// which would interrupt exercises whenever the user stops moving the mouse.
71+
if (this.audio.isPlaying) {
72+
return;
73+
}
74+
this.pause();
75+
}
76+
@action
6677
async startIdleWatcher() {
6778
if (isTesting()) {
6879
return;
@@ -75,7 +86,7 @@ export default class StudyingTimerService extends Service {
7586
this.idleWatcher = new IdleJs({
7687
idle: timerInstance.idleTimeout || config.idleTimeout,
7788
onIdle() {
78-
player.pause();
89+
player.maybeIdlePause();
7990
},
8091
onActive() {
8192
timerInstance.relaunchStartedTimer();
Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,39 @@
11
import { module, test } from 'qunit';
22
import { setupTest } from 'ember-qunit';
3+
import Service from '@ember/service';
34

45
module('Unit | Service | studying-timer', function (hooks) {
56
setupTest(hooks);
67

7-
// Replace this with your real tests.
88
test('it exists', function (assert) {
99
let service = this.owner.lookup('service:studying-timer');
1010
assert.ok(service);
1111
});
12+
13+
module('maybeIdlePause', function () {
14+
function withAudio(owner, playing) {
15+
owner.register('service:audio', class extends Service { isPlaying = playing; });
16+
return owner.lookup('service:studying-timer');
17+
}
18+
19+
test('pauses when audio is not playing', function (assert) {
20+
const timer = withAudio(this.owner, false);
21+
timer.maybeIdlePause();
22+
assert.true(timer.isPaused);
23+
});
24+
25+
test('does not pause when audio is playing', function (assert) {
26+
const timer = withAudio(this.owner, true);
27+
timer.maybeIdlePause();
28+
assert.false(timer.isPaused);
29+
});
30+
31+
test('user pause still works while audio plays', function (assert) {
32+
// Regression guard: only maybeIdlePause should defer to audio state;
33+
// direct pause() (timer button click) must remain unconditional.
34+
const timer = withAudio(this.owner, true);
35+
timer.pause();
36+
assert.true(timer.isPaused);
37+
});
38+
});
1239
});

0 commit comments

Comments
 (0)