Skip to content

Instantly share code, notes, and snippets.

@adamw
Last active June 4, 2020 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamw/b3942821c2a5d32a7356b0123d92424d to your computer and use it in GitHub Desktop.
Save adamw/b3942821c2a5d32a7356b0123d92424d to your computer and use it in GitHub Desktop.
import zio.{Task, ZIO, ZLayer}
object UserRegistration {
// 1. service
class Service(notifier: UserNotifier.Service, userModel: UserModel.Service) {
def register(u: User): Task[User] = {
for {
_ <- userModel.insert(u)
_ <- notifier.notify(u, "Welcome!")
} yield u
}
}
// 2. layer
val live: ZLayer[UserNotifier with UserModel, Nothing, UserRegistration] =
ZLayer.fromServices[UserNotifier.Service,
UserModel.Service,
UserRegistration.Service](
new Service(_, _)
)
// 3. accessor
def register(u: User): ZIO[UserRegistration, Throwable, User] =
ZIO.accessM(_.get.register(u))
}
// ---
// 4. type alias in package.scala
type UserRegistration = Has[UserRegistration.Service]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment