-
-
Notifications
You must be signed in to change notification settings - Fork 273
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow Effect.Tag to take a constructor, enables direct inference and …
…layer construction.
- Loading branch information
1 parent
ce8b810
commit a895f76
Showing
3 changed files
with
191 additions
and
29 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,67 @@ | ||
--- | ||
"effect": patch | ||
--- | ||
|
||
Allow Effect.Tag to take a constructor, enables direct inference and layer construction. | ||
|
||
Namely the following is now possible: | ||
|
||
```ts | ||
import { Effect } from "effect" | ||
|
||
class Logger extends Effect.Tag("Logger", { | ||
sync: () => ({ | ||
info: (message: string) => Effect.logInfo(message) | ||
}) | ||
})<Logger>() {} | ||
``` | ||
|
||
Together with: | ||
|
||
```ts | ||
import { Effect } from "effect" | ||
|
||
class Logger extends Effect.Tag("Logger", { | ||
effect: Effect.sync(() => ({ | ||
info: (message: string) => Effect.logInfo(message) | ||
})) | ||
})<Logger>() {} | ||
``` | ||
|
||
And: | ||
|
||
```ts | ||
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: `Logger.Live`. | ||
|
||
The created layer is also exported with the name `Logger.Layer` to make dependency providing more ergonomic, for example: | ||
|
||
```ts | ||
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) | ||
} | ||
}) | ||
})<Logger>() { | ||
static Live = this.Layer.pipe(Layer.provide(Dummy.Live)) | ||
} | ||
``` |
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
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