Last active
June 4, 2020 05:17
-
-
Save adamw/b3942821c2a5d32a7356b0123d92424d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
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