diff options
author | Hayleigh Thompson <me@hayleigh.dev> | 2024-03-11 06:26:43 +0100 |
---|---|---|
committer | Hayleigh Thompson <me@hayleigh.dev> | 2024-03-12 08:04:11 +0100 |
commit | 6090aff38e3fb3712524196f86a2f85ca91780ea (patch) | |
tree | 85d85a7db599294dedfdb41a09e1839f83de532f /src | |
parent | 2902de25833338c0298afb0b959ab193d2a5e46e (diff) | |
download | lustre-6090aff38e3fb3712524196f86a2f85ca91780ea.tar.gz lustre-6090aff38e3fb3712524196f86a2f85ca91780ea.zip |
:recycle: Refactor event handlers as proper decoders.
Diffstat (limited to 'src')
-rw-r--r-- | src/lustre/event.gleam | 7 | ||||
-rw-r--r-- | src/lustre/internals/patch.gleam | 12 | ||||
-rw-r--r-- | src/lustre/internals/runtime.gleam | 2 | ||||
-rw-r--r-- | src/lustre/internals/vdom.gleam | 8 |
4 files changed, 12 insertions, 17 deletions
diff --git a/src/lustre/event.gleam b/src/lustre/event.gleam index 2250057..e8c8b0d 100644 --- a/src/lustre/event.gleam +++ b/src/lustre/event.gleam @@ -6,7 +6,7 @@ // IMPORTS --------------------------------------------------------------------- -import gleam/dynamic.{type DecodeError, type Dynamic} +import gleam/dynamic.{type DecodeError, type Decoder, type Dynamic} import gleam/json.{type Json} import gleam/result import lustre/attribute.{type Attribute} @@ -39,10 +39,7 @@ pub fn emit(event: String, data: Json) -> Effect(msg) { /// If you're listening for non-standard events (like those emitted by a custom /// element) their event names might be slightly different. /// -pub fn on( - name: String, - handler: fn(Dynamic) -> Result(msg, error), -) -> Attribute(msg) { +pub fn on(name: String, handler: Decoder(msg)) -> Attribute(msg) { attribute.on(name, handler) } diff --git a/src/lustre/internals/patch.gleam b/src/lustre/internals/patch.gleam index a0fff47..b93a9a6 100644 --- a/src/lustre/internals/patch.gleam +++ b/src/lustre/internals/patch.gleam @@ -2,7 +2,7 @@ import gleam/bool import gleam/dict.{type Dict} -import gleam/dynamic.{type Dynamic} +import gleam/dynamic.{type Decoder} import gleam/int import gleam/json.{type Json} import gleam/list @@ -27,7 +27,7 @@ pub type ElementDiff(msg) { created: Dict(String, Element(msg)), removed: Set(String), updated: Dict(String, AttributeDiff(msg)), - handlers: Dict(String, fn(Dynamic) -> Result(msg, Nil)), + handlers: Dict(String, Decoder(msg)), ) } @@ -35,7 +35,7 @@ pub type AttributeDiff(msg) { AttributeDiff( created: Set(Attribute(msg)), removed: Set(String), - handlers: Dict(String, fn(Dynamic) -> Result(msg, Nil)), + handlers: Dict(String, Decoder(msg)), ) } @@ -330,7 +330,7 @@ fn attribute_dict( fn event_handler( attribute: Attribute(msg), -) -> Result(#(String, fn(Dynamic) -> Result(msg, Nil)), Nil) { +) -> Result(#(String, Decoder(msg)), Nil) { case attribute { Attribute(_, _, _) -> Error(Nil) Event(name, handler) -> { @@ -342,10 +342,10 @@ fn event_handler( } fn fold_event_handlers( - handlers: Dict(String, fn(Dynamic) -> Result(msg, Nil)), + handlers: Dict(String, Decoder(msg)), element: Element(msg), key: String, -) -> Dict(String, fn(Dynamic) -> Result(msg, Nil)) { +) -> Dict(String, Decoder(msg)) { case element { Text(_) -> handlers Map(subtree) -> fold_event_handlers(handlers, subtree(), key) diff --git a/src/lustre/internals/runtime.gleam b/src/lustre/internals/runtime.gleam index f9c4e87..a2b5365 100644 --- a/src/lustre/internals/runtime.gleam +++ b/src/lustre/internals/runtime.gleam @@ -25,7 +25,7 @@ type State(model, msg, runtime) { view: fn(model) -> Element(msg), html: Element(msg), renderers: Dict(String, fn(Patch(msg)) -> Nil), - handlers: Dict(String, fn(Dynamic) -> Result(msg, Nil)), + handlers: Dict(String, Decoder(msg)), on_attribute_change: Dict(String, Decoder(msg)), ) } diff --git a/src/lustre/internals/vdom.gleam b/src/lustre/internals/vdom.gleam index 1bbf9fc..c803b44 100644 --- a/src/lustre/internals/vdom.gleam +++ b/src/lustre/internals/vdom.gleam @@ -32,17 +32,15 @@ pub type Attribute(msg) { // QUERIES --------------------------------------------------------------------- -pub fn handlers( - element: Element(msg), -) -> Dict(String, fn(Dynamic) -> Result(msg, Nil)) { +pub fn handlers(element: Element(msg)) -> Dict(String, Decoder(msg)) { do_handlers(element, dict.new(), "0") } fn do_handlers( element: Element(msg), - handlers: Dict(String, fn(Dynamic) -> Result(msg, Nil)), + handlers: Dict(String, Decoder(msg)), key: String, -) -> Dict(String, fn(Dynamic) -> Result(msg, Nil)) { +) -> Dict(String, Decoder(msg)) { case element { Text(_) -> handlers Map(subtree) -> do_handlers(subtree(), handlers, key) |