feat: dispatch managed events through capture and bubble subscriptions
Verify Tessera GUI / verify (push) Failing after 7m13s

This commit is contained in:
2026-10-21 02:20:00 +08:00
parent 6c42e5dde4
commit 8886bdbd2d
11 changed files with 767 additions and 46 deletions
+80 -29
View File
@@ -31,6 +31,7 @@ pub const TYPE_NAMES: &[&str] = &[
"Page",
"Mount",
"Event",
"Subscription",
];
pub const QUERY_NAMES: &[&str] = &["isInstance", "isSubtype"];
@@ -206,6 +207,9 @@ enum Operation {
Back,
Forward,
OnClick,
On,
Dispatch,
OnEvent,
Click,
Present,
}
@@ -487,6 +491,9 @@ impl NativeCallable for Function {
Replace | Move => (2, 2),
Page | Unmount | Open | Close | Activate | Back | Forward | Children => (0, 0),
Click => (0, 1),
On => (2, 3),
Dispatch => (1, 2),
OnEvent => (1, 1),
};
if !(minimum..=maximum).contains(&args.len()) {
return Err(invalid("incorrect UI method argument count"));
@@ -869,38 +876,64 @@ impl NativeCallable for Function {
data.click = Some(handler);
result = Value::Object(owner);
}
Click => {
let Some(handler) = data.click else {
return Ok(NativeResult::Return(Value::Nil));
On => {
let kind = text(args.first())?;
if kind.is_empty() || kind.len() > 128 {
return Err(invalid("invalid event kind"));
}
let handler = object(args.get(1))?;
let capture = match args.get(2) {
None | Some(Value::Bool(false)) => false,
Some(Value::Bool(true)) => true,
Some(Value::Text(phase)) if phase == "capture" => true,
Some(Value::Text(phase)) if phase == "bubble" => false,
_ => return Err(invalid("phase must be capture or bubble")),
};
let event = cx.allocate(crate::events::Event)?;
attach_named_type(cx, event, "Event", &self.session)?;
cx.set_attr(event, "kind", Value::Text("click".into()))?;
cx.set_attr(event, "target", Value::Object(owner))?;
let context = if let Some(value) = args.first() {
let mount = object(Some(value))?;
let data = cx.store().payload::<crate::mount::Mount>(mount, "Mount")?;
if !data.mounted || data.block != owner {
return Err(invalid("click context does not belong to this Block"));
let mut active = Vec::new();
for token in &data.subscriptions {
if cx
.store()
.payload::<crate::event_dispatch::Connection>(*token, "Connection")?
.handler
.is_some()
{
active.push(*token);
}
Value::Object(mount)
}
if active.len() >= MAX_UI_NODES {
return Err(invalid("too many event subscriptions"));
}
let connection = cx.allocate(crate::event_dispatch::Connection {
kind,
handler: Some(handler),
capture,
})?;
let token = cx.allocate(crate::event_dispatch::Subscription {
connection: cx.store().downgrade(connection)?,
})?;
attach_named_type(cx, token, "Subscription", &self.session)?;
active.push(connection);
data.subscriptions = active;
result = Value::Object(token);
}
OnEvent => return Ok(NativeResult::Return(Value::Nil)),
Click | Dispatch => {
let desktop = self
.session
.lock()
.map_err(|_| invalid("UI session poisoned"))?
.desktop;
let (event, mount) = if matches!(self.operation, Click) {
let definition = desktop_type(cx.store(), "Event", desktop)?;
(
crate::event_dispatch::new_event(cx, definition, "click")?,
args.first(),
)
} else {
Value::Nil
(object(args.first())?, args.get(1))
};
cx.set_attr(event, "mount", context)?;
let arguments = if cx
.store()
.positional_parameter_count(handler)?
.is_some_and(|count| count > 0)
{
vec![Value::Object(event)]
} else {
vec![]
};
return Ok(NativeResult::Invoke {
function: handler,
arguments,
});
let mount = mount.map(|value| object(Some(value))).transpose()?;
return crate::event_dispatch::dispatch(cx, owner, event, mount, desktop);
}
_ => return Err(invalid("unsupported Block operation")),
}
@@ -1175,10 +1208,22 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
("Button", "ContainerBlock", Some("button")),
("Page", "ContainerBlock", Some("page")),
("Mount", "Object", None),
("Event", "Object", None),
("Event", "Object", Some("event")),
("Subscription", "Object", None),
] {
let base = object(Some(&vm.get_attr(ui, base_name)?))?;
let constructor = match constructor_name {
Some("event") => {
let factory = vm.allocate(ContextNativeFunction::new(
"event",
crate::event_dispatch::Constructor,
vec![],
))?;
roots.push(factory);
let function_type = object(Some(&vm.get_attr(ui, "Function")?))?;
vm.attach_type(factory, function_type)?;
TypeConstructor::NativeTyped(factory)
}
Some(constructor_name) => {
let operation = match constructor_name {
"text" => Operation::Text,
@@ -1243,6 +1288,9 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
for (type_name, name, operation) in [
("Block", "mounts", Operation::Mounts),
("Block", "onClick", Operation::OnClick),
("Block", "on", Operation::On),
("Block", "dispatch", Operation::Dispatch),
("Block", "onEvent", Operation::OnEvent),
("Block", "click", Operation::Click),
("TextBlock", "setText", Operation::SetText),
("TextBlock", "setColor", Operation::SetColor),
@@ -1275,6 +1323,9 @@ pub fn install(vm: &mut Vm, roots: &mut Vec<ObjectRef>) -> Result<UiBridge> {
vm.attach_type(method, function_type)?;
vm.define_method(expected, name, method)?;
}
let event_type = object(Some(&vm.get_attr(ui, "Event")?))?;
let token_type = object(Some(&vm.get_attr(ui, "Subscription")?))?;
crate::event_dispatch::install(vm, roots, event_type, token_type, function_type)?;
Ok(UiBridge { ui, session })
}