Skip to content

Commit

Permalink
fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
zhztheplayer committed Dec 5, 2024
1 parent 6f3306c commit 247723c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ object Component {
graph.sorted()
}

private[backend] def sortedUnsafe(): Seq[Component] = {
graph.sorted()
}

private class Registry {
private val lookupByUid: mutable.Map[Int, Component] = mutable.Map()
private val lookupByClass: mutable.Map[Class[_ <: Component], Component] = mutable.Map()
Expand Down Expand Up @@ -119,14 +123,14 @@ object Component {
}
}

class Graph private[Component] {
private class Graph {
import Graph._
private val registry: Registry = new Registry()
private val requirements: mutable.Buffer[(Int, Class[_ <: Component])] = mutable.Buffer()

private var sortedComponents: Option[Seq[Component]] = None

private[Component] def add(comp: Component): Unit = synchronized {
def add(comp: Component): Unit = synchronized {
require(
!registry.isUidRegistered(comp.uid),
s"Component UID ${comp.uid} already registered: ${comp.name()}")
Expand All @@ -137,9 +141,7 @@ object Component {
sortedComponents = None
}

private[Component] def declareRequirement(
comp: Component,
requiredCompClass: Class[_ <: Component]): Unit =
def declareRequirement(comp: Component, requiredCompClass: Class[_ <: Component]): Unit =
synchronized {
require(registry.isUidRegistered(comp.uid))
require(registry.isClassRegistered(comp.getClass))
Expand Down Expand Up @@ -191,7 +193,7 @@ object Component {
* requirement from component A to component B.
*/
// format: on
private[Component] def sorted(): Seq[Component] = synchronized {
def sorted(): Seq[Component] = synchronized {
if (sortedComponents.isDefined) {
return sortedComponents.get
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ package object backend extends Logging {
all.foreach(_.ensureRegistered())

// Output log so user could view the component loading order.
val components = Component.sorted()
// Call #sortedUnsafe than on #sorted to avoid unnecessary recursion.
val components = Component.sortedUnsafe()
logInfo(s"Components registered within order: ${components.map(_.name()).mkString(", ")}")
}
}

0 comments on commit 247723c

Please sign in to comment.