Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
rpiaggio committed Dec 23, 2024
1 parent b1aee55 commit 42c0675
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
11 changes: 6 additions & 5 deletions workers/src/main/scala/japgolly/webapputil/binary/BinaryJs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ trait BinaryJs {

final def base64ToByteBuffer(base64: String): ByteBuffer = {
val binstr = window.atob(base64)
val buf = new Int8Array(binstr.length)
var i = 0
val buf = new Int8Array(binstr.length)
var i = 0
binstr.foreach { ch =>
buf(i) = ch.toByte
i += 1
Expand All @@ -33,8 +33,9 @@ trait BinaryJs {

final def blobToArrayBuffer(blob: Blob): ArrayBuffer = {
var arrayBuffer: ArrayBuffer = null
val fileReader = new FileReader()
fileReader.onload = e => arrayBuffer = e.target.asInstanceOf[js.Dynamic].result.asInstanceOf[ArrayBuffer]
val fileReader = new FileReader()
fileReader.onload = e =>
arrayBuffer = e.target.asInstanceOf[js.Dynamic].result.asInstanceOf[ArrayBuffer]
fileReader.readAsArrayBuffer(blob)
assert(arrayBuffer != null)
arrayBuffer
Expand All @@ -51,7 +52,7 @@ trait BinaryJs {
if (bb.hasTypedArray())
bb.typedArray()
else if (bb.hasArray) {
var array = bb.array()
var array = bb.array()
val offset = bb.arrayOffset()
if (limit != array.length)
array = array.slice(offset, offset + limit)
Expand Down
32 changes: 17 additions & 15 deletions workers/src/main/scala/japgolly/webapputil/indexeddb/KeyCodec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,14 @@ import java.util.UUID
import scala.reflect.ClassTag
import scala.scalajs.js

final case class KeyCodec[A](encode: A => IndexedDbKey,
decode: IndexedDbKey => CallbackTo[A]) {
final case class KeyCodec[A](encode: A => IndexedDbKey, decode: IndexedDbKey => CallbackTo[A]) {

def xmap[B](onDecode: A => B)(onEncode: B => A): KeyCodec[B] =
// Delegating because decoding can fail and must be wrapped to be pure
xmapSync(
a => CallbackTo(onDecode(a)))(
onEncode)
xmapSync(a => CallbackTo(onDecode(a)))(onEncode)

def xmapSync[B](onDecode: A => CallbackTo[B])(onEncode: B => A): KeyCodec[B] =
KeyCodec[B](
encode = encode compose onEncode,
decode = decode(_).flatMap(onDecode))
KeyCodec[B](encode = encode.compose(onEncode), decode = decode(_).flatMap(onDecode))
}

object KeyCodec {
Expand All @@ -34,13 +29,20 @@ object KeyCodec {
lazy val long: KeyCodec[Long] =
string.xmap(_.toLong)(_.toString)

def primative[A](name: String)(implicit ev: A => IndexedDbKey.Typed, ct: ClassTag[A]): KeyCodec[A] =
apply[A](a => IndexedDbKey(ev(a)), k => CallbackTo(
(k.value: Any) match {
case a: A => a
case x => throw new js.JavaScriptException(s"Invalid IDB key found. $name expected, got: $x")
}
))
def primative[A](
name: String
)(implicit ev: A => IndexedDbKey.Typed, ct: ClassTag[A]): KeyCodec[A] =
apply[A](
a => IndexedDbKey(ev(a)),
k =>
CallbackTo(
(k.value: Any) match {
case a: A => a
case x =>
throw new js.JavaScriptException(s"Invalid IDB key found. $name expected, got: $x")
}
)
)

lazy val string: KeyCodec[String] =
primative("String")
Expand Down
22 changes: 15 additions & 7 deletions workers/src/main/scala/japgolly/webapputil/indexeddb/TxnDsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ sealed abstract class TxnDsl[M <: TxnMode] {
final def objectStore[K, V](s: ObjectStoreDef.Sync[K, V]): Txn[M, ObjectStore[K, V]] =
TxnStep.GetStore(s)

@inline final def objectStore[K, V](s: ObjectStoreDef.Async[K, V]): Txn[M, ObjectStore[K, s.Value]] =
@inline final def objectStore[K, V](
s: ObjectStoreDef.Async[K, V]
): Txn[M, ObjectStore[K, s.Value]] =
objectStore(s.sync)

@inline final def sequence[G[_], A](txns: G[Txn[M, A]])(implicit G: Traverse[G]): Txn[M, G[A]] =
traverse(txns)(identityFn)

@inline final def sequenceIterable[F[x] <: Iterable[x], A](txns: => F[Txn[M, A]])(implicit cbf: BuildFrom[F[Txn[M, A]], A, F[A]]): Txn[M, F[A]] =
@inline final def sequenceIterable[F[x] <: Iterable[x], A](txns: => F[Txn[M, A]])(implicit
cbf: BuildFrom[F[Txn[M, A]], A, F[A]]
): Txn[M, F[A]] =
traverseIterable(txns)(identityFn)

@inline final def sequenceIterable_(txns: => Iterable[Txn[M, Any]]): Txn[M, Unit] =
Expand All @@ -62,13 +66,17 @@ sealed abstract class TxnDsl[M <: TxnMode] {
@inline final def sequenceOption_(o: Option[Txn[M, Any]]): Txn[M, Unit] =
traverseOption_(o)(identityFn)

final def traverse[G[_], A, B](ga: G[A])(f: A => Txn[M, B])(implicit G: Traverse[G]): Txn[M, G[B]] =
final def traverse[G[_], A, B](ga: G[A])(f: A => Txn[M, B])(implicit
G: Traverse[G]
): Txn[M, G[B]] =
G.traverse(ga)(f.andThen(_.step))

final def traverseIterable[F[x] <: Iterable[x], A, B](fa: => F[A])(f: A => Txn[M, B])(implicit cbf: BuildFrom[F[A], B, F[B]]): Txn[M, F[B]] =
final def traverseIterable[F[x] <: Iterable[x], A, B](
fa: => F[A]
)(f: A => Txn[M, B])(implicit cbf: BuildFrom[F[A], B, F[B]]): Txn[M, F[B]] =
suspend {
val as = fa
val b = cbf.newBuilder(as)
val b = cbf.newBuilder(as)
if (as.isEmpty)
pure(b.result())
else
Expand Down Expand Up @@ -121,12 +129,12 @@ sealed abstract class TxnDsl[M <: TxnMode] {
object TxnDsl {

object RO extends TxnDsl[RO] {
override implicit def catsInstance: Txn.CatsInstance[RO] = Txn.catsInstance(this)
override implicit def catsInstance: Txn.CatsInstance[RO] = Txn.catsInstance(this)
override protected implicit def autoWrapStepRO[B](s: TxnStep[RO, B]): Txn[RO, B] = Txn(s)
}

object RW extends TxnDsl[RW] {
override implicit def catsInstance: Txn.CatsInstance[RW] = Txn.catsInstance(this)
override implicit def catsInstance: Txn.CatsInstance[RW] = Txn.catsInstance(this)
override protected implicit def autoWrapStepRO[B](s: TxnStep[RO, B]): Txn[RO, B] = Txn(s)
}
}

0 comments on commit 42c0675

Please sign in to comment.