From 4d5be27f3dfa84a9b432d803bb086d48ada8c9dc Mon Sep 17 00:00:00 2001 From: Felix Dietze Date: Sat, 2 Nov 2019 07:47:32 -0400 Subject: [PATCH] isLoading: set true earlier, and false later --- .../webApp/state/GlobalStateFactory.scala | 39 +++++++++++++------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/webApp/src/main/scala/wust/webApp/state/GlobalStateFactory.scala b/webApp/src/main/scala/wust/webApp/state/GlobalStateFactory.scala index 5bfa7b470..c786f4b24 100644 --- a/webApp/src/main/scala/wust/webApp/state/GlobalStateFactory.scala +++ b/webApp/src/main/scala/wust/webApp/state/GlobalStateFactory.scala @@ -26,6 +26,9 @@ import rx.async.Platform._ import scala.concurrent.duration._ import wust.facades.segment.Segment import wust.api.AuthUser +import scala.concurrent.Future +import scala.util.Success +import scala.util.Failure object GlobalStateFactory { def init(): Unit = { @@ -140,17 +143,11 @@ object GlobalStateFactory { } } - def getNewGraph(page: Page) = { - isLoading() = true - val graph = for { + def getNewGraph(page: Page): Future[Graph] = { + for { graph <- Client.api.getGraph(page) } yield enrichVisitedGraphWithSideEffects(page, graph) - - graph.transform { result => - isLoading() = false - result } - } // if we have a payment success on startup, the notify user. urlConfig.now.info.foreach { @@ -171,8 +168,15 @@ object GlobalStateFactory { urlConfig.update(_.copy(invitation = None)) // get a new graph with new right after the accepted invitation - getNewGraph(viewConfig.pageChange.page).foreach { graph => - eventProcessor.localEvents.onNext(ReplaceGraph(graph)) + isLoading() = true + getNewGraph(viewConfig.pageChange.page).onComplete { + case Success(graph) => + eventProcessor.localEvents.onNext(ReplaceGraph(graph)).foreach { _ => + isLoading() = false + } + case Failure(_) => + //TODO: send empty graph event? + isLoading() = false } if (wasAssumed) { Segment.trackEvent("New Unregistered User", js.Dynamic.literal(`type` = "invite")) @@ -251,15 +255,25 @@ object GlobalStateFactory { isFirstGraphRequest = false result + }.map{ userAndPage => + isLoading() = true + userAndPage }.switchMap { case (viewConfig, user) => val currentTransitChanges = lastTransitChanges.fold(GraphChanges.empty)(_ merge _) SourceStream .fromFuture(getNewGraph(viewConfig.pageChange.page)) - .recover { case _ => Graph.empty } + .recover { + case _ => + isLoading() = false // TODO: capture error case later, after switchmap did its thing? + Graph.empty + } .map(g => ReplaceGraph(g.applyChanges(currentTransitChanges))) + }.foreach { graphEvent => + eventProcessor.localEvents.onNext(graphEvent).foreach { _ => + isLoading() = false } - .subscribe(eventProcessor.localEvents) + } } GlobalState.permissionState.triggerLater { state => @@ -366,6 +380,7 @@ object GlobalStateFactory { view.debug("view") user.debug("auth") selectedNodes.debug("selected") + isLoading.debug("isLoading") } } }