diff --git a/.changeset/lemon-steaks-sing.md b/.changeset/lemon-steaks-sing.md new file mode 100644 index 0000000000..71561c4a29 --- /dev/null +++ b/.changeset/lemon-steaks-sing.md @@ -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. diff --git a/packages/effect/src/internal/clock.ts b/packages/effect/src/internal/clock.ts index 53b1ce5e72..49d18e5640 100644 --- a/packages/effect/src/internal/clock.ts +++ b/packages/effect/src/internal/clock.ts @@ -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))