-
-
Notifications
You must be signed in to change notification settings - Fork 256
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 and allow multiple layers to be provided in …
…Effect.provide (#3690) Co-authored-by: Tim <hello@timsmart.co> Co-authored-by: Patrick Roza <contact@patrickroza.com>
- Loading branch information
1 parent
0ba66f2
commit d75140c
Showing
10 changed files
with
763 additions
and
95 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 and Layer.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,53 @@ | ||
--- | ||
"effect": minor | ||
--- | ||
|
||
Implement Effect.Service as a Tag and Layer with Opaque Type. | ||
|
||
Namely the following is now possible: | ||
|
||
```ts | ||
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", { | ||
accessors: true, | ||
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.Default, Postfix.Default] | ||
}) {} | ||
|
||
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.Default, Prefix.Default, Postfix.Default])) | ||
) | ||
}) | ||
``` |
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
Oops, something went wrong.