Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/big-steaks-train.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

add `Effect.acquireDisposable`
23 changes: 23 additions & 0 deletions packages/effect/src/Effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5460,6 +5460,29 @@ export const acquireRelease: {
): Effect<A, E, Scope.Scope | R | R2>
} = fiberRuntime.acquireRelease

/**
* Constructs a scoped resource from an {@linkcode AsyncDisposable} or {@linkcode Disposable}
* @see {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/using}
*
* @example
* import sqlite from "node:sqlite";
* import { Effect } from "effect";
*
* // Define how the resource is acquired
* const acquire = Effect.sync(() => new sqlite.DatabaseSync(":memory:"))
* const resource = Effect.acquireDisposable(acquire)
*
* @see {@link acquireRelease} for more information about scopes.
*
* @since 3.0.0
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

idk what to put here

* @category Scoping, Resources & Finalization
*/
export const acquireDisposable: {
<A extends AsyncDisposable | Disposable, E, R>(
acquire: Effect<A, E, R>
): Effect<A, E, R | Scope.Scope>
} = fiberRuntime.acquireDisposable

/**
* Creates a scoped resource with an interruptible acquire action.
*
Expand Down
11 changes: 11 additions & 0 deletions packages/effect/src/internal/fiberRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,17 @@ export const acquireRelease: {
core.tap(acquire, (a) => addFinalizer((exit) => release(a, exit)))
))

/* @internal */
export const acquireDisposable: {
<A extends AsyncDisposable | Disposable, E, R>(
acquire: Effect.Effect<A, E, R>
): Effect.Effect<A, E, R | Scope.Scope>
} = (acquire) =>
acquireRelease(acquire, (resource) =>
Predicate.hasProperty(resource, Symbol.asyncDispose)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! I just had a thought.
What if an object has both Dispose symbols—why does the asynchronous one take priority?
Maybe it would make sense to split this function into two, or allow configuring the priority?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe the assumption is that if there is an async one the sync doesn't wait?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it has both dispose symbols, are both invoked? In which order?
The mdn suggests that async always takes priority and is even valid for sync cases, but I can't find a definition of behavior for using both symbols... Open to suggestions 🤷🏻

? internalEffect.promise(() => resource[Symbol.asyncDispose]())
: core.sync(() => resource[Symbol.dispose]()))

/* @internal */
export const acquireReleaseInterruptible: {
<X, R2>(
Expand Down
79 changes: 72 additions & 7 deletions packages/effect/test/Effect/acquire-release.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import { describe, it } from "@effect/vitest"
import { assertTrue, strictEqual } from "@effect/vitest/utils"
import { assertEquals, assertFalse, assertTrue, strictEqual } from "@effect/vitest/utils"
import * as Cause from "effect/Cause"
import * as Chunk from "effect/Chunk"
import * as Effect from "effect/Effect"
import { equals } from "effect/Equal"
import * as Exit from "effect/Exit"
import { pipe } from "effect/Function"
import * as Ref from "effect/Ref"

const disposable = (hook: () => void) => ({
[Symbol.dispose]() {
hook()
}
})
const asyncDisposable = (hook: () => Promise<void>) => ({
async [Symbol.asyncDispose]() {
await hook()
}
})

describe("Effect", () => {
it.effect("acquireUseRelease - happy path", () =>
Effect.gen(function*() {
Expand Down Expand Up @@ -53,8 +63,8 @@ describe("Effect", () => {
exit,
Exit.matchEffect({ onFailure: Effect.succeed, onSuccess: () => Effect.fail("effect should have failed") })
)
assertTrue(equals(Cause.failures(result), Chunk.of("use failed")))
assertTrue(equals(Cause.defects(result), Chunk.of(releaseDied)))
assertEquals(Cause.failures(result), Chunk.of("use failed"))
assertEquals(Cause.defects(result), Chunk.of(releaseDied))
}))
it.effect("acquireUseRelease - error handling + disconnect", () =>
Effect.gen(function*() {
Expand All @@ -75,8 +85,8 @@ describe("Effect", () => {
onSuccess: () => Effect.fail("effect should have failed")
})
)
assertTrue(equals(Cause.failures(result), Chunk.of("use failed")))
assertTrue(equals(Cause.defects(result), Chunk.of(releaseDied)))
assertEquals(Cause.failures(result), Chunk.of("use failed"))
assertEquals(Cause.defects(result), Chunk.of(releaseDied))
}))
it.effect("acquireUseRelease - beast mode error handling + disconnect", () =>
Effect.gen(function*() {
Expand Down Expand Up @@ -105,7 +115,62 @@ describe("Effect", () => {
)
)
const released = yield* (Ref.get(release))
assertTrue(equals(Cause.defects(result), Chunk.of(useDied)))
assertEquals(Cause.defects(result), Chunk.of(useDied))
assertTrue(released)
}))
it.effect("acquireDisposable - happy path", () =>
Effect.gen(function*() {
let disposed = false
yield* Effect.succeed(
disposable(() => {
disposed = true
})
)
.pipe(
Effect.acquireDisposable,
Effect.tap(() => assertFalse(disposed)),
Effect.scoped
)
assertTrue(disposed)
}))
it.effect("acquireDisposable - happy path async", () =>
Effect.gen(function*() {
let disposed = false
yield* Effect.succeed(
asyncDisposable(() =>
new Promise((resolve) => {
disposed = true
resolve()
})
)
)
.pipe(
Effect.acquireDisposable,
Effect.tap(() => assertFalse(disposed)),
Effect.scoped
)
assertTrue(disposed)
}))
it.effect("acquireDisposable - error handling", () =>
Effect.gen(function*() {
const err = new Error("oh no!")
const exit = yield* Effect.succeed(
disposable(() => {
throw err
})
)
.pipe(
Effect.acquireDisposable,
Effect.scoped,
Effect.exit
)
const result = yield* Exit.matchEffect(
exit,
{
onFailure: Effect.succeed,
onSuccess: () => Effect.fail("effect should have failed")
}
)
assertEquals(Cause.defects(result), Chunk.of(err))
}))
})
Loading