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

Commit

Permalink
move into internal timeout module
Browse files Browse the repository at this point in the history
  • Loading branch information
fubhy committed Sep 11, 2023
1 parent 974142e commit 2dbe149
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 55 deletions.
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) {
core.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) => core.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) => core.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 = core.setTimeout(() => {
const handle = timeout.set(() => {
completed = true
task()
}, millis)
return () => {
core.clearTimeout(handle)
timeout.clear(handle)
return !completed
}
}
Expand Down
30 changes: 0 additions & 30 deletions src/internal/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2729,33 +2729,3 @@ const deferredInterruptJoiner = <E, A>(
)
}
})

// -----------------------------------------------------------------------------
// Timeout
// -----------------------------------------------------------------------------

/**
* 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 */
const clearTimeout_: (id: NodeJS.Timeout) => void = isBun ? clearInterval : clearTimeout

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

return id
} :
setTimeout

export { clearTimeout_ as clearTimeout, setTimeout_ as setTimeout }
25 changes: 22 additions & 3 deletions src/internal/timeout.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
import * as core from "@effect/io/internal/core"
/**
* Bun currently has a bug where `setTimeout` doesn't behave correctly with a 0ms delay.
*
* @see https://github.com/oven-sh/bun/issues/3333
*/

export const set = core.setTimeout
export const clear = core.clearTimeout
/** @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
4 changes: 2 additions & 2 deletions test/Effect/promise.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as Effect from "@effect/io/Effect"
import { setTimeout } from "@effect/io/test/utils/timeout"
import * as timeout from "@effect/io/internal/timeout"

describe.concurrent("Effect", () => {
it("promise - success with AbortSignal", async () => {
Expand All @@ -9,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
8 changes: 4 additions & 4 deletions test/Effect/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
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 { setTimeout } from "@effect/io/test/utils/timeout"
import { assert, describe } from "vitest"

describe.concurrent("Effect", () => {
Expand All @@ -15,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
12 changes: 6 additions & 6 deletions test/Effect/tryPromise.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import * as Either from "@effect/data/Either"
import * as Effect from "@effect/io/Effect"
import { setTimeout } from "@effect/io/test/utils/timeout"
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 @@ -18,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 @@ -31,7 +31,7 @@ describe.concurrent("Effect", () => {
const effect = Effect.tryPromise({
try: () =>
new Promise((_resolve, reject) => {
setTimeout(() => {
timeout.set(() => {
reject("error")
}, 100)
}),
Expand All @@ -48,7 +48,7 @@ describe.concurrent("Effect", () => {
aborted = true
})
return new Promise((resolve) => {
setTimeout(() => {
timeout.set(() => {
resolve()
}, 100)
})
Expand All @@ -69,7 +69,7 @@ describe.concurrent("Effect", () => {
aborted = true
})
return new Promise((resolve) => {
setTimeout(() => {
timeout.set(() => {
resolve()
}, 100)
})
Expand Down
4 changes: 0 additions & 4 deletions test/utils/timeout.ts

This file was deleted.

0 comments on commit 2dbe149

Please sign in to comment.