perf(wire): pre-size effect batch encoding

This commit is contained in:
slhx agent
2026-07-13 09:32:46 +02:00
parent ecbdea5cd0
commit 945122cb02
4 changed files with 189 additions and 2 deletions
+58 -1
View File
@@ -1,7 +1,8 @@
use hemx_core::{
event, navigate, redirect, replace, Atom, AtomSnapshot, AtomState, BuildFingerprint,
ComponentRef, CssClass, CssClasses, Effect, EffectBatch, Form, Handle, IntoEffect, KeyedSlot,
NavigateMode, ParamName, Payload, ResourceKind, SafeHtml, ScopeKey, Slot,
NavigateMode, ParamName, Payload, ResourceId, ResourceKind, ResourceRef, SafeHtml, ScopeKey,
ScrollBehavior, Slot,
};
#[test]
@@ -22,12 +23,68 @@ fn effect_batch_wire_round_trips() {
let bytes = batch.to_wire();
assert_eq!(&bytes[..4], b"HEMX");
assert_eq!(batch.encoded_len(), bytes.len()); // req: wire/007
let decoded = EffectBatch::from_wire(&bytes).unwrap();
assert_eq!(decoded, batch);
assert!(decoded.is_compatible());
}
#[test]
fn encoded_len_covers_every_effect_shape() {
let unscoped = ResourceRef::unscoped(ResourceId::new(ResourceKind::Slot, 1));
let scoped = ResourceRef::scoped(
ResourceId::new(ResourceKind::Form, 2),
ScopeKey::Field(String::from("email")),
);
let batch = EffectBatch {
abi_version: hemx_core::EFFECT_BATCH_ABI_VERSION,
fingerprint: BuildFingerprint(42),
ops: vec![
Effect::Put {
target: unscoped.clone(),
payload: Payload::Html(String::from("<p>safe</p>")),
},
Effect::Insert {
target: scoped.clone(),
key: String::from("insert"),
payload: Payload::Text(String::from("one")),
},
Effect::Prepend {
target: scoped.clone(),
key: String::from("prepend"),
payload: Payload::Text(String::from("two")),
},
Effect::Remove {
target: scoped.clone(),
key: Some(String::from("remove")),
},
Effect::Move {
target: scoped.clone(),
key: String::from("move"),
before: Some(String::from("before")),
},
Effect::Focus {
target: scoped.clone(),
},
Effect::Navigate {
url: String::from("/next"),
mode: NavigateMode::Replace,
scroll: ScrollBehavior::Element(unscoped),
title: Some(String::from("Next")),
},
Effect::Emit {
name: String::from("notice"),
payload: String::from("saved"),
},
],
};
let bytes = batch.to_wire();
assert_eq!(batch.encoded_len(), bytes.len()); // req: wire/007
assert_eq!(EffectBatch::from_wire(&bytes).unwrap(), batch);
}
#[test]
fn optional_effects_compose_into_batches() {
// req: component/005 req: public_api/003