From c132863920f9b39fa85f373e0c4b791fc08e578d Mon Sep 17 00:00:00 2001 From: Tim Date: Sat, 13 Jul 2024 21:33:08 +1200 Subject: [PATCH] if performance.timeOrigin is 0, use performance.now() directly in Clock --- .changeset/lemon-steaks-sing.md | 8 ++++++++ packages/effect/src/internal/clock.ts | 2 ++ 2 files changed, 10 insertions(+) create mode 100644 .changeset/lemon-steaks-sing.md 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))