aboutsummaryrefslogtreecommitdiff
path: root/src/lustre.gleam
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2023-07-10 23:11:32 +0100
committerGitHub <noreply@github.com>2023-07-10 23:11:32 +0100
commitf350db196bcab490b8a6b67f9536f0b9b7322073 (patch)
tree59664ca671be2cfdb473424ca1bf737cf12622e8 /src/lustre.gleam
parent6d314230346336ba5b452b1df39b908ffa666f45 (diff)
downloadlustre-f350db196bcab490b8a6b67f9536f0b9b7322073.tar.gz
lustre-f350db196bcab490b8a6b67f9536f0b9b7322073.zip
♻️ Replace React with diffhtml (#10)
* :wrench: Remove react dependency, add vite for running examples. * :heavy_plus_sign: Update stdlib version to 0.29 * :fire: Remove old examples. * :sparkles: Vendor diffhtml and update runtime ffi code to replace react. * :recycle: Refactor all the things now react is gone. * :memo: Remove references to react in the readme. * :sparkles: Create a simple counter example.
Diffstat (limited to 'src/lustre.gleam')
-rw-r--r--src/lustre.gleam43
1 files changed, 20 insertions, 23 deletions
diff --git a/src/lustre.gleam b/src/lustre.gleam
index 54f6c55..073d03b 100644
--- a/src/lustre.gleam
+++ b/src/lustre.gleam
@@ -2,9 +2,9 @@
// IMPORTS ---------------------------------------------------------------------
+import gleam/result
import lustre/cmd.{Cmd}
import lustre/element.{Element}
-import gleam/javascript/promise.{Promise}
// TYPES -----------------------------------------------------------------------
@@ -49,13 +49,7 @@ import gleam/javascript/promise.{Promise}
/// <small>Someone please PR the Gleam docs generator to fix the monospace font,
/// thanks! 💖</small>
///
-pub opaque type App(model, msg) {
- App(
- init: #(model, Cmd(msg)),
- update: Update(model, msg),
- render: Render(model, msg),
- )
-}
+pub external type App(model, msg)
pub type Error {
ElementNotFound
@@ -100,11 +94,11 @@ type Render(model, msg) =
/// ```
///
pub fn element(element: Element(msg)) -> App(Nil, msg) {
- let init = #(Nil, cmd.none())
+ let init = fn() { #(Nil, cmd.none()) }
let update = fn(_, _) { #(Nil, cmd.none()) }
let render = fn(_) { element }
- App(init, update, render)
+ application(init, update, render)
}
/// If you start off with a simple `[element`](#element) app, you may find
@@ -157,14 +151,14 @@ pub fn element(element: Element(msg)) -> App(Nil, msg) {
/// ```
///
pub fn simple(
- init: model,
+ init: fn() -> model,
update: fn(model, msg) -> model,
render: fn(model) -> Element(msg),
) -> App(model, msg) {
- let init = #(init, cmd.none())
+ let init = fn() { #(init(), cmd.none()) }
let update = fn(model, msg) { #(update(model, msg), cmd.none()) }
- App(init, update, render)
+ application(init, update, render)
}
/// An evolution of a [`simple`](#simple) app that allows you to return a
@@ -208,13 +202,12 @@ pub fn simple(
/// external fn set_timeout (f: fn () -> a, delay: Int) -> Nil
/// = "" "window.setTimeout"
///```
-pub fn application(
- init: #(model, Cmd(msg)),
+pub external fn application(
+ init: fn() -> #(model, Cmd(msg)),
update: Update(model, msg),
render: Render(model, msg),
-) -> App(model, msg) {
- App(init, update, render)
-}
+) -> App(model, msg) =
+ "./lustre.ffi.mjs" "setup"
// EFFECTS ---------------------------------------------------------------------
@@ -242,12 +235,16 @@ pub fn application(
/// function from your `main` (or elsewhere) you can get events into your Lustre
/// app from the outside world.
///
-pub fn start(app: App(model, msg), selector: String) -> Promise(fn(msg) -> Nil) {
- mount(app, selector)
+pub fn start(
+ app: App(model, msg),
+ selector: String,
+) -> Result(fn(msg) -> Nil, Error) {
+ start_(app, selector)
+ |> result.replace_error(ElementNotFound)
}
-external fn mount(
+external fn start_(
app: App(model, msg),
selector: String,
-) -> Promise(fn(msg) -> Nil) =
- "./lustre.ffi.mjs" "mount"
+) -> Result(fn(msg) -> Nil, Nil) =
+ "./lustre.ffi.mjs" "start"