Skip to content

Commit

Permalink
Updated with requested changes for error message and IArray
Browse files Browse the repository at this point in the history
  • Loading branch information
KristianAN authored and davesmith00000 committed Jan 23, 2024
1 parent b23bb9a commit cf9cf04
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion docs/02-guides/goodies.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Tyrian comes with a number of handy functions built-in that you can make use of
These nuggets of functionality are used as commands.

- `Dom` - A few methods such as `focus` and `blur` to manipulate the DOM. Inspired by the Elm [Browser.Dom](https://package.elm-lang.org/packages/elm/browser/latest/Browser.Dom) package.
- `FileReader` - Given the id of a file input field that has had a file selected, this Cmd will read either raw bytes, an image or text file to return a `Vector[Byte]` or an `HTMLImageElement` or `String` respectively.
- `FileReader` - Given the id of a file input field that has had a file selected, this Cmd will read either raw bytes, an image or text file to return a `IArray[Byte]` or an `HTMLImageElement` or `String` respectively.
- `Http` - Make HTTP requests that return their responses as a message.
- `ImageLoader` - Given a path, this cmd will load an image and return an `HTMLImageElement` for you to make use of.
- `LocalStorage` - Allows you to save and load to/from your browsers local storage.
Expand Down
8 changes: 4 additions & 4 deletions sandbox/src/main/scala/example/Sandbox.scala
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ object Sandbox extends TyrianIOApp[Msg, Model]:

case Msg.ReadBytesFile(file) =>
val cmd: Cmd[IO, Msg] =
FileReader.readBytes(file)((r: FileReader.Result[Vector[Byte]]) =>
FileReader.readBytes(file)((r: FileReader.Result[IArray[Byte]]) =>
r match {
case FileReader.Result.File(_, _, d) =>
Msg.FileBytesRead(d)
Expand Down Expand Up @@ -642,7 +642,7 @@ object Sandbox extends TyrianIOApp[Msg, Model]:
.map(data => p(data))
.toList ++
model.loadedBytes
.map(data => p(s"Read ${data.length} bytes"))
.map(data => p(s"Read ${data.length} bytes in an IArray"))
.toList
)
)
Expand Down Expand Up @@ -757,7 +757,7 @@ enum Msg:
case FileTextRead(fileData: String)
case SelectBytesFile
case ReadBytesFile(file: dom.File)
case FileBytesRead(fileData: Vector[Byte])
case FileBytesRead(fileData: IArray[Byte])
case NoOp

enum Status:
Expand Down Expand Up @@ -809,7 +809,7 @@ final case class Model(
fruitInput: String,
loadedImage: Option[String],
loadedText: Option[String],
loadedBytes: Option[Vector[Byte]]
loadedBytes: Option[IArray[Byte]]
)

final case class Fruit(name: String, available: Boolean)
Expand Down
18 changes: 9 additions & 9 deletions tyrian/js/src/main/scala/tyrian/cmds/FileReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import scala.scalajs.js
import scala.scalajs.js.typedarray

/** Given the id of a file input field that has had a file selected, this Cmd will read either raw bytes, an image or
* text file to return a `Vector[Byte]` or an `HTMLImageElement` or `String` respectively.
* text file to return a `IArray[Byte]` or an `HTMLImageElement` or `String` respectively.
*/
object FileReader:

Expand Down Expand Up @@ -57,22 +57,22 @@ object FileReader:
readFile(file, ReadType.AsText)(cast andThen resultToMessage)

/** Reads an input file from an input field as bytes */
def readBytes[F[_]: Async, Msg](inputFieldId: String)(resultToMessage: Result[Vector[Byte]] => Msg): Cmd[F, Msg] =
val cast: Result[js.Any] => Result[Vector[Byte]] =
def readBytes[F[_]: Async, Msg](inputFieldId: String)(resultToMessage: Result[IArray[Byte]] => Msg): Cmd[F, Msg] =
val cast: Result[js.Any] => Result[IArray[Byte]] =
case Result.Error(msg) => Result.Error(msg)
case Result.File(n, p, d) =>
try Result.File(n, p, new typedarray.Int8Array(d.asInstanceOf[typedarray.ArrayBuffer]).toVector)
catch case _ => Result.Error("File is not bytes")
try Result.File(n, p, IArray.from(new typedarray.Int8Array(d.asInstanceOf[typedarray.ArrayBuffer])))
catch case _ => Result.Error("Could not cast loaded file data to byte array")

readFromInputField(inputFieldId, ReadType.AsArrayBuffer)(cast andThen resultToMessage)

/** Reads an input file as bytes */
def readBytes[F[_]: Async, Msg](file: dom.File)(resultToMessage: Result[Vector[Byte]] => Msg): Cmd[F, Msg] =
val cast: Result[js.Any] => Result[Vector[Byte]] =
def readBytes[F[_]: Async, Msg](file: dom.File)(resultToMessage: Result[IArray[Byte]] => Msg): Cmd[F, Msg] =
val cast: Result[js.Any] => Result[IArray[Byte]] =
case Result.Error(msg) => Result.Error(msg)
case Result.File(n, p, d) =>
try Result.File(n, p, new typedarray.Int8Array(d.asInstanceOf[typedarray.ArrayBuffer]).toVector)
catch case _ => Result.Error("File is not bytes")
try Result.File(n, p, IArray.from(new typedarray.Int8Array(d.asInstanceOf[typedarray.ArrayBuffer])))
catch case _ => Result.Error("Could not cast loaded file data to byte array")

readFile(file, ReadType.AsArrayBuffer)(cast andThen resultToMessage)

Expand Down

0 comments on commit cf9cf04

Please sign in to comment.