diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2024-02-15 17:00:10 +0000 |
---|---|---|
committer | Hayleigh Thompson <me@hayleigh.dev> | 2024-02-15 17:00:10 +0000 |
commit | 7af666e56a1a98a823c3d29201fa16d96591ae58 (patch) | |
tree | c4a88e998e9fd32c2bc70a67c402926570d76337 /src | |
parent | 1be73ee8d2206a7f1520257d81c8a5a4f3cc195e (diff) | |
download | lustre-7af666e56a1a98a823c3d29201fa16d96591ae58.tar.gz lustre-7af666e56a1a98a823c3d29201fa16d96591ae58.zip |
:construction: Fix some merge problems.
Diffstat (limited to 'src')
-rw-r--r-- | src/lustre.gleam | 2 | ||||
-rw-r--r-- | src/lustre/element.gleam | 11 | ||||
-rw-r--r-- | src/lustre/internals/runtime.gleam | 28 |
3 files changed, 26 insertions, 15 deletions
diff --git a/src/lustre.gleam b/src/lustre.gleam index 7aeb6b4..95c8939 100644 --- a/src/lustre.gleam +++ b/src/lustre.gleam @@ -161,7 +161,7 @@ import argv import gleam/bool import gleam/dict.{type Dict} -import gleam/dynamic.{type Decoder, type Dynamic} +import gleam/dynamic.{type Decoder} import gleam/erlang/process.{type Subject} import gleam/otp/actor.{type StartError} import gleam/result diff --git a/src/lustre/element.gleam b/src/lustre/element.gleam index 930dead..1c1fa68 100644 --- a/src/lustre/element.gleam +++ b/src/lustre/element.gleam @@ -17,6 +17,11 @@ import lustre/internals/patch pub type Element(msg) = vdom.Element(msg) +/// Patches are sent by server components to any connected renderers. Because +/// server components are not opinionated about your network layer or how your +/// wider application is organised, it is your responsibility to make sure a `Patch` +/// makes its way to the server component client runtime. +/// pub type Patch(msg) = patch.Patch(msg) @@ -135,6 +140,12 @@ fn escape(escaped: String, content: String) -> String { // MANIPULATIONS --------------------------------------------------------------- +/// The `Element` type is parameterised by the type of messages it can produce +/// from events. Sometimes you might end up with a fragment of HTML from another +/// library or module that produces a different type of message: this function lets +/// you map the messages produced from one type to another. +/// +/// Think of it like `list.map` or `result.map` but for HTML events! /// pub fn map(element: Element(a), f: fn(a) -> b) -> Element(b) { case element { diff --git a/src/lustre/internals/runtime.gleam b/src/lustre/internals/runtime.gleam index 646baf4..a907656 100644 --- a/src/lustre/internals/runtime.gleam +++ b/src/lustre/internals/runtime.gleam @@ -18,9 +18,9 @@ import lustre/internals/vdom /// /// -type State(runtime, model, msg) { +type State(model, msg, runtime) { State( - self: Subject(Action(runtime, msg)), + self: Subject(Action(msg, runtime)), model: model, update: fn(model, msg) -> #(model, Effect(msg)), view: fn(model) -> Element(msg), @@ -33,7 +33,7 @@ type State(runtime, model, msg) { /// /// -pub type Action(runtime, msg) { +pub type Action(msg, runtime) { AddRenderer(Dynamic, fn(Patch(msg)) -> Nil) Attrs(List(#(String, Dynamic))) Batch(List(msg), Effect(msg)) @@ -42,7 +42,7 @@ pub type Action(runtime, msg) { Emit(String, Json) Event(String, Dynamic) RemoveRenderer(Dynamic) - SetSelector(Selector(Action(runtime, msg))) + SetSelector(Selector(Action(msg, runtime))) Shutdown } @@ -61,7 +61,7 @@ pub fn start( update: fn(model, msg) -> #(model, Effect(msg)), view: fn(model) -> Element(msg), on_attribute_change: Dict(String, Decoder(msg)), -) -> Result(Subject(Action(runtime, msg)), StartError) { +) -> Result(Subject(Action(msg, runtime)), StartError) { let timeout = 1000 let init = fn() { let self = process.new_subject() @@ -89,9 +89,9 @@ pub fn start( @target(erlang) fn loop( - message: Action(runtime, msg), - state: State(runtime, model, msg), -) -> Next(Action(runtime, msg), State(runtime, model, msg)) { + message: Action(msg, runtime), + state: State(model, msg, runtime), +) -> Next(Action(msg, runtime), State(model, msg, runtime)) { case message { Attrs(attrs) -> { list.filter_map(attrs, fn(attr) { @@ -213,7 +213,7 @@ fn run_renderers( } @target(erlang) -fn run_effects(effects: Effect(msg), self: Subject(Action(runtime, msg))) -> Nil { +fn run_effects(effects: Effect(msg), self: Subject(Action(msg, runtime))) -> Nil { let dispatch = fn(msg) { actor.send(self, Dispatch(msg)) } let emit = fn(name, event) { actor.send(self, Emit(name, event)) } @@ -234,15 +234,15 @@ pub fn start( update: fn(model, msg) -> #(model, Effect(msg)), view: fn(model) -> Element(msg), on_attribute_change: Dict(String, Decoder(msg)), -) -> Result(Subject(Action(runtime, msg)), StartError) { +) -> Result(Subject(Action(msg, runtime)), StartError) { panic } @target(javascript) fn loop( - message: Action(runtime, msg), - state: State(runtime, model, msg), -) -> Next(Action(runtime, msg), State(runtime, model, msg)) { + message: Action(msg, runtime), + state: State(model, msg, runtime), +) -> Next(Action(msg, runtime), State(model, msg, runtime)) { panic } @@ -255,6 +255,6 @@ fn run_renderers( } @target(javascript) -fn run_effects(effects: Effect(msg), self: Subject(Action(runtime, msg))) -> Nil { +fn run_effects(effects: Effect(msg), self: Subject(Action(msg, runtime))) -> Nil { panic } |