-
-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add Layer.RuntimeClass, to make incremental adoption easier
- Loading branch information
Showing
2 changed files
with
169 additions
and
4 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,36 @@ | ||
--- | ||
"effect": patch | ||
--- | ||
|
||
add Layer.RuntimeClass, to make incremental adoption easier | ||
|
||
You can use a RuntimeClass to build an Effect runner that can use the | ||
dependencies from the given Layer. For example: | ||
|
||
```ts | ||
import type { Effect } from "effect"; | ||
import { Console, Context, Layer } from "effect"; | ||
|
||
class Notifications extends Context.Tag("Notifications")< | ||
Notifications, | ||
{ readonly notify: (message: string) => Effect.Effect<void> } | ||
>() { | ||
static Live = Layer.succeed(this, { | ||
notify: (message) => Console.log(message), | ||
}); | ||
} | ||
|
||
class NotificationsClass extends Layer.RuntimeClass(() => Notifications.Live) { | ||
notify: (message: string) => Promise<void> = this.$serviceFn( | ||
Notifications, | ||
(_) => _.notify, | ||
); | ||
} | ||
|
||
async function main() { | ||
await using notifications = new NotificationsClass(); | ||
await notifications.notify("Hello, world!"); | ||
} | ||
|
||
main(); | ||
``` |
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