1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//! Response context for worker actors.
//!
//! On WASM, responses are posted to the main thread via `postMessage`.
//! On native (non-WASM), responses are collected in memory for testing.
// ── Native implementation (for testing) ──────────────────────
#[cfg(not(target_arch = "wasm32"))]
mod native_impl {
use std::cell::RefCell;
use std::rc::Rc;
struct ContextInner<Evt> {
bytes: Option<Vec<u8>>,
responses: RefCell<Vec<(Evt, Option<Vec<u8>>)>>,
}
/// Response context for dispatching events back to the main thread.
///
/// On native targets, responses are collected in memory for testing.
/// `Clone + 'static` — safe to move into spawned tasks.
pub struct Context<Evt> {
inner: Rc<ContextInner<Evt>>,
}
impl<Evt> Clone for Context<Evt> {
fn clone(&self) -> Self {
Self {
inner: Rc::clone(&self.inner),
}
}
}
impl<Evt> Context<Evt> {
/// Create a test context with optional incoming bytes.
pub fn new(bytes: Option<Vec<u8>>) -> Self {
Self {
inner: Rc::new(ContextInner {
bytes,
responses: RefCell::new(Vec::new()),
}),
}
}
/// Access the binary payload from the incoming command (if any).
pub fn bytes(&self) -> Option<&[u8]> {
self.inner.bytes.as_deref()
}
/// Send an event back to the main thread.
pub fn respond(&self, evt: Evt) {
self.inner.responses.borrow_mut().push((evt, None));
}
/// Send an event with a binary sidecar back to the main thread.
pub fn respond_bytes(&self, evt: Evt, bytes: Vec<u8>) {
self.inner.responses.borrow_mut().push((evt, Some(bytes)));
}
/// Number of responses sent so far.
pub fn response_count(&self) -> usize {
self.inner.responses.borrow().len()
}
}
impl<Evt: Clone> Context<Evt> {
/// Collect all responses (test helper).
pub fn responses(&self) -> Vec<(Evt, Option<Vec<u8>>)> {
self.inner.responses.borrow().clone()
}
}
}
#[cfg(not(target_arch = "wasm32"))]
pub use native_impl::*;
// ── WASM implementation ──────────────────────────────────────
#[cfg(target_arch = "wasm32")]
mod wasm_impl {
use std::cell::Cell;
use std::marker::PhantomData;
use std::rc::Rc;
use serde::Serialize;
struct ContextInner {
correlation_id: Option<u64>,
bytes: Option<Vec<u8>>,
replied_correlated: Cell<bool>,
}
/// Response context for dispatching events back to the main thread.
///
/// `Clone + 'static` — safe to move into spawned tasks on the Worker.
pub struct Context<Evt> {
inner: Rc<ContextInner>,
_phantom: PhantomData<fn(Evt)>,
}
impl<Evt> Clone for Context<Evt> {
fn clone(&self) -> Self {
Self {
inner: Rc::clone(&self.inner),
_phantom: PhantomData,
}
}
}
impl<Evt> Context<Evt> {
pub(crate) fn new(correlation_id: Option<u64>, bytes: Option<Vec<u8>>) -> Self {
Self {
inner: Rc::new(ContextInner {
correlation_id,
bytes,
replied_correlated: Cell::new(false),
}),
_phantom: PhantomData,
}
}
/// Access the binary payload from the incoming command (if any).
pub fn bytes(&self) -> Option<&[u8]> {
self.inner.bytes.as_deref()
}
}
#[allow(clippy::needless_pass_by_value)] // Taking ownership mirrors the main-thread API.
impl<Evt: Serialize + 'static> Context<Evt> {
/// Take the correlation ID for the first reply (RPC routing).
fn take_correlation_id(&self) -> Option<u64> {
if self.inner.replied_correlated.get() {
return None;
}
self.inner.replied_correlated.set(true);
self.inner.correlation_id
}
/// Send an event back to the main thread.
pub fn respond(&self, evt: Evt) {
let corr_id = self.take_correlation_id();
if let Err(e) = crate::transfer::post_to_main(corr_id, &evt, None) {
tracing::error!("respond failed: {e}");
}
}
/// Send an event with a binary sidecar back to the main thread.
pub fn respond_bytes(&self, evt: Evt, bytes: Vec<u8>) {
let corr_id = self.take_correlation_id();
if let Err(e) = crate::transfer::post_to_main(corr_id, &evt, Some(&bytes)) {
tracing::error!("respond failed: {e}");
}
}
}
}
#[cfg(target_arch = "wasm32")]
pub use wasm_impl::*;