convert async/await to promise chain

This commit is contained in:
Corey Frang 2018-06-20 14:51:48 -04:00 committed by Michael "Z" Goddard
parent 1d5d6bfbb2
commit cdd35b5296
No known key found for this signature in database
GPG key ID: 762CD40DD5349872

View file

@ -108,51 +108,61 @@ tap.test('SoundPlayer', suite => {
t.end(); t.end();
}); });
suite.test('play while playing', async t => { suite.test('play while playing', t => {
t.plan(15); t.plan(15);
const log = []; const log = [];
soundPlayer.play(); soundPlayer.play();
soundPlayer.finished().then(() => log.push('play 1 finished')); soundPlayer.finished().then(() => log.push('play 1 finished'));
soundPlayer.connect(audioEngine); soundPlayer.connect(audioEngine);
let oldPlayerNode;
await Promise.resolve(); return Promise.resolve()
.then(() => {
audioContext.$processTo(0.005); audioContext.$processTo(0.005);
t.equal(soundPlayer.outputNode.$state, 'PLAYING'); t.equal(soundPlayer.outputNode.$state, 'PLAYING');
const oldPlayerNode = soundPlayer.outputNode; oldPlayerNode = soundPlayer.outputNode;
soundPlayer.play(); soundPlayer.play();
soundPlayer.finished().then(() => log.push('play 2 finished')); soundPlayer.finished().then(() => log.push('play 2 finished'));
// wait for a micro-task loop to fire our previous events // wait for a micro-task loop to fire our previous events
await Promise.resolve(); return Promise.resolve();
t.equal(log[0], 'play 1 finished'); })
t.notEqual(soundPlayer.outputNode, oldPlayerNode, 'created new player node'); .then(() => {
t.equal(help.engineInputs.length, 2, 'there should be 2 players connected'); t.equal(log[0], 'play 1 finished');
t.equal(oldPlayerNode.$state, 'PLAYING'); t.notEqual(soundPlayer.outputNode, oldPlayerNode, 'created new player node');
t.equal(soundPlayer.outputNode.$state, 'PLAYING');
t.equal(help.engineInputs[0].gain.value, 1, 'old sound connectect to gain node with volume 1');
audioContext.$processTo(audioContext.currentTime + 0.001); t.equal(help.engineInputs.length, 2, 'there should be 2 players connected');
t.notEqual(help.engineInputs[0].gain.value, 1, t.equal(oldPlayerNode.$state, 'PLAYING');
t.equal(soundPlayer.outputNode.$state, 'PLAYING');
t.equal(help.engineInputs[0].gain.value, 1, 'old sound connectect to gain node with volume 1');
audioContext.$processTo(audioContext.currentTime + 0.001);
t.notEqual(help.engineInputs[0].gain.value, 1,
'old sound connected to gain node which will fade'); 'old sound connected to gain node which will fade');
audioContext.$processTo(audioContext.currentTime + audioEngine.DECAY_TIME + 0.001); audioContext.$processTo(audioContext.currentTime + audioEngine.DECAY_TIME + 0.001);
t.equal(soundPlayer.outputNode.$state, 'PLAYING'); t.equal(soundPlayer.outputNode.$state, 'PLAYING');
t.equal(oldPlayerNode.$state, 'FINISHED'); t.equal(oldPlayerNode.$state, 'FINISHED');
t.equal(help.engineInputs[0].gain.value, 0, 'faded old sound to 0'); t.equal(help.engineInputs[0].gain.value, 0, 'faded old sound to 0');
t.equal(log.length, 1); t.equal(log.length, 1);
audioContext.$processTo(audioContext.currentTime + 0.2); audioContext.$processTo(audioContext.currentTime + 0.2);
await Promise.resolve(); // wait for a micro-task loop to fire our previous events
t.equal(log[1], 'play 2 finished'); return Promise.resolve();
t.equal(help.engineInputs.length, 1, 'old sound disconneted itself after done'); })
t.equal(log.length, 2); .then(() => {
t.end(); t.equal(log[1], 'play 2 finished');
t.equal(help.engineInputs.length, 1, 'old sound disconneted itself after done');
t.equal(log.length, 2);
t.end();
});
}); });
suite.end(); suite.end();