Skip to content

Commit

Permalink
di (fix #3708): Support bindXXX(..).onXXX hooks (#3709)
Browse files Browse the repository at this point in the history
  • Loading branch information
xerial authored Oct 31, 2024
1 parent bc8ed3d commit 1c35f4b
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 17 deletions.
17 changes: 9 additions & 8 deletions airframe-di/src/main/scala-2/wvlet/airframe/DesignImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,17 @@ private[airframe] trait DesignImpl extends LogSupport {
def bind[A]: Binder[A] = macro AirframeMacros.designBindImpl[A]
def remove[A]: Design = macro AirframeMacros.designRemoveImpl[A]

def bindInstance[A](obj: A): Design = macro AirframeMacros.designBindInstanceImpl[A]
def bindSingleton[A]: Design = macro AirframeMacros.designBindSingletonImpl[A]
def bindImpl[A, B <: A]: Design = macro AirframeMacros.designBindImplImpl[A, B]
def bindProvider[D1, A](f: D1 => A): Design = macro AirframeMacros.designBindProvider1Impl[D1, A]
def bindProvider[D1, D2, A](f: (D1, D2) => A): Design = macro AirframeMacros.designBindProvider2Impl[D1, D2, A]
def bindProvider[D1, D2, D3, A](f: (D1, D2, D3) => A): Design =
def bindInstance[A](obj: A): DesignWithContext[A] = macro AirframeMacros.designBindInstanceImpl[A]
def bindSingleton[A]: DesignWithContext[A] = macro AirframeMacros.designBindSingletonImpl[A]
def bindImpl[A, B <: A]: DesignWithContext[B] = macro AirframeMacros.designBindImplImpl[A, B]
def bindProvider[D1, A](f: D1 => A): DesignWithContext[A] = macro AirframeMacros.designBindProvider1Impl[D1, A]
def bindProvider[D1, D2, A](f: (D1, D2) => A): DesignWithContext[A] =
macro AirframeMacros.designBindProvider2Impl[D1, D2, A]
def bindProvider[D1, D2, D3, A](f: (D1, D2, D3) => A): DesignWithContext[A] =
macro AirframeMacros.designBindProvider3Impl[D1, D2, D3, A]
def bindProvider[D1, D2, D3, D4, A](f: (D1, D2, D3, D4) => A): Design =
def bindProvider[D1, D2, D3, D4, A](f: (D1, D2, D3, D4) => A): DesignWithContext[A] =
macro AirframeMacros.designBindProvider4Impl[D1, D2, D3, D4, A]
def bindProvider[D1, D2, D3, D4, D5, A](f: (D1, D2, D3, D4, D5) => A): Design =
def bindProvider[D1, D2, D3, D4, D5, A](f: (D1, D2, D3, D4, D5) => A): DesignWithContext[A] =
macro AirframeMacros.designBindProvider5Impl[D1, D2, D3, D4, D5, A]

/**
Expand Down
16 changes: 8 additions & 8 deletions airframe-di/src/main/scala-3/wvlet/airframe/DesignImpl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ private[airframe] trait DesignImpl extends LogSupport:
val target = Surface.of[A]
new Design(self.designOptions, self.binding.filterNot(_.from == target), self.hooks)

inline def bindInstance[A](obj: A): Design =
inline def bindInstance[A](obj: A): DesignWithContext[A] =
bind[A].toInstance(obj)
inline def bindSingleton[A]: Design =
inline def bindSingleton[A]: DesignWithContext[A] =
bind[A].toSingleton
inline def bindImpl[A, B <: A]: Design =
inline def bindImpl[A, B <: A]: DesignWithContext[B] =
bind[A].to[B]
inline def bindProvider[D1, A](f: D1 => A): Design =
inline def bindProvider[D1, A](f: D1 => A): DesignWithContext[A] =
bind[A].toProvider[D1](f)
inline def bindProvider[D1, D2, A](f: (D1, D2) => A): Design =
inline def bindProvider[D1, D2, A](f: (D1, D2) => A): DesignWithContext[A] =
bind[A].toProvider[D1, D2](f)
inline def bindProvider[D1, D2, D3, A](f: (D1, D2, D3) => A): Design =
inline def bindProvider[D1, D2, D3, A](f: (D1, D2, D3) => A): DesignWithContext[A] =
bind[A].toProvider[D1, D2, D3](f)
inline def bindProvider[D1, D2, D3, D4, A](f: (D1, D2, D3, D4) => A): Design =
inline def bindProvider[D1, D2, D3, D4, A](f: (D1, D2, D3, D4) => A): DesignWithContext[A] =
bind[A].toProvider[D1, D2, D3, D4](f)
inline def bindProvider[D1, D2, D3, D4, D5, A](f: (D1, D2, D3, D4, D5) => A): Design =
inline def bindProvider[D1, D2, D3, D4, D5, A](f: (D1, D2, D3, D4, D5) => A): DesignWithContext[A] =
bind[A].toProvider[D1, D2, D3, D4, D5](f)

/**
Expand Down
2 changes: 1 addition & 1 deletion airframe-di/src/main/scala-3/wvlet/airframe/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ val traitFactoryCache = new ConcurrentHashMap[Surface, Session => Any].asScala
def getOrElseUpdateTraitFactoryCache(s: Surface, factory: Session => Any): Session => Any =
traitFactoryCache.getOrElseUpdate(s, factory)

@deprecated("Instantiating trait is still experimental in Scala 3.3.1", "23.9.1")
@deprecated("Instantiating trait with DI is still experimental in Scala 3", "23.9.1")
inline def registerTraitFactory[A]: Unit = {
// registerTraitFactoryImpl[A]
}
Expand Down
22 changes: 22 additions & 0 deletions airframe-di/src/test/scala/wvlet/airframe/di/NewDISyntaxTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,26 @@ object NewDISyntaxTest extends AirSpec {
session.build[D5] shouldBe D5(5, 10, "hello", B("hello"), D3(5, 10, "hello"))
}
}

test("new bind syntax with context") {
var started = false
var closed = false

val d = newDesign
.bindInstance[String]("hello")
.onStart { (s: String) =>
started = true
}
.onShutdown { (s: String) =>
closed = true
}
.noLifeCycleLogging
.withSession { session =>
started shouldBe true
closed shouldBe false
}

started shouldBe true
closed shouldBe true
}
}

0 comments on commit 1c35f4b

Please sign in to comment.