11package wust .webApp
22
3+ import wust .util .time .time
34import wust .facades .googleanalytics .Analytics
45import io .circe ._
56import io .circe .parser ._
@@ -11,18 +12,19 @@ import wust.webUtil.outwatchHelpers._
1112import wust .api .Authentication
1213import wust .api .serialize .Circe ._
1314import wust .graph ._
15+ import wust .ids ._
1416
15- import scala .util .{Failure , Success , Try }
17+ import scala .util .{ Failure , Success , Try }
1618
1719class 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()
0 commit comments