forked from PurpleKingdomGames/tyrian
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PurpleKingdomGames#198 : Add main-launcher example
- Loading branch information
Showing
8 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
0.6.2 | ||
0.6.3-SNAPSHOT |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Tyrian main launcher example | ||
|
||
This is a minimal working project setup to run the main launcher example. | ||
|
||
To run the program in a browser you will need to have yarn (or npm) installed. | ||
|
||
On first run: | ||
|
||
```sh | ||
yarn install | ||
``` | ||
|
||
and from then on | ||
|
||
```sh | ||
yarn start | ||
``` | ||
|
||
Then navigate to [http://localhost:1234/](http://localhost:1234/) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
|
||
<head> | ||
<meta charset="UTF-8"> | ||
<title>Main Launcher Example</title> | ||
<script type="module" src="./target/scala-3.2.2/main-launcher-fastopt.js"></script> | ||
</head> | ||
|
||
<body> | ||
<h1>Main launcher example</h1> | ||
<div data-tyrian-app="CounterApp" data-tyrian-app-flag-initial-counter="42"></div> | ||
|
||
<div data-tyrian-app="ChatApp" data-tyrian-app-flag-initial-message="Hello"></div> | ||
</body> | ||
|
||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{ | ||
"scripts": { | ||
"start": "parcel index.html --no-cache --dist-dir dist --log-level info", | ||
"build": "parcel build index.html --dist-dir dist --log-level info" | ||
}, | ||
"devDependencies": { | ||
"parcel": "^2.1.0", | ||
"process": "^0.11.10" | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
examples/main-launcher/src/main/scala/example/ChatApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package example | ||
|
||
import cats.effect.IO | ||
import tyrian.Html.* | ||
import tyrian.* | ||
|
||
import scala.scalajs.js | ||
import scala.scalajs.js.annotation.* | ||
|
||
object ChatApp extends TyrianApp[ChatAppMsg, ChatAppModel]: | ||
|
||
def init(flags: Map[String, String]): (ChatAppModel, Cmd[IO, ChatAppMsg]) = | ||
val initialChat = flags.get("InitialMessage").getOrElse("") | ||
(ChatAppModel(chatInput = initialChat, messages = Seq()), Cmd.None) | ||
|
||
def update(model: ChatAppModel): ChatAppMsg => (ChatAppModel, Cmd[IO, ChatAppMsg]) = | ||
case ChatInput(input) => (model.copy(chatInput = input), Cmd.None) | ||
case SendChat() => (model.copy(chatInput = "", messages = model.messages :+ model.chatInput), Cmd.None) | ||
|
||
def view(model: ChatAppModel): Html[ChatAppMsg] = | ||
div( | ||
ul()( | ||
for { message <- model.messages.toList } | ||
yield li()(message) | ||
), | ||
input(onInput(ChatInput.apply), value := model.chatInput), | ||
button(onClick(SendChat()))("Send Chat") | ||
) | ||
|
||
def subscriptions(model: ChatAppModel): Sub[IO, ChatAppMsg] = | ||
Sub.None | ||
|
||
case class ChatAppModel(chatInput: String, messages: Seq[String]) | ||
|
||
sealed abstract class ChatAppMsg | ||
case class ChatInput(input: String) extends ChatAppMsg | ||
case class SendChat() extends ChatAppMsg | ||
|
42 changes: 42 additions & 0 deletions
42
examples/main-launcher/src/main/scala/example/CounterApp.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package example | ||
|
||
import cats.effect.IO | ||
import tyrian.Html.* | ||
import tyrian.* | ||
|
||
import scala.scalajs.js.annotation.* | ||
import scala.util.Try | ||
|
||
object CounterApp extends TyrianApp[Msg, Model]: | ||
|
||
def init(flags: Map[String, String]): (Model, Cmd[IO, Msg]) = | ||
val initialValue: Option[Int] = for { | ||
initialCounter <- flags.get("InitialCounter") | ||
initialCounterInt <- Try(initialCounter.toInt).toOption | ||
} yield initialCounterInt | ||
(Model(initialValue.getOrElse(0)), Cmd.None) | ||
|
||
def update(model: Model): Msg => (Model, Cmd[IO, Msg]) = | ||
case Msg.Increment => (model + 1, Cmd.None) | ||
case Msg.Decrement => (model - 1, Cmd.None) | ||
|
||
def view(model: Model): Html[Msg] = | ||
div( | ||
button(onClick(Msg.Decrement))("-"), | ||
div(model.toString), | ||
button(onClick(Msg.Increment))("+") | ||
) | ||
|
||
def subscriptions(model: Model): Sub[IO, Msg] = | ||
Sub.None | ||
|
||
opaque type Model = Int | ||
object Model: | ||
def apply(value: Int): Model = value | ||
|
||
extension (i: Model) | ||
def +(other: Int): Model = i + other | ||
def -(other: Int): Model = i - other | ||
|
||
enum Msg: | ||
case Increment, Decrement |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package example | ||
|
||
import cats.effect.IO | ||
import tyrian.Html.* | ||
import tyrian.* | ||
|
||
object Main { | ||
def main(args: Array[String]): Unit = | ||
TyrianAppF.launchOnContentLoaded(Map( | ||
"CounterApp" -> (() => CounterApp), | ||
"ChatApp" -> (() => ChatApp) | ||
)) | ||
println("") | ||
} |