Skip to content

Commit

Permalink
improved tick precision
Browse files Browse the repository at this point in the history
  • Loading branch information
Mario T. Lanza authored and Mario T. Lanza committed May 16, 2024
1 parent ca96666 commit de662e8
Showing 1 changed file with 22 additions and 6 deletions.
28 changes: 22 additions & 6 deletions src/reactives/types/observable/concrete.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,18 +165,34 @@ function time(){
return _.date().getTime();
}

function tick2(interval, f){
function tick3(interval, frame = 0, f = time){
return observable(function(observer){
const iv = setInterval(function(){
pub(observer, f());
}, interval);
const seed = performance.now();
const target = seed + frame * interval;
const self = {seed, target, frame, stopped: false};
function callback(){
self.offage = performance.now() - self.target;
if (self.offage >= 0) {
pub(observer, f(self));
self.frame += 1;
self.target = self.seed + self.frame * interval;
}
const delay = Math.abs(Math.round(Math.min(0, self.offage), 0));
self.stopped || setTimeout(callback, delay);
}
setTimeout(callback, 0);
return function(){
clearInterval(iv);
self.stopped = true;
complete(observer);
}
});
}

const tick = _.overload(null, tick2(?, time), tick2);
function tick2(interval, f = time){
return tick3(interval, 0, f);
}

const tick = _.overload(null, tick2, tick2, tick3);

function when2(interval, f){
return seed(f, tick(interval, f));
Expand Down

0 comments on commit de662e8

Please sign in to comment.