Skip to content

Commit

Permalink
Make subcontext example slightly more idiomatic, add scaladoc to `Sub…
Browse files Browse the repository at this point in the history
…context.produceRun`
  • Loading branch information
neko-kai committed Nov 28, 2023
1 parent 1cba84f commit e88220f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@ import izumi.reflect.{Tag, TagK}
/** @see [[https://izumi.7mind.io/distage/basics.html#subcontexts Subcontexts feature]] */
trait Subcontext[A] {
def produce[F[_]: QuasiIO: TagK](): Lifecycle[F, A]

/**
* Same as `.produce[F]().use(f)`
*
* @note Resources allocated by the subcontext will be closed after `f` exits.
* Use `produce` if you need to extend the lifetime of the Subcontext's resources.
*/
def produceRun[F[_]: QuasiIO: TagK, B](f: A => F[B]): F[B]
final def produceRun[B](f: A => B): B = produceRun[Identity, B](f)

Expand Down
15 changes: 10 additions & 5 deletions doc/microsite/src/main/tut/distage/basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -1443,7 +1443,7 @@ def module2[F[+_, +_] : TagKK] = new ModuleDef {
.localDependency[RequestId]
}

class HACK_OVERRIDE_PetStoreAPIHandler[F[+_, +_] : IO2 : TagKK](
class HACK_OVERRIDE_PetStoreAPIHandler[F[+_, +_]: IO2: TagKK](
petStoreBusinessLogic: Subcontext[HACK_OVERRIDE_PetStoreBusinessLogic[F]]
) {
def buyPet(petId: PetId, payment: Int): F[Throwable, Pet] = {
Expand All @@ -1457,7 +1457,7 @@ class HACK_OVERRIDE_PetStoreAPIHandler[F[+_, +_] : IO2 : TagKK](
}
```

We managed to move RequestId from a method parameter, that polluted every method signature, to a class parameter, that we pass to the subgraph just once - when the RequestId is generated.
We managed to move RequestId from a method parameter that polluted every method signature, to a class parameter, that we pass to the subgraph just once - when the RequestId is generated.

Full example:

Expand All @@ -1481,6 +1481,7 @@ def HACK_OVERRIDE_IzLogger(): logstage.IzLogger = {
```scala mdoc:override:to-string
import distage.{Injector, Lifecycle, ModuleDef, Subcontext, TagKK}
import izumi.functional.bio.{Error2, F, IO2, Monad2, Primitives2}
import izumi.functional.bio.data.Morphism1
import logstage.{IzLogger, LogIO2}
import izumi.logstage.distage.LogIO2Module

Expand All @@ -1503,10 +1504,14 @@ final class PetStoreAPIHandler[F[+_, +_]: IO2: TagKK](
def buyPet(petId: PetId, payment: Int): F[TransactionFailure, Pet] = {
for {
requestId <- F.sync(RequestId(UUID.randomUUID()))
component <- petStoreBusinessLogic
pet <- petStoreBusinessLogic
.provide[RequestId](requestId)
.produceRun[F[Throwable, _], PetStoreBusinessLogic[F]](F.pure(_)).orTerminate
pet <- component.buyPetLogic(petId, payment)
.produce[F[Throwable, _]]()
.mapK[F[Throwable, _], F[TransactionFailure, _]](Morphism1(_.orTerminate))
.use {
component =>
component.buyPetLogic(petId, payment)
}
} yield pet
}
}
Expand Down

0 comments on commit e88220f

Please sign in to comment.