aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHayleigh Thompson <me@hayleigh.dev>2023-11-04 11:59:34 +0000
committerHayleigh Thompson <me@hayleigh.dev>2023-11-04 11:59:34 +0000
commit6775b97f6cc49c1bd66959f6fb7279382bef97dc (patch)
tree21216670ea6fef965a9b950804bbdf981d1528de
parent992156f0d19e498bb1de08f15a0d33ff490c9a7c (diff)
downloadlustre-6775b97f6cc49c1bd66959f6fb7279382bef97dc.tar.gz
lustre-6775b97f6cc49c1bd66959f6fb7279382bef97dc.zip
:bug: Fixed a bug where effect.map did not account for emitted events.
-rw-r--r--src/lustre/effect.gleam23
1 files changed, 16 insertions, 7 deletions
diff --git a/src/lustre/effect.gleam b/src/lustre/effect.gleam
index 50545c3..ea20c3a 100644
--- a/src/lustre/effect.gleam
+++ b/src/lustre/effect.gleam
@@ -4,19 +4,23 @@
// IMPORTS ---------------------------------------------------------------------
import gleam/list
+import gleam/function
// TYPES -----------------------------------------------------------------------
///
pub opaque type Effect(msg) {
- Effect(List(fn(fn(msg) -> Nil) -> Nil))
+ Effect(all: List(fn(fn(msg) -> Nil, fn(msg) -> Nil) -> Nil))
}
// CONSTRUCTORS ----------------------------------------------------------------
///
pub fn from(effect: fn(fn(msg) -> Nil) -> Nil) -> Effect(msg) {
- Effect([effect])
+ // Effects constructed with `effect.from` only get told about the `dispatch`
+ // function. If users want to emit events from a component they should use
+ // `event.emit` instead!
+ Effect([fn(dispatch, _) { effect(dispatch) }])
}
/// Typically our app's `update` function needs to return a tuple of
@@ -39,9 +43,14 @@ pub fn batch(effects: List(Effect(msg))) -> Effect(msg) {
///
pub fn map(effect: Effect(a), f: fn(a) -> b) -> Effect(b) {
- let Effect(l) = effect
- Effect(list.map(
- l,
- fn(effect) { fn(dispatch) { effect(fn(a) { dispatch(f(a)) }) } },
- ))
+ Effect({
+ use effect <- list.map(effect.all)
+
+ fn(dispatch, emit) {
+ let dispatch = function.compose(f, dispatch)
+ let emit = function.compose(f, emit)
+
+ effect(dispatch, emit)
+ }
+ })
}