Coder Social home page Coder Social logo

Comments (8)

CreativeTechGuy avatar CreativeTechGuy commented on July 17, 2024 1

Sorry, I used some loose language. To keep it simple, the current performance.now() should be passed to the callback when it is called. All of the handling of DOMHisResTimeStamp is already implemented in this library. It is currently used for hrtime() and performance.now(). So nothing fundamentally new needs to be added to make this work. Time origin is already being tracked and updated properly by the library. The only missing piece is that it isn't being passed as the argument and instead a different time is.

from fake-timers.

fatso83 avatar fatso83 commented on July 17, 2024

There might be a bug here, but I think the description here is wrong. You say

The callback passed to requestAnimationFrame should receive an argument which is the number of seconds since time origin

This is not in line with the docs you refer to:

"The callback function is passed one single argument, a DOMHighResTimeStamp similar to the one returned by performance.now(), indicating the point in time when requestAnimationFrame() starts to execute callback functions.

A DOMHighResTimeStamp is not "the number of seconds since time origin", but a millisecond value with high precision.

The DOMHighResTimeStamp type is a double and is used to store a time value in milliseconds.

So it is a high-resolution value in milliseconds denoting the number of secons since time origin, which for most reasons, is the start of the page load.

How would you define time origin in the context of our tests? Should we perhaps just set it to 0 when installing the clock and tick it along with the system clock (or make it a synthetic getter that deduces its value from the system clock)?

from fake-timers.

CreativeTechGuy avatar CreativeTechGuy commented on July 17, 2024

I think the approach of determining the args ahead of time won't work when considering #452. The current implementation of requestAnimationFrame pre-calculates what the time to be passed will be. But if more time has elapsed than 16ms then the wrong value will be passed to the callback. Instead of pre-calculating the args, when calling the callback, at that point it should pass the current value of performance.now(), whatever it happens to be at that time.

So similar to how doTick sets clock.now here it should also set performance.now's value. Then in callTimer here it'd have a conditional to check if the timer is an animation like the following:

if (timer.animation) {
    timer.func.apply(null, [performancePresent ? clock.performance.now() : clock.now]);
} else {
    timer.func.apply(null, timer.args);
}

from fake-timers.

CreativeTechGuy avatar CreativeTechGuy commented on July 17, 2024

I believe this is the necessary diff to resolve this issue. Let me know if you'd like me to create a PR! 😄

diff --git a/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js b/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js
index a8f69fc..c7afe20 100644
--- a/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js
+++ b/node_modules/@sinonjs/fake-timers/src/fake-timers-src.js
@@ -742,7 +742,11 @@ function withGlobal(_global) {
         }
 
         if (typeof timer.func === "function") {
-            timer.func.apply(null, timer.args);
+            if (timer.animation) {
+                timer.func.apply(null, [performancePresent ? clock.performance.now() : clock.now]);
+            } else {
+                timer.func.apply(null, timer.args);
+            }
         } else {
             /* eslint no-eval: "off" */
             const eval2 = eval;
@@ -1226,7 +1230,6 @@ function withGlobal(_global) {
             const result = addTimer(clock, {
                 func: func,
                 delay: getTimeToNextFrame(),
-                args: [clock.now + getTimeToNextFrame()],
                 animation: true,
             });
 

from fake-timers.

fatso83 avatar fatso83 commented on July 17, 2024

That seems great. Please push a PR 👍

from fake-timers.

CreativeTechGuy avatar CreativeTechGuy commented on July 17, 2024

@fatso83 Should I include the changes from the related issue (#452) here in this PR too since they go together?

from fake-timers.

fatso83 avatar fatso83 commented on July 17, 2024

Do this one first. The other one is a type of breaking, unexpected API change and I am not sure how we like that to look. While this change can be merged now independently

from fake-timers.

CreativeTechGuy avatar CreativeTechGuy commented on July 17, 2024

Okay! This PR is out!

(And for the record the other one is a pure addition which has no impact on anything that already exists so it isn't breaking. But yeah let's continue the discussion on that other thread.)

from fake-timers.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.