MCPcopy Create free account
hub / github.com/endbasic/endbasic / do_message_channel_yield

Function do_message_channel_yield

web/src/lib.rs:92–117  ·  view source on GitHub ↗

Yields execution until the browser processes one message-channel event.

()

Source from the content-addressed store, hash-verified

90
91/// Yields execution until the browser processes one message-channel event.
92fn do_message_channel_yield() -> Pin<Box<dyn Future<Output = ()>>> {
93 let (message_tx, message_rx) = async_channel::unbounded();
94 let channel = match web_sys::MessageChannel::new() {
95 Ok(channel) => channel,
96 Err(e) => log_and_panic!("Failed to create message channel: {:?}", e),
97 };
98 let port1 = channel.port1();
99 let port2 = channel.port2();
100 let callback = Closure::wrap(Box::new(move |_event: web_sys::MessageEvent| {
101 message_tx.try_send(()).expect("Send must succeed")
102 }) as Box<dyn FnMut(web_sys::MessageEvent)>);
103 port1.set_onmessage(Some(callback.as_ref().unchecked_ref()));
104 if let Err(e) = port2.post_message(&JsValue::NULL) {
105 log_and_panic!("Failed to post message to port: {:?}", e);
106 }
107
108 Box::pin(async move {
109 let _callback = callback; // Must grab ownership so that the closure remains alive until it is used.
110 if let Err(e) = message_rx.recv().await {
111 log_and_panic!("Failed to wait for message channel event: {}", e);
112 }
113 port1.set_onmessage(None);
114 port1.close();
115 port2.close();
116 })
117}
118
119/// Type of yielding decision taken by the `WebYielder`.
120#[derive(Debug, Eq, PartialEq)]

Callers 2

yield_nowMethod · 0.85

Calls 1

recvMethod · 0.80

Tested by 1