-
-
Notifications
You must be signed in to change notification settings - Fork 230
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Effect.Service as a Tag and Layer with Opaque Type and Supp…
…ort multiple layers in Effect.provide
- Loading branch information
1 parent
09bc526
commit 561c34e
Showing
5 changed files
with
458 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
) | ||
}) | ||
``` |
Oops, something went wrong.