-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebStorageKey.scala
62 lines (42 loc) · 2.31 KB
/
WebStorageKey.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package japgolly.webapputil.webstorage
import japgolly.scalajs.react.{AsyncCallback, Callback, CallbackTo}
import japgolly.webapputil.binary._
import japgolly.webapputil.webstorage.AbstractWebStorage.Key
final case class WebStorageKey[V](key: Key, valueCodec: ValueCodec[V]) {
def get(implicit s: AbstractWebStorage): CallbackTo[Option[V]] =
s.getItem(key).flatMap(CallbackTo.traverseOption(_)(valueCodec.decode))
def set(value: V)(implicit s: AbstractWebStorage): Callback =
valueCodec.encode(value).flatMap(s.setItem(key, _))
def remove(implicit s: AbstractWebStorage): Callback =
s.removeItem(key)
def setOrRemove(value: Option[V])(implicit s: AbstractWebStorage): Callback =
value.fold(remove)(set(_))
}
object WebStorageKey {
final case class Async[V](key: Key, valueCodec: ValueCodec.Async[V]) {
def get(implicit s: AbstractWebStorage): AsyncCallback[Option[V]] =
s.getItem(key).asAsyncCallback.flatMap(AsyncCallback.traverseOption(_)(valueCodec.decode))
def set(value: V)(implicit s: AbstractWebStorage): AsyncCallback[Unit] =
valueCodec.encode(value).flatMap(s.setItem(key, _).asAsyncCallback)
def remove(implicit s: AbstractWebStorage): Callback =
s.removeItem(key)
def setOrRemove(value: Option[V])(implicit s: AbstractWebStorage): AsyncCallback[Unit] =
value.fold(remove.asAsyncCallback)(set(_))
}
// ===================================================================================================================
// Convenience methods
def string(key: String): WebStorageKey[String] =
new WebStorageKey(Key(key), ValueCodec.string)
def boolean(key: String): WebStorageKey[Boolean] =
new WebStorageKey(Key(key), ValueCodec.boolean)
def binaryString(key: String)(implicit enc: BinaryString.Encoder): WebStorageKey[BinaryString] =
new WebStorageKey(Key(key), ValueCodec.binaryString)
def binary(key: String)(implicit enc: BinaryString.Encoder): WebStorageKey[BinaryData] =
new WebStorageKey(Key(key), ValueCodec.binary)
object Async {
def binary(key: String)(implicit enc: BinaryString.Encoder): Async[BinaryData] =
Async(Key(key), ValueCodec.Async.binary)
def binaryFormat[A](key: String, fmt: BinaryFormat[A])(implicit enc: BinaryString.Encoder): Async[A] =
Async(Key(key), ValueCodec.Async.binaryFormat(fmt))
}
}