Skip to content

Commit

Permalink
Micro optimisation for playback position binding
Browse files Browse the repository at this point in the history
Request animation frame is now no longer always on effectively giving the app an idle state as to always running at MAX fps.
Also, fixed up polyfill and commented out a debugging line in ngSlider
  • Loading branch information
atruskie committed Jan 23, 2014
1 parent e60fe9f commit 97796c5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/common/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ if (!Array.prototype.filter) {
}

if (!window.requestAnimationFrame) {
window.requestAnimationFrame = function requestAnimationFrame(callback, element) {
window.requestAnimationFrame = function requestAnimationFrame(callback) {
var
currTime = new Date().getTime(),
timeToCall = Math.max(0, 16 - (currTime - lastTime)),
Expand Down
4 changes: 2 additions & 2 deletions src/components/directives/input[type=range].js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ bawds.directive("ngSlider", function () {
return;
}

console.debug("ngSlider:updateScope: Calling apply!", element.value);
//console.debug("ngSlider:updateScope: Calling apply!", element.value);

scope.$apply(function () {
scope[attrs.ngSlider] = element.value;
});
Expand Down
21 changes: 16 additions & 5 deletions src/components/directives/ngAudio.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ bawds.directive('ngAudio', ['$parse', function ($parse) {
'mozaudioavailable': undefined,
'pause': updateState,
'play': updateState,
'playing': updateState,
'playing': function() {
// restart request animation frame
audioElementPositionRAF();
updateState();
},
'progress': undefined,
'ratechange': undefined,
'seeked': undefined,
Expand All @@ -107,9 +111,7 @@ bawds.directive('ngAudio', ['$parse', function ($parse) {

// position binding - reverse (element to model)
// TODO: we can optimise this, it does not always need to be running
window.requestAnimationFrame(function audioElementPositionRAF() {
// need to request each new frame
window.requestAnimationFrame(audioElementPositionRAF);
function audioElementPositionRAF() {
if (attributes.ngAudio) {
var target = scope.$eval(attributes.ngAudio);
if (target) {
Expand All @@ -121,7 +123,16 @@ bawds.directive('ngAudio', ['$parse', function ($parse) {
}
}
}
}, elements[0]);

// optimisation - do not request a new frame if element is paused
// requires loop to be restarted on play event
if (element.paused) {
return;
}

// need to request each new frame
window.requestAnimationFrame(audioElementPositionRAF);
}

}
};
Expand Down

0 comments on commit 97796c5

Please sign in to comment.