def register()

in util-core/src/main/scala/com/twitter/util/Event.scala [184:223]


    def register(s: Witness[(T, U)]): Closable = {
      val mu = new Object()
      var state: Option[Either[Queue[T], Queue[U]]] = None

      val left = self.respond {
        Function.synchronizeWith(mu) { t =>
          state match {
            case None =>
              state = Some(Left(Queue(t)))
            case Some(Left(q)) =>
              state = Some(Left(q enqueue t))
            case Some(Right(Queue(u, rest @ _*))) =>
              if (rest.isEmpty) state = None
              else state = Some(Right(Queue(rest: _*)))
              s.notify((t, u))
            case Some(Right(_)) =>
              throw new IllegalStateException("unexpected empty queue")
          }
        }
      }

      val right = other.respond {
        Function.synchronizeWith(mu) { u =>
          state match {
            case None =>
              state = Some(Right(Queue(u)))
            case Some(Right(q)) =>
              state = Some(Right(q enqueue u))
            case Some(Left(Queue(t, rest @ _*))) =>
              if (rest.isEmpty) state = None
              else state = Some(Left(Queue(rest: _*)))
              s.notify((t, u))
            case Some(Left(_)) =>
              throw new IllegalStateException("unexpected empty queue")
          }
        }
      }

      Closable.all(left, right)
    }