feat(slhx): prefer render methods for views
Move view rendering onto Slot::render and KeyedSlot append/prepend/replace extension methods, leaving explicit text/html methods for non-view escape hatches. req: codegen/002 req: view/001
This commit is contained in:
@@ -27,7 +27,7 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn kanban_board_updates_generated_slot() {
|
fn kanban_board_updates_generated_slot() {
|
||||||
fn render_board() -> impl IntoEffect {
|
fn render_board() -> impl IntoEffect {
|
||||||
ui::board::slots::board.render_view(&empty_board())
|
ui::board::slots::board.render(&empty_board())
|
||||||
}
|
}
|
||||||
|
|
||||||
let effect = inspect(render_board());
|
let effect = inspect(render_board());
|
||||||
|
|||||||
@@ -131,13 +131,13 @@ async fn interact(
|
|||||||
// req: push/001 req: push/003 req: examples/001
|
// req: push/001 req: push/003 req: examples/001
|
||||||
async fn events(Query(params): Query<BTreeMap<String, String>>) -> impl IntoResponse {
|
async fn events(Query(params): Query<BTreeMap<String, String>>) -> impl IntoResponse {
|
||||||
if params.contains_key("once") {
|
if params.contains_key("once") {
|
||||||
let effect = ui::board::slots::presence.render_view(&Presence { count: 1 });
|
let effect = ui::board::slots::presence.render(&Presence { count: 1 });
|
||||||
return sse(stream::iter([Ok::<_, Infallible>(effect.into_batch(ui::BUILD_FINGERPRINT))]).boxed());
|
return sse(stream::iter([Ok::<_, Infallible>(effect.into_batch(ui::BUILD_FINGERPRINT))]).boxed());
|
||||||
}
|
}
|
||||||
|
|
||||||
let batches = stream::unfold(1_u64, |count| async move {
|
let batches = stream::unfold(1_u64, |count| async move {
|
||||||
tokio::time::sleep(Duration::from_secs(4)).await;
|
tokio::time::sleep(Duration::from_secs(4)).await;
|
||||||
let effect = ui::board::slots::presence.render_view(&Presence { count });
|
let effect = ui::board::slots::presence.render(&Presence { count });
|
||||||
Some((Ok::<_, Infallible>(effect.into_batch(ui::BUILD_FINGERPRINT)), count + 1))
|
Some((Ok::<_, Infallible>(effect.into_batch(ui::BUILD_FINGERPRINT)), count + 1))
|
||||||
})
|
})
|
||||||
.boxed();
|
.boxed();
|
||||||
@@ -199,7 +199,7 @@ fn registry(state: Arc<AppState>) -> HandlerRegistry {
|
|||||||
|
|
||||||
fn board_effects(board: &BoardState, notice: &'static str) -> impl IntoEffect {
|
fn board_effects(board: &BoardState, notice: &'static str) -> impl IntoEffect {
|
||||||
(
|
(
|
||||||
ui::board::slots::board.render_view(&board_view(board)),
|
ui::board::slots::board.render(&board_view(board)),
|
||||||
ui::board::slots::notice.text(notice),
|
ui::board::slots::notice.text(notice),
|
||||||
ui::board::forms::create_card.clear("title"),
|
ui::board::forms::create_card.clear("title"),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ mod tests {
|
|||||||
fn techdemo_uses_generated_slots_for_multi_target_updates() {
|
fn techdemo_uses_generated_slots_for_multi_target_updates() {
|
||||||
fn update() -> impl IntoEffect {
|
fn update() -> impl IntoEffect {
|
||||||
(
|
(
|
||||||
ui::control_center::slots::hero_metrics.render_view(&FastMetric { label: "fast" }),
|
ui::control_center::slots::hero_metrics.render(&FastMetric { label: "fast" }),
|
||||||
ui::control_center::slots::notice.text("typed"),
|
ui::control_center::slots::notice.text("typed"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -263,13 +263,13 @@ async fn interact(
|
|||||||
// req: push/001 req: push/003 req: examples/001
|
// req: push/001 req: push/003 req: examples/001
|
||||||
async fn events(Query(params): Query<BTreeMap<String, String>>) -> impl IntoResponse {
|
async fn events(Query(params): Query<BTreeMap<String, String>>) -> impl IntoResponse {
|
||||||
if params.contains_key("once") {
|
if params.contains_key("once") {
|
||||||
let effect = ui::control_center::slots::live_feed.render_view(&LiveFeed { tick: 1 });
|
let effect = ui::control_center::slots::live_feed.render(&LiveFeed { tick: 1 });
|
||||||
return sse(stream::iter([Ok::<_, Infallible>(effect.into_batch(ui::BUILD_FINGERPRINT))]).boxed());
|
return sse(stream::iter([Ok::<_, Infallible>(effect.into_batch(ui::BUILD_FINGERPRINT))]).boxed());
|
||||||
}
|
}
|
||||||
|
|
||||||
let batches = stream::unfold(1_u64, |tick| async move {
|
let batches = stream::unfold(1_u64, |tick| async move {
|
||||||
tokio::time::sleep(Duration::from_secs(4)).await;
|
tokio::time::sleep(Duration::from_secs(4)).await;
|
||||||
let effect = ui::control_center::slots::live_feed.render_view(&LiveFeed { tick });
|
let effect = ui::control_center::slots::live_feed.render(&LiveFeed { tick });
|
||||||
Some((Ok::<_, Infallible>(effect.into_batch(ui::BUILD_FINGERPRINT)), tick + 1))
|
Some((Ok::<_, Infallible>(effect.into_batch(ui::BUILD_FINGERPRINT)), tick + 1))
|
||||||
})
|
})
|
||||||
.boxed();
|
.boxed();
|
||||||
@@ -378,10 +378,10 @@ fn registry(shared: Arc<Shared>) -> HandlerRegistry {
|
|||||||
let mut demo = shared.demo.lock().unwrap();
|
let mut demo = shared.demo.lock().unwrap();
|
||||||
demo.log("Simulated push event produced the same EffectBatch shape");
|
demo.log("Simulated push event produced the same EffectBatch shape");
|
||||||
(
|
(
|
||||||
ui::control_center::slots::live_feed.render_view(&LiveFeed {
|
ui::control_center::slots::live_feed.render(&LiveFeed {
|
||||||
tick: demo.activity.len() as u64,
|
tick: demo.activity.len() as u64,
|
||||||
}),
|
}),
|
||||||
ui::control_center::slots::activity.render_view(&activity_view(&demo)),
|
ui::control_center::slots::activity.render(&activity_view(&demo)),
|
||||||
ui::control_center::slots::notice.text("Push simulated · no client app code"),
|
ui::control_center::slots::notice.text("Push simulated · no client app code"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -407,10 +407,10 @@ fn update_work(demo: &mut DemoState, id: Option<&str>, update: impl FnOnce(&mut
|
|||||||
|
|
||||||
fn demo_effects(demo: &DemoState, notice: &'static str) -> impl IntoEffect {
|
fn demo_effects(demo: &DemoState, notice: &'static str) -> impl IntoEffect {
|
||||||
(
|
(
|
||||||
ui::control_center::slots::hero_metrics.render_view(&hero_view(demo)),
|
ui::control_center::slots::hero_metrics.render(&hero_view(demo)),
|
||||||
ui::control_center::slots::board.render_view(&board_view(demo)),
|
ui::control_center::slots::board.render(&board_view(demo)),
|
||||||
ui::control_center::slots::activity.render_view(&activity_view(demo)),
|
ui::control_center::slots::activity.render(&activity_view(demo)),
|
||||||
ui::control_center::slots::inspector.render_view(&inspector_view(demo)),
|
ui::control_center::slots::inspector.render(&inspector_view(demo)),
|
||||||
ui::control_center::slots::notice.text(notice),
|
ui::control_center::slots::notice.text(notice),
|
||||||
ui::control_center::forms::launch_work.clear("title"),
|
ui::control_center::forms::launch_work.clear("title"),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -77,7 +77,7 @@ mod tests {
|
|||||||
fn todos_append_keyed_rows_from_form_input() {
|
fn todos_append_keyed_rows_from_form_input() {
|
||||||
fn add_todo(input: TodoInput) -> impl IntoEffect {
|
fn add_todo(input: TodoInput) -> impl IntoEffect {
|
||||||
let todo = Todo { id: 7, title: input.title };
|
let todo = Todo { id: 7, title: input.title };
|
||||||
ui::slots::todo_row.append_view(
|
ui::slots::todo_row.append(
|
||||||
todo.id.to_string(),
|
todo.id.to_string(),
|
||||||
&TodoRow { title: todo.title },
|
&TodoRow { title: todo.title },
|
||||||
)
|
)
|
||||||
@@ -121,7 +121,7 @@ mod tests {
|
|||||||
fn page_swap_updates_content_and_history() {
|
fn page_swap_updates_content_and_history() {
|
||||||
fn load_docs() -> impl IntoEffect {
|
fn load_docs() -> impl IntoEffect {
|
||||||
(
|
(
|
||||||
ui::slots::content.render_view(&DocsContent {
|
ui::slots::content.render(&DocsContent {
|
||||||
message: "This page was swapped.",
|
message: "This page was swapped.",
|
||||||
}),
|
}),
|
||||||
ui::slots::title.text("Docs"),
|
ui::slots::title.text("Docs"),
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ fn registry(state: Arc<ExampleState>) -> HandlerRegistry {
|
|||||||
todos.push(Todo { id, title: title.into() });
|
todos.push(Todo { id, title: title.into() });
|
||||||
}
|
}
|
||||||
(
|
(
|
||||||
ui::todos::slots::todo_list.render_view(&todos_view(&todos)),
|
ui::todos::slots::todo_list.render(&todos_view(&todos)),
|
||||||
ui::todos::forms::new_todo.clear("title"),
|
ui::todos::forms::new_todo.clear("title"),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -163,7 +163,7 @@ fn registry(state: Arc<ExampleState>) -> HandlerRegistry {
|
|||||||
.register_handle(ui::page_swap::handles::load_docs, |_| {
|
.register_handle(ui::page_swap::handles::load_docs, |_| {
|
||||||
// req: page_swap/002, req: examples/001
|
// req: page_swap/002, req: examples/001
|
||||||
(
|
(
|
||||||
ui::page_swap::slots::content.render_view(&DocsContent {
|
ui::page_swap::slots::content.render(&DocsContent {
|
||||||
message: "This content came from an EffectBatch.",
|
message: "This content came from an EffectBatch.",
|
||||||
}),
|
}),
|
||||||
ui::page_swap::slots::title.text("Docs"),
|
ui::page_swap::slots::title.text("Docs"),
|
||||||
|
|||||||
+3
-10
@@ -777,13 +777,6 @@ impl<T> Slot<T> {
|
|||||||
self.id
|
self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn render(self, value: T) -> Effect
|
|
||||||
where
|
|
||||||
T: ToString,
|
|
||||||
{
|
|
||||||
self.text(value)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn text(self, value: impl ToString) -> Effect {
|
pub fn text(self, value: impl ToString) -> Effect {
|
||||||
Effect::Put {
|
Effect::Put {
|
||||||
target: ResourceRef::unscoped(self.id),
|
target: ResourceRef::unscoped(self.id),
|
||||||
@@ -834,7 +827,7 @@ where
|
|||||||
self.id
|
self.id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn append(self, key: K, value: T) -> Effect
|
pub fn append_text(self, key: K, value: T) -> Effect
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
{
|
{
|
||||||
@@ -845,7 +838,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn prepend(self, key: K, value: T) -> Effect
|
pub fn prepend_text(self, key: K, value: T) -> Effect
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
{
|
{
|
||||||
@@ -856,7 +849,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn replace(self, key: K, value: T) -> Effect
|
pub fn replace_text(self, key: K, value: T) -> Effect
|
||||||
where
|
where
|
||||||
T: ToString,
|
T: ToString,
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -11,9 +11,9 @@ fn effect_batch_wire_round_trips() {
|
|||||||
let user = Atom::<String>::new(3);
|
let user = Atom::<String>::new(3);
|
||||||
|
|
||||||
let batch = (
|
let batch = (
|
||||||
count.render(2),
|
count.text(2),
|
||||||
todos.append(7, String::from("Buy milk")),
|
todos.append_text(7, String::from("Buy milk")),
|
||||||
todos.replace(7, String::from("Buy oat milk")),
|
todos.replace_text(7, String::from("Buy oat milk")),
|
||||||
user.set("Ada"),
|
user.set("Ada"),
|
||||||
navigate("/todos"),
|
navigate("/todos"),
|
||||||
event("toast", "Saved"),
|
event("toast", "Saved"),
|
||||||
@@ -32,7 +32,7 @@ fn effect_batch_wire_round_trips() {
|
|||||||
fn keyed_slot_replace_uses_scoped_resource_ref() {
|
fn keyed_slot_replace_uses_scoped_resource_ref() {
|
||||||
let todos = KeyedSlot::<u64, String>::new(9);
|
let todos = KeyedSlot::<u64, String>::new(9);
|
||||||
// req: codegen/002
|
// req: codegen/002
|
||||||
let effect = todos.replace(12, String::from("done"));
|
let effect = todos.replace_text(12, String::from("done"));
|
||||||
|
|
||||||
let Effect::Put { target, payload } = effect else {
|
let Effect::Put { target, payload } = effect else {
|
||||||
panic!("expected Put");
|
panic!("expected Put");
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ fn inspects_tuple_effects() {
|
|||||||
#[test]
|
#[test]
|
||||||
fn finds_keyed_slot_targets() {
|
fn finds_keyed_slot_targets() {
|
||||||
let rows = KeyedSlot::<u32, String>::new(9);
|
let rows = KeyedSlot::<u32, String>::new(9);
|
||||||
let inspected = slhx_test::inspect(rows.append(7, String::from("row")));
|
let inspected = slhx_test::inspect(rows.append_text(7, String::from("row")));
|
||||||
|
|
||||||
assert!(inspected.has_keyed_slot(rows));
|
assert!(inspected.has_keyed_slot(rows));
|
||||||
assert_eq!(inspected.ops().len(), 1);
|
assert_eq!(inspected.ops().len(), 1);
|
||||||
|
|||||||
+36
-8
@@ -14,34 +14,62 @@ pub fn render_html(view: &impl hemplate::Hemplate) -> SafeHtml {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait RenderSlotExt {
|
pub trait RenderSlotExt {
|
||||||
fn render_view(self, view: &impl hemplate::Hemplate) -> Effect;
|
fn render(self, view: &impl hemplate::Hemplate) -> Effect;
|
||||||
|
|
||||||
|
fn render_view(self, view: &impl hemplate::Hemplate) -> Effect
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
self.render(view)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> RenderSlotExt for Slot<T> {
|
impl<T> RenderSlotExt for Slot<T> {
|
||||||
fn render_view(self, view: &impl hemplate::Hemplate) -> Effect {
|
fn render(self, view: &impl hemplate::Hemplate) -> Effect {
|
||||||
self.html(render_html(view))
|
self.html(render_html(view))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait RenderKeyedSlotExt<K> {
|
pub trait RenderKeyedSlotExt<K> {
|
||||||
fn append_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
fn append(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
||||||
fn prepend_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
fn prepend(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
||||||
fn replace_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
fn replace(self, key: K, view: &impl hemplate::Hemplate) -> Effect;
|
||||||
|
|
||||||
|
fn append_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
self.append(key, view)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn prepend_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
self.prepend(key, view)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn replace_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect
|
||||||
|
where
|
||||||
|
Self: Sized,
|
||||||
|
{
|
||||||
|
self.replace(key, view)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<K, T> RenderKeyedSlotExt<K> for KeyedSlot<K, T>
|
impl<K, T> RenderKeyedSlotExt<K> for KeyedSlot<K, T>
|
||||||
where
|
where
|
||||||
K: ToString,
|
K: ToString,
|
||||||
{
|
{
|
||||||
fn append_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
fn append(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
||||||
self.append_html(key, render_html(view))
|
self.append_html(key, render_html(view))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prepend_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
fn prepend(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
||||||
self.prepend_html(key, render_html(view))
|
self.prepend_html(key, render_html(view))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn replace_view(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
fn replace(self, key: K, view: &impl hemplate::Hemplate) -> Effect {
|
||||||
self.replace_html(key, render_html(view))
|
self.replace_html(key, render_html(view))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user