Skip to content

Commit b969c5a

Browse files
committed
Use audio API instead of audio elements
1 parent afa92f4 commit b969c5a

6 files changed

Lines changed: 273 additions & 127 deletions

File tree

cypress/e2e/actions/play.spec.js

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,6 @@ context ('Play', function () {
66
cy.window ().its ('Monogatari.default').as ('monogatari');
77
});
88

9-
it ('Plays music correctly', function () {
10-
this.monogatari.setting ('TypeAnimation', false);
11-
this.monogatari.script ({
12-
'Start': [
13-
'Zero',
14-
'play music theme',
15-
'One',
16-
]
17-
});
18-
19-
cy.start ();
20-
21-
cy.wrap (this.monogatari).invoke ('state', 'music').should ('be.empty');
22-
cy.wrap (this.monogatari).invoke ('history', 'music').should ('be.empty');
23-
cy.wrap (this.monogatari).invoke ('mediaPlayers', 'music').should ('have.length', 0);
24-
25-
cy.get ('text-box').contains ('Zero');
26-
27-
cy.proceed ();
28-
29-
cy.wrap (this.monogatari).invoke ('state', 'music').should ('deep.equal', [{ statement: 'play music theme', paused: false }]);
30-
cy.wrap (this.monogatari).invoke ('history', 'music').should ('deep.equal', ['play music theme']);
31-
cy.wrap (this.monogatari).invoke ('mediaPlayers', 'music').should ('have.length', 1);
32-
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('theme.paused').should ('equal', false);
33-
34-
cy.get ('text-box').contains ('One');
35-
cy.rollback ();
36-
37-
cy.wrap (this.monogatari).invoke ('state', 'music').should ('be.empty');
38-
cy.wrap (this.monogatari).invoke ('history', 'music').should ('be.empty');
39-
cy.wrap (this.monogatari).invoke ('mediaPlayers', 'music').should ('have.length', 0);
40-
});
41-
429
it ('Plays all music correctly', function () {
4310
this.monogatari.setting ('TypeAnimation', false);
4411
this.monogatari.script ({
@@ -63,6 +30,7 @@ context ('Play', function () {
6330
cy.get ('text-box').contains ('Zero');
6431

6532
cy.proceed ();
33+
cy.wait(100); // Add a small delay to ensure async operations complete
6634

6735
cy.wrap (this.monogatari).invoke ('state', 'music').should ('deep.equal', [{ statement: 'play music theme loop', paused: false }, { statement: 'play music subspace loop', paused: false }]);
6836
cy.wrap (this.monogatari).invoke ('history', 'music').should ('deep.equal', ['play music theme loop', 'play music subspace loop']);
@@ -73,7 +41,16 @@ context ('Play', function () {
7341
cy.get ('text-box').contains ('One');
7442

7543
cy.proceed ();
76-
cy.wrap (this.monogatari).invoke ('state', 'music').should ('deep.equal', [{ statement: 'play music theme loop', paused: true }, { statement: 'play music subspace loop', paused: true }]);
44+
cy.wait(100);
45+
cy.wrap(this.monogatari).invoke('history', 'music').should('have.length', 2);
46+
cy.wrap(this.monogatari).invoke('history', 'music').should('deep.equal', [
47+
'play music theme loop',
48+
'play music subspace loop'
49+
]);
50+
cy.wrap(this.monogatari).invoke('state', 'music').should('deep.equal', [
51+
{ statement: 'play music theme loop', paused: true },
52+
{ statement: 'play music subspace loop', paused: true }
53+
]);
7754
cy.wrap (this.monogatari).invoke ('history', 'music').should ('deep.equal', ['play music theme loop', 'play music subspace loop']);
7855
cy.wrap (this.monogatari).invoke ('mediaPlayers', 'music').should ('have.length', 2);
7956
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('theme.paused').should ('equal', true);
@@ -175,8 +152,8 @@ context ('Play', function () {
175152
cy.start ();
176153

177154
cy.wrap (this.monogatari).invoke ('mediaPlayers', 'music').should ('have.length', 2);
178-
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('theme.volume').should ('equal', 0.25);
179-
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('subspace.volume').should ('equal', 0.075);
155+
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('theme.volume').should ('be.closeTo', 0.25, 0.001);
156+
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('subspace.volume').should ('be.closeTo', 0.075, 0.001);
180157

181158
cy.get ('text-box').contains ('One');
182159
});
@@ -196,17 +173,17 @@ context ('Play', function () {
196173
cy.start ();
197174

198175
cy.wrap (this.monogatari).invoke ('mediaPlayers', 'music').should ('have.length', 2);
199-
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('theme.volume').should ('equal', 0.25);
200-
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('subspace.volume').should ('equal', 0.075);
176+
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('theme.volume').should ('be.closeTo', 0.25, 0.001);
177+
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('subspace.volume').should ('be.closeTo', 0.075, 0.001);
201178

202179
cy.get ('text-box').contains ('One');
203180

204181
cy.get ('[data-component="quick-menu"] [data-open="settings"]').click ();
205182
cy.get ('settings-screen').should ('be.visible');
206183

207184
cy.get('[data-action="set-volume"][data-target="music"]').as('range').invoke('val', 0.7).trigger('mouseover');
208-
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('theme.volume').should ('equal', 0.7);
209-
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('subspace.volume').should ('equal', 0.21);
185+
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('theme.volume').should ('be.closeTo', 0.7, 0.001);
186+
cy.wrap (this.monogatari.mediaPlayers ('music', true)).its ('subspace.volume').should ('be.closeTo', 0.21, 0.001);
210187
});
211188

212189
});

src/actions/Pause.js

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { Action } from './../lib/Action';
2-
32
export class Pause extends Action {
43

54
static matchString ([ action ]) {
@@ -39,32 +38,35 @@ export class Pause extends Action {
3938

4039
didApply () {
4140
const state = {};
41+
const prev = this.engine.state (this.type);
42+
4243
if (this.player instanceof Array) {
43-
state[this.type] = this.engine.state (this.type).map ((s) => {
44-
s.paused = true;
45-
return s;
46-
});
44+
state[this.type] = prev.map((s) => ({
45+
...s,
46+
paused: true
47+
}));
4748
} else {
48-
state[this.type] = [...this.engine.state (this.type).map ((item) => {
49+
state[this.type] = prev.map((item) => {
4950
if (typeof item.statement === 'string') {
50-
const [play, type, media] = item.statement.split (' ');
51-
51+
const [play, type, media] = item.statement.split(' ');
5252
if (media === this.media) {
53-
item.paused = true;
53+
return { ...item, paused: true };
5454
}
55-
return item;
5655
}
5756
return item;
58-
})];
57+
});
5958
}
60-
this.engine.state (state);
61-
return Promise.resolve ({ advance: true });
59+
60+
this.engine.state(state);
61+
62+
return Promise.resolve({ advance: true });
6263
}
6364

6465
willRevert () {
6566
if (this.player) {
6667
return Promise.resolve ();
6768
}
69+
6870
return Promise.reject ('Media player was not defined.');
6971
}
7072

@@ -75,31 +77,33 @@ export class Pause extends Action {
7577
promises.push (player.play ());
7678
}
7779
return Promise.all (promises);
78-
} else {
79-
return this.player.play ();
8080
}
81+
82+
return this.player.play ();
8183
}
8284

8385
didRevert () {
8486
const state = {};
8587
if (this.player instanceof Array) {
86-
state[this.type] = this.engine.state (this.type).map ((s) => {
87-
s.paused = false;
88-
return s;
89-
});
88+
state[this.type] = this.engine.state (this.type).map ((s) => ({
89+
...s,
90+
paused: false
91+
}));
9092
} else {
91-
state[this.type] = [...this.engine.state (this.type).map ((item) => {
93+
state[this.type] = this.engine.state (this.type).map ((item) => {
9294
if (typeof item.statement === 'string') {
9395
const [play, type, media] = item.statement.split (' ');
9496

9597
if (media === this.media) {
96-
item.paused = false;
98+
return { ...item, paused: false };
9799
}
98-
return item;
99100
}
100-
})];
101+
return item;
102+
});
101103
}
104+
102105
this.engine.state (state);
106+
103107
return Promise.resolve ({ advance: true, step: true });
104108
}
105109
}

0 commit comments

Comments
 (0)