Skip to content

Commit c095ee9

Browse files
committed
wip
1 parent 6c8849f commit c095ee9

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

webApp/src/main/scala/wust/webApp/ClientStorage.scala

Lines changed: 36 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package wust.webApp
22

3+
import wust.util.time.time
34
import wust.facades.googleanalytics.Analytics
45
import io.circe._
56
import io.circe.parser._
@@ -11,18 +12,19 @@ import wust.webUtil.outwatchHelpers._
1112
import wust.api.Authentication
1213
import wust.api.serialize.Circe._
1314
import wust.graph._
15+
import wust.ids._
1416

15-
import scala.util.{Failure, Success, Try}
17+
import scala.util.{ Failure, Success, Try }
1618

1719
class ClientStorage(implicit owner: Ctx.Owner) {
18-
import org.scalajs.dom.ext.{LocalStorage => internal}
20+
import org.scalajs.dom.ext.{ LocalStorage => internal }
1921

2022
object keys {
2123
val auth = "wust.auth"
2224
val sidebarOpen = "wust.sidebar.open"
2325
val taglistOpen = "wust.taglist.open"
2426
val filterlistOpen = "wust.filterlist.open"
25-
val graphChanges = "wust.graph.changes"
27+
val pendingGraphChanges = "wust.graph.pendingChanges"
2628
val backendTimeDelta = "wust.backendtimedelta"
2729
val graphCachePageIndex = "wust.graphCachePageIndex"
2830
val graphCache = "wust.graphCache"
@@ -46,13 +48,36 @@ class ClientStorage(implicit owner: Ctx.Owner) {
4648
}
4749
}
4850

49-
def getGraph(page:Page):Option[Graph] = {
50-
internal
51+
def getGraph(page: Page): Option[Graph] = page.parentId.flatMap(getGraph)
52+
def getGraph(pageId: NodeId): Option[Graph] = {
53+
for {
54+
value <- internal(s"${keys.graphCache}${pageId.toBase58}")
55+
graph <- fromJson[Graph](value)
56+
} yield graph
57+
}
58+
59+
def updateGraph(page: Page, graph: Graph): Unit = page.parentId.foreach(updateGraph(_, graph))
60+
def updateGraph(pageId: NodeId, graph: Graph): Unit = {
61+
time(s"writing to cache: $graph") {
62+
internal.update(
63+
s"${keys.graphCache}${pageId.toBase58}",
64+
toJson(graph)
65+
)
66+
}
67+
}
68+
69+
def getPendingGraphChanges: Seq[GraphChanges] = {
70+
internal(keys.pendingGraphChanges)
71+
.flatMap(fromJson[Seq[GraphChanges]])
72+
.getOrElse(Seq.empty[GraphChanges])
73+
}
74+
75+
def addPendingGraphChanges(changes: Seq[GraphChanges]): Unit = {
76+
internal.update(keys.pendingGraphChanges, toJson(getPendingGraphChanges ++ changes))
5177
}
52-
def updateGraph(page:Page, graph:Graph):Unit = ???
5378

5479
val auth: Var[Option[Authentication]] = {
55-
if(canAccessLs) {
80+
if (canAccessLs) {
5681
LocalStorage
5782
.handlerWithoutEvents(keys.auth)
5883
.unsafeRunSync()
@@ -61,27 +86,8 @@ class ClientStorage(implicit owner: Ctx.Owner) {
6186
} else Var(None)
6287
}
6388

64-
// val rawGraph: Var[List[Graph]] = {
65-
// if(canAccessLs) {
66-
// LocalStorage
67-
// .handlerWithoutEvents(keys.graphChanges)
68-
// .unsafeRunSync()
69-
// .mapHandler[List[GraphChanges]](changes => Option(toJson(changes)))(_.flatMap(fromJson[List[GraphChanges]]).getOrElse(Nil))
70-
// } else Handler.unsafe[List[GraphChanges]]
71-
// }
72-
73-
//TODO: howto handle with events from other tabs?
74-
// val graphChanges: Handler[List[GraphChanges]] = {
75-
// if(canAccessLs) {
76-
// LocalStorage
77-
// .handlerWithoutEvents(keys.graphChanges)
78-
// .unsafeRunSync()
79-
// .mapHandler[List[GraphChanges]](changes => Option(toJson(changes)))(_.flatMap(fromJson[List[GraphChanges]]).getOrElse(Nil))
80-
// } else Handler.unsafe[List[GraphChanges]]
81-
// }
82-
8389
val sidebarOpen: Var[Option[Boolean]] = {
84-
if(canAccessLs) {
90+
if (canAccessLs) {
8591
LocalStorage
8692
.handlerWithoutEvents(keys.sidebarOpen)
8793
.unsafeRunSync()
@@ -91,7 +97,7 @@ class ClientStorage(implicit owner: Ctx.Owner) {
9197
}
9298

9399
val taglistOpen: Var[Option[Boolean]] = {
94-
if(canAccessLs) {
100+
if (canAccessLs) {
95101
LocalStorage
96102
.handlerWithoutEvents(keys.taglistOpen)
97103
.unsafeRunSync()
@@ -101,7 +107,7 @@ class ClientStorage(implicit owner: Ctx.Owner) {
101107
}
102108

103109
val filterlistOpen: Var[Option[Boolean]] = {
104-
if(canAccessLs) {
110+
if (canAccessLs) {
105111
LocalStorage
106112
.handlerWithoutEvents(keys.filterlistOpen)
107113
.unsafeRunSync()
@@ -111,7 +117,7 @@ class ClientStorage(implicit owner: Ctx.Owner) {
111117
}
112118

113119
val backendTimeDelta: Var[Long] = {
114-
if(canAccessLs) {
120+
if (canAccessLs) {
115121
LocalStorage
116122
.handlerWithoutEvents(keys.backendTimeDelta)
117123
.unsafeRunSync()

webApp/src/main/scala/wust/webApp/state/GlobalStateFactory.scala

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package wust.webApp.state
22

33
import java.util.concurrent.TimeUnit
4-
4+
import wust.webUtil.Elements.defer
5+
import concurrent.Future
56
import wust.facades.googleanalytics.Analytics
67
import wust.facades.hotjar
78
import monix.eval.Task
@@ -137,15 +138,24 @@ object GlobalStateFactory {
137138
}
138139
}
139140

140-
rawGraph.foreach { graph =>
141-
ClientStorage
141+
rawGraph.toObservable.debounce(2 second).foreach { graph =>
142+
Client.storage.updateGraph(page.now, graph)
142143
}
143144

144145
def getNewGraph(page: Page) = {
146+
def getGraphFromApi: Future[Graph] = {
147+
for {
148+
graph <- Client.api.getGraph(page)
149+
} yield enrichVisitedGraphWithSideEffects(page, graph)
150+
}
151+
145152
isLoading() = true
146-
val graph = for {
147-
graph <- Client.api.getGraph(page)
148-
} yield enrichVisitedGraphWithSideEffects(page, graph)
153+
val graph: Future[Graph] = if (!isOnline.now) {
154+
// offline. try to read graph from cache
155+
Client.storage.getGraph(page).fold(getGraphFromApi)(Future.successful)
156+
} else {
157+
getGraphFromApi
158+
}
149159

150160
graph.transform { result =>
151161
isLoading() = false
@@ -302,7 +312,7 @@ object GlobalStateFactory {
302312
state
303313
}
304314

305-
def setupStateDebugLogging(state: GlobalState)(implicit ctx:Ctx.Owner):Unit = {
315+
def setupStateDebugLogging(state: GlobalState)(implicit ctx: Ctx.Owner): Unit = {
306316
import state._
307317

308318
rawGraph.debugWithDetail((g: Graph) => s"rawGraph: ${g.toString}", (g: Graph) => g.toDetailedString)

0 commit comments

Comments
 (0)