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
159
160
161
162
163
164
165
166
167
168
169
use std::cell::RefCell;
use std::fmt;
#[cfg(feature = "futures")]
use std::future::Future;
use std::rc::Rc;
use wasm_bindgen_futures::spawn_local;
use crate::codec::Codec;
use crate::handler_id::HandlerId;
use crate::lifecycle::{WorkerLifecycleEvent, WorkerRunnable, WorkerState};
use crate::messages::FromWorker;
use crate::native_worker::{DedicatedWorker, NativeWorkerExt, WorkerSelf};
use crate::traits::Worker;
use crate::Shared;
pub struct WorkerDestroyHandle<W: Worker> {
scope: WorkerScope<W>,
}
impl<W: Worker> fmt::Debug for WorkerDestroyHandle<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("WorkerDestroyHandle<_>")
}
}
impl<W> WorkerDestroyHandle<W>
where
W: Worker,
{
pub(crate) fn new(scope: WorkerScope<W>) -> Self {
Self { scope }
}
}
impl<W> Drop for WorkerDestroyHandle<W>
where
W: Worker,
{
fn drop(&mut self) {
self.scope.send(WorkerLifecycleEvent::Destroy);
}
}
pub struct WorkerScope<W: Worker> {
state: Shared<WorkerState<W>>,
post_msg: Rc<dyn Fn(FromWorker<W>)>,
}
impl<W: Worker> fmt::Debug for WorkerScope<W> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("WorkerScope<_>")
}
}
impl<W: Worker> Clone for WorkerScope<W> {
fn clone(&self) -> Self {
WorkerScope {
state: self.state.clone(),
post_msg: self.post_msg.clone(),
}
}
}
impl<W> WorkerScope<W>
where
W: Worker,
{
pub(crate) fn new<CODEC>() -> Self
where
CODEC: Codec,
{
let post_msg = move |msg: FromWorker<W>| {
DedicatedWorker::worker_self().post_packed_message::<_, CODEC>(msg)
};
let state = Rc::new(RefCell::new(WorkerState::new()));
WorkerScope {
post_msg: Rc::new(post_msg),
state,
}
}
pub(crate) fn send(&self, event: WorkerLifecycleEvent<W>) {
let state = self.state.clone();
spawn_local(async move {
WorkerRunnable { state, event }.run();
});
}
pub fn respond(&self, id: HandlerId, output: W::Output) {
let msg = FromWorker::<W>::ProcessOutput(id, output);
(self.post_msg)(msg);
}
pub fn send_message<T>(&self, msg: T)
where
T: Into<W::Message>,
{
self.send(WorkerLifecycleEvent::Message(msg.into()));
}
pub fn callback<F, IN, M>(&self, function: F) -> Rc<dyn Fn(IN)>
where
M: Into<W::Message>,
F: Fn(IN) -> M + 'static,
{
let scope = self.clone();
let closure = move |input| {
let output = function(input).into();
scope.send(WorkerLifecycleEvent::Message(output));
};
Rc::new(closure)
}
#[cfg(feature = "futures")]
#[cfg_attr(docsrs, doc(cfg(feature = "futures")))]
pub fn callback_future<FN, FU, IN, M>(&self, function: FN) -> Rc<dyn Fn(IN)>
where
M: Into<W::Message>,
FU: Future<Output = M> + 'static,
FN: Fn(IN) -> FU + 'static,
{
let scope = self.clone();
let closure = move |input: IN| {
let future: FU = function(input);
scope.send_future(future);
};
Rc::new(closure)
}
#[cfg(feature = "futures")]
#[cfg_attr(docsrs, doc(cfg(feature = "futures")))]
pub fn send_future<F, M>(&self, future: F)
where
M: Into<W::Message>,
F: Future<Output = M> + 'static,
{
let scope = self.clone();
let js_future = async move {
let message: W::Message = future.await.into();
let cb = scope.callback(|m: W::Message| m);
(*cb)(message);
};
wasm_bindgen_futures::spawn_local(js_future);
}
}