Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgraus committed Feb 12, 2020
2 parents 411ebdf + b276996 commit 44301f0
Show file tree
Hide file tree
Showing 11 changed files with 259 additions and 61 deletions.
2 changes: 1 addition & 1 deletion dist/forestguide.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/forestguide.css.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/forestguide.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/js/ActionProcessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class ActionProcessor {
return;
}

this.deactivate();
this._guide = guide;
this._tick = 0;
}
Expand Down
115 changes: 84 additions & 31 deletions src/js/AudioPlayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ export default class AudioPlayer {
this._callbackMapper = new CallbackMapper();
this._audio = null;
this._requestedPlay = false;
this._ignoreNextPauseForStop = false;
this._ignoreNextPause = false;
this._ignoreNextStop = false;
this._urlToLoad = null;
this._urlLoaded = false;
this._isPlaying = false;
}

/**
Expand Down Expand Up @@ -61,7 +63,7 @@ export default class AudioPlayer {
* @private
*/
_loadStartHandler(event) {
if(!this._audio) return null;
if(!this._audio || this.isPlaying()) return null;
this._callbackMapper.trigger('loading');
}

Expand Down Expand Up @@ -125,11 +127,26 @@ export default class AudioPlayer {
if(!this._audio) return null;
if(this._requestedPlay === true) {
this._requestedPlay = false;
this._audio.play();
this._callbackMapper.trigger('play');
this._audio.play().then(function() {
this._urlLoaded = true;
this._callbackMapper.trigger('play');
}.bind(this)).catch(function (exceptionName) {
console.error('Could not play because of an error: ');
switch (exceptionName) {
case 'NotAllowedError':
console.error('The browser does not allow to play the sound. (NotAllowedError)');
break;
case 'NotSupportedError':
console.error('The sound file isn\'t supported (NotSupportedError)');
break;
default:
console.error(exceptionName);
}
})
} else {
this._urlLoaded = true;
this._callbackMapper.trigger('canPlay');
}
this._urlLoaded = true;
this._callbackMapper.trigger('canPlay');
}

/**
Expand Down Expand Up @@ -167,6 +184,7 @@ export default class AudioPlayer {
* @private
*/
_playHandler(event) {
this._isPlaying = true;
if(!this._audio) return null;
this._callbackMapper.trigger('play');
}
Expand All @@ -178,9 +196,10 @@ export default class AudioPlayer {
* @private
*/
_pauseHandler(event) {
this._isPlaying = false;
if(!this._audio) return null;
if(this._ignoreNextPauseForStop) {
this._ignoreNextPauseForStop = false;
if(this._ignoreNextPause) {
this._ignoreNextPause = false;
return;
} //A stop was triggered. That's a pause followed by setDuration = 0. We ignore this pause in such situations
this._callbackMapper.trigger('pause');
Expand All @@ -206,6 +225,7 @@ export default class AudioPlayer {
*/
_endedHandler(event)
{
this._isPlaying = false;
if(!this._audio) return null;
this._callbackMapper.trigger('finish');
}
Expand All @@ -215,10 +235,12 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onLoadedMetaData(callback, args = []) {
this._callbackMapper.on('loadedmetadata', callback, args);
onLoadedMetaData(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('loadedMetaData');
this._callbackMapper.on('loadedMetaData', callback, args);
return this;
}

Expand All @@ -227,9 +249,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onLoading(callback, args = []) {
onLoading(callback, args = [], clearExistingRegistration= false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('loading');
this._callbackMapper.on('loading', callback, args);
return this;
}
Expand All @@ -240,9 +264,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onLoadedData(callback, args = []) {
onLoadedData(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('loadedData');
this._callbackMapper.on('loadedData', callback, args);
return this;
}
Expand All @@ -255,9 +281,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onProgress(callback, args = []) {
onProgress(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('progress');
this._callbackMapper.on('progress', callback, args);
return this;
}
Expand All @@ -268,9 +296,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onDurationChanged(callback, args = []) {
onDurationChanged(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('durationChanged');
this._callbackMapper.on('durationChanged', callback, args);
return this;
}
Expand All @@ -280,9 +310,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onCanPlay(callback, args = []) {
onCanPlay(callback, args = [], clearExistingRegistration= false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('canPlay');
this._callbackMapper.on('canPlay', callback, args);
return this;
}
Expand All @@ -293,9 +325,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onCanPlayTrough(callback, args = []) {
onCanPlayTrough(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('canPlayTrough');
this._callbackMapper.on('canPlayTrough', callback, args);
return this;
}
Expand All @@ -305,9 +339,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onPlay(callback, args = []) {
onPlay(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('play');
this._callbackMapper.on('play', callback, args);
return this;
}
Expand All @@ -317,9 +353,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onPause(callback, args = []) {
onPause(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('pause');
this._callbackMapper.on('pause', callback, args);
return this;
}
Expand All @@ -329,9 +367,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onStopped(callback, args = []) {
onStopped(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('stop');
this._callbackMapper.on('stop', callback, args);
return this;
}
Expand All @@ -341,9 +381,11 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onPlayProgress(callback, args = []) {
onPlayProgress(callback, args = [], clearExistingRegistration= false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('playProgress');
this._callbackMapper.on('playProgress', callback, args);
return this;
}
Expand All @@ -353,13 +395,22 @@ export default class AudioPlayer {
*
* @param callback
* @param args
* @param {boolean} clearExistingRegistration
* @return AudioPlayer
*/
onFinish(callback, args = []) {
onFinish(callback, args = [], clearExistingRegistration = false) {
if(clearExistingRegistration) this._callbackMapper.clearCallbacksForAction('finish');
this._callbackMapper.on('finish', callback, args);
return this;
}

/**
* Clear all callback registrations
*/
clearCallbacks() {
this._callbackMapper.reset();
}

/**
* Clear system resources
*
Expand All @@ -368,7 +419,9 @@ export default class AudioPlayer {
_reset()
{
if(!this._audio) return null;
this._ignoreNextStop = true;
this.stop();
this._ignoreNextStop = false;
this._enableListeners(false);
}

Expand All @@ -380,11 +433,12 @@ export default class AudioPlayer {
*/
stop()
{
if(!this._audio) return null;
this._ignoreNextPauseForStop = true; //Prevent pause event from being processed
if(!this._audio || !this.isPlaying()) return null;
this._ignoreNextPause = true; //Prevent pause event from being processed
this._audio.pause();
this._audio.currentTime = 0;
this._callbackMapper.trigger('stop')
if(!this._ignoreNextStop) this._callbackMapper.trigger('stop');
this._ignoreNextPause = false;
}

/**
Expand Down Expand Up @@ -423,9 +477,12 @@ export default class AudioPlayer {
/**
* Requests to play the sound as soon as it is loaded
*/
play()
play(reference)
{
if(!this._audio) return null;
this._ignoreNextStop = true;
this.stop();
this._ignoreNextStop = false;
this._requestedPlay = true;
if(this._urlLoaded) this._canPlayHandler(null); //Because the url is loaded it won't trigger the _canPlayHandler. So we do it manually. The handler will play the sound.
}
Expand All @@ -436,7 +493,7 @@ export default class AudioPlayer {
* @return {null}
*/
pause() {
if(!this._audio) return null;
if(!this._audio || !this.isPlaying()) return null;
this._audio.pause();
}

Expand Down Expand Up @@ -480,10 +537,6 @@ export default class AudioPlayer {
* @return {boolean}
*/
isPlaying() {
return !!(this._audio
&& this._audio.currentTime > 0
&& !this._audio.paused
&& !this._audio.ended
&& this._audio.readyState > 2);
return this._isPlaying;
}
}
17 changes: 16 additions & 1 deletion src/js/CallbackMapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ export default class CalbackMapper {
* CallbackMapper constructor
*/
constructor() {
this.reset();
}

/**
* Reset the registrations
*/
reset() {
this._callbackMap = {};
this._argsMap = {};
}
Expand Down Expand Up @@ -56,7 +63,15 @@ export default class CalbackMapper {

let callbacksCount = this._callbackMap[action].length;
for(let index = 0; index < callbacksCount; index++) {
this._callbackMap[action][index].apply(this, this._argsMap[action][index]);
this._callbackMap[action][index].apply(this, this._argsMap[action][index].slice());
}
}

/**
* @param {string} action
*/
clearCallbacksForAction(action) {
if(!this._callbackMap.hasOwnProperty(action)) return;
delete this._callbackMap[action];
}
}
7 changes: 4 additions & 3 deletions src/js/Cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ export default class Cookies {
* @param {int|null} days or null for "forever"
*/
set(name, value, days = null) {
let d = new Date;
d.setTime(d.getTime() + ((days) ? 24*60*60*1000*days : '9999999999'));
document.cookie = name + "=" + value + ";path=/;expires=" + d.toGMTString();
let date = new Date();
if(!days) date = new Date(8640000000000000); //maximum possible value
else date.setTime(date.getTime() + (86400000 * days)); //86400000 milliseconds in a day
document.cookie = name + "=" + value + ";path=/;expires=" + date.toGMTString();
}
delete(name) { this.set(name, '', -1); }
}
Loading

0 comments on commit 44301f0

Please sign in to comment.