lila/modules/study/src/main/StudyApi.scala

46 lines
1.2 KiB
Scala
Raw Normal View History

2016-02-26 05:08:11 -07:00
package lila.study
2016-02-27 04:30:38 -07:00
import akka.actor.ActorRef
2016-02-26 05:08:11 -07:00
import org.joda.time.DateTime
import lila.hub.actorApi.map.Tell
import lila.hub.actorApi.SendTo
2016-02-27 04:30:38 -07:00
import lila.hub.Sequencer
2016-02-26 05:08:11 -07:00
final class StudyApi(
repo: StudyRepo,
jsonView: JsonView,
2016-02-27 04:30:38 -07:00
sequencers: ActorRef,
2016-02-26 05:08:11 -07:00
socketHub: akka.actor.ActorRef) {
def byId = repo byId _
2016-02-27 04:30:38 -07:00
def locationByRef(ref: Location.Ref): Fu[Option[Location]] =
byId(ref.studyId) map (_ flatMap (_ location ref.chapterId))
def locationById(id: Location.Ref.ID): Fu[Option[Location]] =
(Location.Ref parseId id) ?? locationByRef
def setOwnerPath(refId: Location.Ref.ID, path: Path) = sequenceRef(refId) {
repo.setOwnerPath(_, path)
}
2016-03-02 02:18:52 -07:00
def addNode(refId: Location.Ref.ID, node: Node) = sequenceRef(refId) {
2016-03-01 08:26:14 -07:00
???
2016-02-28 17:28:35 -07:00
}
2016-02-27 04:30:38 -07:00
private def sequenceRef(refId: Location.Ref.ID)(f: Location.Ref => Funit): Funit =
Location.Ref.parseId(refId) ?? { ref =>
sequence(ref.studyId) {
f(ref)
}
}
private def sequence(studyId: String)(f: => Funit): Funit = {
val promise = scala.concurrent.Promise[Unit]
val work = Sequencer.work(f, promise.some)
sequencers ! Tell(studyId, work)
promise.future
}
2016-02-26 05:08:11 -07:00
}