-
-
Notifications
You must be signed in to change notification settings - Fork 272
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement Effect.Service and allow multiple layers to be provided in Effect.provide #3690
Conversation
🦋 Changeset detectedLatest commit: 9ce2e30 The changes in this PR will be included in the next version bump. This PR includes changesets to release 32 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Yes this rocks! |
class DateTag extends Effect.Tag("DateTag", {
sync: () => new Date(1970, 1, 1)
})<DateTag>() {
static Test = Layer.sync(this, () => new Date(1970, 1, 2))
} not sure I understand |
e684cef
to
314c418
Compare
Yea it's fine too. not sure why I made |
Updated: Namely the following is now possible: import { Effect } from "effect"
class Logger extends Effect.Tag("Logger", {
sync: () => ({
info: (message: string) => Effect.logInfo(message)
})
})<Logger>() {} Together with: import { Effect } from "effect"
class Logger extends Effect.Tag("Logger", {
effect: Effect.sync(() => ({
info: (message: string) => Effect.logInfo(message)
}))
})<Logger>() {} And: import { Effect } from "effect"
class Logger extends Effect.Tag("Logger", {
scoped: Effect.gen(function* () {
yield* Effect.addFinalizer(() => Effect.logInfo("Done"))
return {
info: (message: string) => Effect.logInfo(message)
}
})
})<Logger>() {} All the above will lead to automatic layer creation named: The created layer is also exported with the name import { Effect, Layer } from "effect"
class Dummy extends Effect.Tag("Dummy", {
sync: () => ({})
})<Dummy>() {}
class Logger extends Effect.Tag("Logger", {
effect: Effect.gen(function* () {
yield* Dummy
return {
info: (message: string) => Effect.logInfo(message)
}
}),
dependencies: [Dummy]
})<Logger>() {} You'll have that |
If we make |
Is kind of neat yes. maybe call it
Do you mean |
yes
that won't be implemented here as you'll fill up your service with irrelevant fields and lose assignability to plain objects |
well, I was also the one fully against that when we were discussing the Context, and making it a requirement for service interfaces to be polluted with said fields, but now that it's optional, an implementation detail of your tag/service, |
but the pollution isn't only on assign, it's also on usage, you'll have stuff like |
leftover I think, I guess because using the constructor in userland wasn't really useful for anything, so |
also how did you manage to override |
I think I am kind of happy with: e17d085 |
I'm a fan of this - I especially like the idea of a variadic |
actually looking at implementation it won't be possible: |
I guess b95558e is fine though, looks like: 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])
))
}) |
The only remaining footgun is calling provide multiple times, wondering if we should work on having a shared global memoMap to avoid layer re-instantiation cc @tim-smart |
b95558e
to
c092480
Compare
215c0ed
to
3af7f31
Compare
…Effect.provide (#3690) Co-authored-by: Tim <[email protected]> Co-authored-by: Patrick Roza <[email protected]>
…Effect.provide (#3690) Co-authored-by: Tim <[email protected]> Co-authored-by: Patrick Roza <[email protected]>
…Effect.provide (#3690) Co-authored-by: Tim <[email protected]> Co-authored-by: Patrick Roza <[email protected]>
…Effect.provide (#3690) Co-authored-by: Tim <[email protected]> Co-authored-by: Patrick Roza <[email protected]>
…Effect.provide (#3690) Co-authored-by: Tim <[email protected]> Co-authored-by: Patrick Roza <[email protected]>
…Effect.provide (#3690) Co-authored-by: Tim <[email protected]> Co-authored-by: Patrick Roza <[email protected]>
Namely the following is now possible: