Skip to content

Commit

Permalink
FView.defer loop with max loop process time
Browse files Browse the repository at this point in the history
  • Loading branch information
gadicc committed Jun 4, 2015
1 parent 6ed7270 commit 0a9dc0b
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 10 deletions.
35 changes: 35 additions & 0 deletions lib/defer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
var DEFER_RUN_THRESHOLD = 2;

/*
* Rather than just `setTimeout(f, 0)`, let's assume defer'd functions don't
* need to be run "as soon as possible". We can loop through them once per
* frame, and abort the queue if we exceed the given threshold. Inspired by
* famous 0.3.
*/

var queue = [];
var timer = typeof performance === 'object' ? performance : Date;

FView.defer = function(func) {
queue.push(func);
};

// avoid garbage collection
var start, i, len, diff;

var deferRunObj = {
onUpdate: function() {
start = timer.now();
len = queue.length;

for (i = 0; i < len && timer.now() - start < DEFER_RUN_THRESHOLD; i++)
queue[i]();

queue.splice(0, i);
FamousEngine.requestUpdateOnNextTick(deferRunObj);
}
};

FView.ready(function() {
FamousEngine.requestUpdate(deferRunObj);
});
15 changes: 6 additions & 9 deletions lib/meteor/setimmediate.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
FView.ready(function() {
var clock = FamousEngine.getClock();
// log.debug('Overriding Meteor._setImmediate() to use famous clock');
// log.debug('Overriding Meteor._setImmediate() to use FView.defer');

var setImmediate = function (fn) {
clock.setTimeout(fn, 0);
};
var setImmediate = function (fn) {
FView.defer(fn);
};

setImmediate.implementation = 'famous';
Meteor._setImmediate = setImmediate;
});
setImmediate.implementation = 'famous';
Meteor._setImmediate = setImmediate;
2 changes: 1 addition & 1 deletion lib/meteor/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ FView.ready(function() {
return clock.clearTimer(x);
},
defer: function (f) {
Meteor._setImmediate(bindAndCatch("defer callback", f));
FView.defer(bindAndCatch("defer callback", f));
}
});
});
1 change: 1 addition & 0 deletions package.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function common(api) {
'lib/famous-views.js',
'lib/utilities.js',
'lib/meteorFamousView.js',
'lib/defer.js',

'lib/wrappers/wrap.js',
'lib/wrappers/Nodes.js',
Expand Down

0 comments on commit 0a9dc0b

Please sign in to comment.