Skip to content

Commit

Permalink
if performance.timeOrigin is 0, use performance.now() directly in Clo…
Browse files Browse the repository at this point in the history
…ck (#3247)
  • Loading branch information
tim-smart authored Jul 13, 2024
1 parent eb1c4d4 commit ca775ce
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .changeset/lemon-steaks-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"effect": patch
---

if performance.timeOrigin is 0, use performance.now() directly in Clock

This is a workaround for cloudflare, where performance.now() cannot be used in
the global scope to calculate the origin.
2 changes: 2 additions & 0 deletions packages/effect/src/internal/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const performanceNowNanos = (function() {
const bigint1e6 = BigInt(1_000_000)
if (typeof performance === "undefined") {
return () => BigInt(Date.now()) * bigint1e6
} else if (typeof performance.timeOrigin === "number" && performance.timeOrigin === 0) {
return () => BigInt(Math.round(performance.now() * 1_000_000))
}
const origin = (BigInt(Date.now()) * bigint1e6) - BigInt(Math.round(performance.now() * 1_000_000))
return () => origin + BigInt(Math.round(performance.now() * 1_000_000))
Expand Down

0 comments on commit ca775ce

Please sign in to comment.