Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 62 additions & 34 deletions crates/nsh-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3920,6 +3920,65 @@ impl Net {
}

impl<M: TextEngine> State<M> {
/// Hands the events taken from the core (`self.events`) out, in order:
/// those on a Rust component's nodes to the component, the rest to JS.
fn deliver_events(&mut self, script: Option<&Script>) {
let mut events = std::mem::take(&mut self.events);
for event in events.drain(..) {
match event {
JsEvent::Event(id, event, payload) => {
if matches!(event, EventKind::KeyDown | EventKind::KeyUp) {
dispatch_key(
script,
&mut self.core,
&mut self.key_path,
id,
event,
&payload,
);
} else if !self.core.component_event(id, event, &payload)
&& let Some(script) = script
{
script.dispatch_event(id, event, &payload);
}
}
JsEvent::LayoutListener {
host,
id,
listener,
frame,
} => {
self.core.component_layout(host, id, &listener, frame);
}
// Without JS nothing waits for them.
other => {
if let Some(script) = script {
script.deliver(other);
}
}
}
}
// Keep the buffer's capacity; what delivering queued stays queued.
events.append(&mut self.events);
self.events = events;
}

/// What earlier input in this batch made the core queue for JS (a Rust
/// component's `onPress` from a key, ...) reaches JS before the next
/// input's handlers run, so JS hears events in the order they happened
/// (#4256). Without this, a plain view's event, handed to JS at once,
/// would overtake them: they wait for the commit.
fn deliver_queued_events(&mut self, script: Option<&Script>) {
if !self.core.has_events() {
return;
}
// A task's posts come before its result, as in `commit`.
if let Some(script) = script {
script.run_posted();
}
self.core.take_events(&mut self.events);
self.deliver_events(script);
}
/// Ends the measured wake-up that began at `started`
/// ([`EngineControl::set_perf_monitor`]).
fn record_perf(&mut self, started: Option<Instant>) {
Expand Down Expand Up @@ -4202,40 +4261,7 @@ impl<M: TextEngine> State<M> {
// The core's events (layouts, ends reached, ...) on a Rust
// component's nodes are the component's; the rest are JS's, in
// order with Rust components' events and calls' results.
for event in self.events.drain(..) {
match event {
JsEvent::Event(id, event, payload) => {
if matches!(event, EventKind::KeyDown | EventKind::KeyUp) {
dispatch_key(
script,
&mut self.core,
&mut self.key_path,
id,
event,
&payload,
);
} else if !self.core.component_event(id, event, &payload)
&& let Some(script) = script
{
script.dispatch_event(id, event, &payload);
}
}
JsEvent::LayoutListener {
host,
id,
listener,
frame,
} => {
self.core.component_layout(host, id, &listener, frame);
}
// Without JS nothing waits for them.
other => {
if let Some(script) = script {
script.deliver(other);
}
}
}
}
self.deliver_events(script);
if let Some(script) = script {
script.take_ops(&mut self.ops);
}
Expand Down Expand Up @@ -4598,6 +4624,8 @@ fn handle<M: TextEngine>(script: Option<&Script>, state: &mut State<M>, message:
}
return true;
}
// Events earlier input queued for JS go before this one's.
state.deliver_queued_events(script);
if !matches!(&payload, EventPayload::Touch(t) if t.phase == nsh_core::TouchPhase::Move)
{
state.dispatch_mouse_moves(script);
Expand Down
81 changes: 81 additions & 0 deletions crates/nsh-engine/tests/engine/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12851,3 +12851,84 @@ fn a_context_menu_draws_its_children_on_the_desktops() {
"{os:?}"
);
}

const ORDERED: &str = r#"import { mount, signal, Text, View } from "nsh";
import { Button } from "nsh/base";

function App() {
const log = signal<string[]>([]);
const note = (s: string) => log.update((l) => [...l, s]);
return (
<View>
<View
testID="hold"
style={{ height: 20 }}
onPress={() => {
// Busy, so the next inputs reach the engine in one batch.
const until = Date.now() + 300;
while (Date.now() < until) {}
note("hold");
}}
/>
<Button title="Press" onPress={() => note("button")} />
<View testID="after" style={{ height: 20 }} onPress={() => note("after")} />
<Text>{"log " + log().join("|")}</Text>
</View>
);
}

mount(App);
"#;

/// A Rust component's event and a plain view's that come after it in the
/// same batch of input reach JS in the order they happened (#4256).
#[test]
fn a_components_event_reaches_js_before_a_later_plain_event_in_one_batch() {
let (engine, rx) = start(ORDERED);
let first = rx
.recv_timeout(Duration::from_secs(5))
.expect("first frame");
let test_id = |wanted: &str| {
first
.items
.iter()
.find_map(|i| match i {
MountItem::ViewProps { id, props } if props.test_id.as_deref() == Some(wanted) => {
Some(*id)
}
_ => None,
})
.unwrap_or_else(|| panic!("{wanted} is mounted"))
};
let (hold, after) = (test_id("hold"), test_id("after"));
let button = first
.items
.iter()
.find_map(|i| match i {
MountItem::Events { id, events }
if events.contains(EventKind::Press) && *id != hold && *id != after =>
{
Some(*id)
}
_ => None,
})
.expect("the button");
let control = engine.control();
control.dispatch_event(hold, EventKind::Press, EventPayload::None);
// JS is busy in the hold's handler: both presses wait in one batch.
std::thread::sleep(Duration::from_millis(100));
control.dispatch_event(button, EventKind::Press, EventPayload::None);
control.dispatch_event(after, EventKind::Press, EventPayload::None);
let deadline = std::time::Instant::now() + Duration::from_secs(5);
let mut last = String::new();
while last.matches('|').count() < 2 {
let batch = rx.recv_deadline(deadline).expect("the three presses' log");
if let Some(text) = batch.items.iter().find_map(|i| match i {
MountItem::Text { text, .. } if text.text.starts_with("log ") => Some(&text.text),
_ => None,
}) {
last = text.clone();
}
}
assert_eq!(last, "log hold|button|after");
}
Loading