Skip to content
This repository has been archived by the owner on Jul 16, 2024. It is now read-only.

Commit

Permalink
Add Scheduler hotfix for Bun (#662)
Browse files Browse the repository at this point in the history
  • Loading branch information
fubhy authored Sep 15, 2023
1 parent 3d5dd6f commit c43d4aa
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-dryers-trade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@effect/io": patch
---

Added `Scheduler` hotfix for Bun
7 changes: 4 additions & 3 deletions src/Scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type { Effect } from "@effect/io/Effect"
import type { RuntimeFiber } from "@effect/io/Fiber"
import type { FiberRef } from "@effect/io/FiberRef"
import * as core from "@effect/io/internal/core"
import * as timeout from "@effect/io/internal/timeout"

/**
* @since 1.0.0
Expand Down Expand Up @@ -106,7 +107,7 @@ export class MixedScheduler implements Scheduler {
*/
private starve(depth = 0) {
if (depth >= this.maxNextTickBeforeTimer) {
setTimeout(() => this.starveInternal(0), 0)
timeout.set(() => this.starveInternal(0), 0)
} else {
Promise.resolve(void 0).then(() => this.starveInternal(depth + 1))
}
Expand Down Expand Up @@ -336,14 +337,14 @@ export const makeBatched = (
* @category constructors
*/
export const timer = (ms: number, shouldYield: Scheduler["shouldYield"] = defaultShouldYield) =>
make((task) => setTimeout(task, ms), shouldYield)
make((task) => timeout.set(task, ms), shouldYield)

/**
* @since 1.0.0
* @category constructors
*/
export const timerBatched = (ms: number, shouldYield: Scheduler["shouldYield"] = defaultShouldYield) =>
makeBatched((task) => setTimeout(task, ms), shouldYield)
makeBatched((task) => timeout.set(task, ms), shouldYield)

/** @internal */
export const currentScheduler: FiberRef<Scheduler> = globalValue(
Expand Down
5 changes: 3 additions & 2 deletions src/internal/clock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { constFalse } from "@effect/data/Function"
import type * as Clock from "@effect/io/Clock"
import type * as Effect from "@effect/io/Effect"
import * as core from "@effect/io/internal/core"
import * as timeout from "@effect/io/internal/timeout"

/** @internal */
const ClockSymbolKey = "@effect/io/Clock"
Expand All @@ -28,12 +29,12 @@ export const globalClockScheduler: Clock.ClockScheduler = {
return constFalse
}
let completed = false
const handle = setTimeout(() => {
const handle = timeout.set(() => {
completed = true
task()
}, millis)
return () => {
clearTimeout(handle)
timeout.clear(handle)
return !completed
}
}
Expand Down
23 changes: 23 additions & 0 deletions src/internal/timeout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Bun currently has a bug where `setTimeout` doesn't behave correctly with a 0ms delay.
*
* @see https://github.com/oven-sh/bun/issues/3333
*/

/** @internal */
const isBun = !!((process as any)?.isBun)

/** @internal */
export const clear: (id: NodeJS.Timeout) => void = isBun ? clearInterval : clearTimeout

/** @internal */
export const set: (fn: () => void, ms: number) => NodeJS.Timeout = isBun ?
(fn: () => void, ms: number) => {
const id = setInterval(() => {
fn()
clearInterval(id)
}, ms)

return id
} :
setTimeout
3 changes: 2 additions & 1 deletion test/Effect/promise.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Effect from "@effect/io/Effect"
import * as timeout from "@effect/io/internal/timeout"

describe.concurrent("Effect", () => {
it("promise - success with AbortSignal", async () => {
Expand All @@ -8,7 +9,7 @@ describe.concurrent("Effect", () => {
aborted = true
})
return new Promise((resolve) => {
setTimeout(() => {
timeout.set(() => {
resolve()
}, 100)
})
Expand Down
3 changes: 2 additions & 1 deletion test/Effect/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as Exit from "@effect/io/Exit"
import * as Fiber from "@effect/io/Fiber"
import * as FiberRef from "@effect/io/FiberRef"
import * as TestClock from "@effect/io/internal/testing/testClock"
import * as timeout from "@effect/io/internal/timeout"
import * as Layer from "@effect/io/Layer"
import * as Request from "@effect/io/Request"
import * as Resolver from "@effect/io/RequestResolver"
Expand Down Expand Up @@ -49,7 +50,7 @@ export const GetNameById = Request.tagged<GetNameById>("GetNameById")

const delay = <R, E, A>(self: Effect.Effect<R, E, A>) =>
Effect.zipRight(
Effect.promise(() => new Promise((r) => setTimeout(() => r(0), 0))),
Effect.promise(() => new Promise((r) => timeout.set(() => r(0), 0))),
self
)

Expand Down
7 changes: 4 additions & 3 deletions test/Effect/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as Effect from "@effect/io/Effect"
import * as timeout from "@effect/io/internal/timeout"
import * as Scheduler from "@effect/io/Scheduler"
import * as it from "@effect/io/test/utils/extend"
import { assert, describe } from "vitest"
Expand All @@ -14,21 +15,21 @@ describe.concurrent("Effect", () => {
0,
Scheduler.makeBatched((runBatch) => {
ps000.push(0)
setTimeout(runBatch, 0)
timeout.set(runBatch, 0)
})
],
[
100,
Scheduler.makeBatched((runBatch) => {
ps100.push(100)
setTimeout(runBatch, 0)
timeout.set(runBatch, 0)
})
],
[
200,
Scheduler.makeBatched((runBatch) => {
ps200.push(200)
setTimeout(runBatch, 0)
timeout.set(runBatch, 0)
})
],
[
Expand Down
11 changes: 6 additions & 5 deletions test/Effect/tryPromise.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import * as Either from "@effect/data/Either"
import * as Effect from "@effect/io/Effect"
import * as timeout from "@effect/io/internal/timeout"

describe.concurrent("Effect", () => {
it("tryPromise - success, no catch, no AbortSignal", async () => {
const effect = Effect.tryPromise<number>(() =>
new Promise((resolve) => {
setTimeout(() => {
timeout.set(() => {
resolve(1)
}, 100)
})
Expand All @@ -17,7 +18,7 @@ describe.concurrent("Effect", () => {
it("tryPromise - failure, no catch, no AbortSignal", async () => {
const effect = Effect.tryPromise<void>(() =>
new Promise((_resolve, reject) => {
setTimeout(() => {
timeout.set(() => {
reject("error")
}, 100)
})
Expand All @@ -30,7 +31,7 @@ describe.concurrent("Effect", () => {
const effect = Effect.tryPromise({
try: () =>
new Promise((_resolve, reject) => {
setTimeout(() => {
timeout.set(() => {
reject("error")
}, 100)
}),
Expand All @@ -47,7 +48,7 @@ describe.concurrent("Effect", () => {
aborted = true
})
return new Promise((resolve) => {
setTimeout(() => {
timeout.set(() => {
resolve()
}, 100)
})
Expand All @@ -68,7 +69,7 @@ describe.concurrent("Effect", () => {
aborted = true
})
return new Promise((resolve) => {
setTimeout(() => {
timeout.set(() => {
resolve()
}, 100)
})
Expand Down

0 comments on commit c43d4aa

Please sign in to comment.