Thanks to visit codestin.com
Credit goes to github.com

Skip to content

jilen/clayer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZLayer like data type for cats.effect.IO

This small library is intented to direct copy into your project.

Component layers

trait DB
object DB {
  val live: Layer[Any, DB] = Layer.resource {
    makeRes("db").as(new DB {})
  }
}

trait HttpCli
object HttpCli {
  val live: Layer[Any, HttpCli] = Layer.resource {
    makeRes("httpClient").as(new HttpCli {})
  }
}

trait AClient
object AClient {
  val live: Layer[HttpCli, AClient] = Layer.function { (a: HttpCli) =>
    makeRes("AClient").as(new AClient {})
  }
}

trait Service1
object Service1 {
  val live: Layer[DB & HttpCli, Service1] = Layer.function {
    (db: DB, http: HttpCli) =>
    IO(new Service1 {}).toResource
  }
}
trait Service2
object Service2 {
  val live: Layer[DB & AClient, Service2] = Layer.function {
    (db: DB, aclient: AClient) =>
    IO(new Service2 {}).toResource
  }
}

trait App
object App {
  val live: Layer[Service1 & Service2, App] = Layer.function {
    (s1: Service1, s2: Service2) =>
    makeRes("app").as(new App {})
  }
}

Wire app toghther

val appLayer: Layer[Any, App] =
    (DB.live ++ (HttpCli.live >+> AClient.live)) >>> (Service1.live ++ Service2.live) >>> App.live // Munally construction

val appLayer = Layer.make[App]( // Auto construction
    DB.live,
    HttpCli.live,
    AClient.live,
    Service1.live,
    Service2.live,
    App.live
  )

Use app layer

appLayer.build(ZEnv(())).use { env =>
  IO.println(env.get[App])
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages