Comments (8)
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.
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.
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.
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.
That seems great. Please push a PR 👍
from fake-timers.
@fatso83 Should I include the changes from the related issue (#452) here in this PR too since they go together?
from fake-timers.
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.
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)
- Test failing since Node 18.8.0 HOT 2
- The `shouldAdvanceTime` option seems to cause Jest Test environment to clear during the test HOT 4
- `runToLastAsync` doing infinite loop when `now` is specified to setup clock
- [Feature] Jump forward in time to simulate throttled timers HOT 22
- [Feature]: Speed up all fake timers HOT 12
- [Feature]: In Node, hijack perf_hooks.performance in addition to global.performance HOT 5
- TypeError Since Node 19 HOT 4
- NodeJS "timers" module is not mocked HOT 17
- Calls to `tick()` do not cause all setTimeouts to fire in specific scenarios. HOT 6
- Mock "timers/promise" module HOT 2
- NodeJS `timers` module mock causing tests to timeout in JSDom HOT 3
- process.hrtime gives incorrect results HOT 4
- Leave global scope untouched HOT 5
- How should `shouldAdvanceTime` work together with `Date`? HOT 1
- runAllAsync and runToLastAsync do not run code in the microtask queue HOT 5
- Cleanly switch to real time and back again to fake time while testing HOT 2
- Skypack bundle cannot be loaded in the browser HOT 5
- `install({ toFake: ["setImmediate"] })` fails when `setImmediate` is not available in context (such as browser environment) HOT 5
- Support performance.measure method
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from fake-timers.