Skip to content

Commit

Permalink
feat(preload): support itako.options.read.preload
Browse files Browse the repository at this point in the history
  • Loading branch information
59naga committed May 14, 2016
1 parent 46f5833 commit 04e6aef
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ rules:
complexity:
- 1
- max: 10

no-param-reassign:
- 1
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"conventional-github-releaser": "^1.1.1",
"eslint": "^2.7.0",
"eslint-config-airbnb": "^7.0.0",
"itako": "^0.1.0",
"itako": "^0.3.0",
"mocha": "^2.4.5",
"mock-audio-element": "^0.0.0-beta.2",
"npm-statement": "^0.0.0",
Expand Down
45 changes: 38 additions & 7 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
// dependencies
import Promise from 'bluebird';

// @class ItakoAudioReaderAudioElement
Expand Down Expand Up @@ -52,20 +51,52 @@ export default class ItakoAudioReaderAudioElement {
return token;
}

return this.play(token.value.url || token.value, token.options)
return this.play(token.meta.preload || token.value.url || token.value, token.options)
.then((audio) => token.setMeta('audio', audio));
}

/**
* @method createAudio
* @param {token} token - a itako-token instance
* @returns {boolean} - returns true if preloaded
*/
preload(token) {
if (token.type !== this.type) {
return false;
}

token.setMeta('preloadStart', Date.now());
token.setMeta('preload', this.createAudio(token.value.url || token.value));
token.meta.preload.then(() => {
token.setMeta('preloadEnd', Date.now());
});
return true;
}

/**
* @method createAudio
* @param {string} url - an audio url
* @returns {promise<audio>} - returns audio instance
*/
createAudio(url) {
return new Promise((resolve, reject) => {
const audio = new Audio;
audio.src = url;

audio.addEventListener('canplaythrough', () => resolve(audio));
audio.addEventListener('error', (event) => reject(event.target.error));
});
}

/**
* @method play
* @param {url} url - an audio url
* @param {string|promise} url - an audio url or promise
* @param {object} [options={}] - a gain/playbackRate options
* @returns {promise} - returns the used nodes after playing
*/
play(url, options = {}) {
return new Promise((resolve) => {
const audio = new Audio;
audio.src = url;
const canplaythrough = url instanceof Promise ? url : this.createAudio(url);
return canplaythrough.then((audio) => new Promise((resolve) => {
audio.volume = options.volume || 1;
audio.playbackRate = options.pitch || options.speed || 1;
audio.play();
Expand All @@ -79,6 +110,6 @@ export default class ItakoAudioReaderAudioElement {
});

this.audios.push(audio);
});
}));
}
}
19 changes: 19 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,23 @@ describe('itako-audio-reader-audio-element', () => {
assert(tokens[0].meta.audio.playbackRate === 0.5);
assert(tokens[0].value === text);
});

it('should preload the audio token', async () => {
const reader = new ItakoAudioReaderAudioElement();
const itako = new Itako([reader], [audioTransformer]).setOptions({
read: {
preload: true,
},
});

// request the audio files in before read
const [a, b, c] = await itako.read([
Itako.createToken('audio', 'http://static.edgy.black/beep.mp3'),
Itako.createToken('audio', 'http://static.edgy.black/beep.mp3'),
Itako.createToken('audio', 'http://static.edgy.black/beep.mp3'),
]);
assert(a.meta.reader.name === 'audio-element');
assert(b.meta.preloadStart - a.meta.preloadStart < 100);
assert(c.meta.preloadStart - b.meta.preloadStart < 100);
});
});

0 comments on commit 04e6aef

Please sign in to comment.