Skip to content

Commit

Permalink
Implement Effect.Service as a Tag and Layer with Opaque Type and Supp…
Browse files Browse the repository at this point in the history
…ort multiple layers in Effect.provide
  • Loading branch information
mikearnaldi committed Sep 26, 2024
1 parent 09bc526 commit 561c34e
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 18 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-ways-accept.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": minor
---

Support providing an array of layers via Effect.provide
54 changes: 54 additions & 0 deletions .changeset/twelve-dots-enjoy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
"effect": minor
---

Implement Effect.Service as a Tag and Layer with Opaque Type.

Namely the following is now possible:

```ts
import * as Effect from "effect/Effect"
import * as it from "effect/test/utils/extend"
import { describe, expect } from "vitest"

class Prefix extends Effect.Service<Prefix>()("Prefix", {
sync: () => ({
prefix: "PRE"
})
}) {}

class Postfix extends Effect.Service<Postfix>()("Postfix", {
sync: () => ({
postfix: "POST"
})
}) {}

const messages: Array<string> = []

class Logger extends Effect.Service<Logger>()("Logger", {
effect: Effect.gen(function* () {
const { prefix } = yield* Prefix
const { postfix } = yield* Postfix
return {
info: (message: string) =>
Effect.sync(() => messages.push(`[${prefix}][${message}][${postfix}]`))
}
}),
dependencies: [Prefix, Postfix]
}) {}

describe("Effect", () => {
it.effect("Service correctly wires dependencies", () =>
Effect.gen(function* () {
const { _tag } = yield* Logger
expect(_tag).toEqual("Logger")
yield* Logger.info("Ok")
expect(messages).toEqual(["[PRE][Ok][POST]"])
const { prefix } = yield* Prefix
expect(prefix).toEqual("PRE")
const { postfix } = yield* Postfix
expect(postfix).toEqual("POST")
}).pipe(Effect.provide([Logger, Prefix, Postfix]))
)
})
```
Loading

0 comments on commit 561c34e

Please sign in to comment.