Skip to content

Commit 523e46b

Browse files
Mario T. LanzaMario T. Lanza
authored andcommitted
improved tick precision
1 parent d006e56 commit 523e46b

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

src/reactives/types/observable/concrete.js

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,18 +165,34 @@ function time(){
165165
return _.date().getTime();
166166
}
167167

168-
function tick2(interval, f){
168+
function tick3(interval, frame = 0, f = time){
169169
return observable(function(observer){
170-
const iv = setInterval(function(){
171-
pub(observer, f());
172-
}, interval);
170+
const seed = performance.now();
171+
const target = seed + frame * interval;
172+
const self = {seed, target, frame, stopped: false};
173+
function callback(){
174+
self.offage = performance.now() - self.target;
175+
if (self.offage >= 0) {
176+
pub(observer, f(self));
177+
self.frame += 1;
178+
self.target = self.seed + self.frame * interval;
179+
}
180+
const delay = Math.abs(Math.round(Math.min(0, self.offage), 0));
181+
self.stopped || setTimeout(callback, delay);
182+
}
183+
setTimeout(callback, 0);
173184
return function(){
174-
clearInterval(iv);
185+
self.stopped = true;
186+
complete(observer);
175187
}
176188
});
177189
}
178190

179-
const tick = _.overload(null, tick2(?, time), tick2);
191+
function tick2(interval, f = time){
192+
return tick3(interval, 0, f);
193+
}
194+
195+
const tick = _.overload(null, tick2, tick2, tick3);
180196

181197
function when2(interval, f){
182198
return seed(f, tick(interval, f));

0 commit comments

Comments
 (0)