Skip to content

Commit

Permalink
⚗️ Add internal api for getting the root element of a running app.
Browse files Browse the repository at this point in the history
  • Loading branch information
hayleigh-dot-dev committed Oct 29, 2024
1 parent 3f4b51a commit 05f99b4
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
9 changes: 6 additions & 3 deletions src/lustre.ffi.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,9 @@ export class LustreClientApplication {
}),
);
const select = () => {};
const root = this.root;

effect({ dispatch, emit, select });
effect({ dispatch, emit, select, root });
}

// If any effects immediately dispatched a message we can process it
Expand Down Expand Up @@ -494,8 +495,9 @@ export const make_lustre_client_component = (
}),
);
const select = () => {};
const root = this.shadowRoot;

effect({ dispatch, emit, select });
effect({ dispatch, emit, select, root });
}

// If any effects immediately dispatched a message we can process it
Expand Down Expand Up @@ -693,8 +695,9 @@ export class LustreServerApplication {
}),
);
const select = () => {};
const root = null;

effect({ dispatch, emit, select });
effect({ dispatch, emit, select, root });
}

// If any effects immediately dispatched a message we can process it
Expand Down
36 changes: 22 additions & 14 deletions src/lustre/effect.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

// IMPORTS ---------------------------------------------------------------------

import gleam/dynamic.{type Dynamic}
import gleam/erlang/process.{type Selector}
import gleam/json.{type Json}
import gleam/list
Expand All @@ -65,6 +66,7 @@ type Actions(msg) {
dispatch: fn(msg) -> Nil,
emit: fn(String, Json) -> Nil,
select: fn(Selector(msg)) -> Nil,
root: Dynamic,
)
}

Expand Down Expand Up @@ -101,7 +103,7 @@ type Actions(msg) {
/// }
/// ```
pub fn from(effect: fn(fn(msg) -> Nil) -> Nil) -> Effect(msg) {
use dispatch, _, _ <- custom
use dispatch, _, _, _ <- custom

effect(dispatch)
}
Expand All @@ -113,7 +115,7 @@ pub fn from(effect: fn(fn(msg) -> Nil) -> Nil) -> Effect(msg) {
///
@internal
pub fn event(name: String, data: Json) -> Effect(msg) {
use _, emit, _ <- custom
use _, emit, _, _ <- custom

emit(name, data)
}
Expand All @@ -122,12 +124,17 @@ pub fn event(name: String, data: Json) -> Effect(msg) {
///
@internal
pub fn custom(
run: fn(fn(msg) -> Nil, fn(String, Json) -> Nil, fn(Selector(msg)) -> Nil) ->
run: fn(
fn(msg) -> Nil,
fn(String, Json) -> Nil,
fn(Selector(msg)) -> Nil,
Dynamic,
) ->
Nil,
) -> Effect(msg) {
Effect([
fn(actions: Actions(msg)) {
run(actions.dispatch, actions.emit, actions.select)
run(actions.dispatch, actions.emit, actions.select, actions.root)
},
])
}
Expand Down Expand Up @@ -172,15 +179,14 @@ pub fn map(effect: Effect(a), f: fn(a) -> b) -> Effect(b) {
Effect({
use eff <- list.map(effect.all)
fn(actions: Actions(b)) {
eff(
Actions(
dispatch: fn(msg) { actions.dispatch(f(msg)) },
emit: actions.emit,
select: fn(selector) {
actions.select(process.map_selector(selector, f))
},
),
)
eff(Actions(
dispatch: fn(msg) { actions.dispatch(f(msg)) },
emit: actions.emit,
select: fn(selector) {
actions.select(process.map_selector(selector, f))
},
root: actions.root,
))
}
})
}
Expand All @@ -204,6 +210,7 @@ pub fn map(effect: Effect(a), f: fn(a) -> b) -> Effect(b) {
// we have to use `@target` for this and duplicate the function but
// so be it.
select: fn(_) { Nil },
root: actions.root,
))
}
})
Expand All @@ -225,8 +232,9 @@ pub fn perform(
dispatch: fn(a) -> Nil,
emit: fn(String, Json) -> Nil,
select: fn(Selector(a)) -> Nil,
root: Dynamic,
) -> Nil {
let actions = Actions(dispatch, emit, select)
let actions = Actions(dispatch, emit, select, root)
use eff <- list.each(effect.all)

eff(actions)
Expand Down
11 changes: 11 additions & 0 deletions src/lustre/element.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@

// IMPORTS ---------------------------------------------------------------------

import gleam/dynamic.{type Dynamic}
import gleam/list
import gleam/string
import gleam/string_builder.{type StringBuilder}
import lustre/attribute.{type Attribute, attribute}
import lustre/effect.{type Effect}
import lustre/internals/vdom.{Element, Map, Text}

// TYPES -----------------------------------------------------------------------
Expand Down Expand Up @@ -289,6 +291,15 @@ pub fn map(element: Element(a), f: fn(a) -> b) -> Element(b) {
}
}

// EFFECTS ---------------------------------------------------------------------

@internal
pub fn get_root(effect: fn(fn(msg) -> Nil, Dynamic) -> Nil) -> Effect(msg) {
use dispatch, _, _, root <- effect.custom

effect(dispatch, root)
}

// CONVERSIONS -----------------------------------------------------------------

/// Convert a Lustre `Element` to a string. This is _not_ pretty-printed, so
Expand Down
2 changes: 1 addition & 1 deletion src/lustre/internals/runtime.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ 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)) }
let select = fn(selector) { actor.send(self, SetSelector(selector)) }
let root = fn(_) { Nil }
let root = unsafe_coerce(Nil)

effect.perform(effects, dispatch, emit, select, root)
}
Expand Down
2 changes: 1 addition & 1 deletion src/lustre/server_component.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub fn select(
fn do_select(
sel: fn(fn(msg) -> Nil, Subject(a)) -> Selector(msg),
) -> Effect(msg) {
use dispatch, _, select <- effect.custom
use dispatch, _, select, _ <- effect.custom
let self = process.new_subject()
let selector = sel(dispatch, self)

Expand Down

0 comments on commit 05f99b4

Please sign in to comment.