Trait gloo_worker::Worker
source · [−]pub trait Worker: Sized + 'static {
type Message;
type Input: Serialize + for<'de> Deserialize<'de>;
type Output: Serialize + for<'de> Deserialize<'de>;
fn create(scope: &WorkerScope<Self>) -> Self;
fn update(&mut self, scope: &WorkerScope<Self>, msg: Self::Message);
fn received(
&mut self,
scope: &WorkerScope<Self>,
msg: Self::Input,
id: HandlerId
);
fn connected(&mut self, scope: &WorkerScope<Self>, id: HandlerId) { ... }
fn disconnected(&mut self, scope: &WorkerScope<Self>, id: HandlerId) { ... }
fn destroy(
&mut self,
scope: &WorkerScope<Self>,
destruct: WorkerDestroyHandle<Self>
) { ... }
}
Expand description
Declares the behaviour of a worker.
Required Associated Types
type Input: Serialize + for<'de> Deserialize<'de>
type Input: Serialize + for<'de> Deserialize<'de>
Incoming message type.
type Output: Serialize + for<'de> Deserialize<'de>
type Output: Serialize + for<'de> Deserialize<'de>
Outgoing message type.
Required Methods
fn create(scope: &WorkerScope<Self>) -> Self
fn create(scope: &WorkerScope<Self>) -> Self
Creates an instance of a worker.
fn update(&mut self, scope: &WorkerScope<Self>, msg: Self::Message)
fn update(&mut self, scope: &WorkerScope<Self>, msg: Self::Message)
Receives an update.
This method is called when the worker send messages to itself via WorkerScope::send_message
.
fn received(&mut self, scope: &WorkerScope<Self>, msg: Self::Input, id: HandlerId)
fn received(&mut self, scope: &WorkerScope<Self>, msg: Self::Input, id: HandlerId)
Receives an input from a connected bridge.
When a bridge sends an input via WorkerBridge::send
, the worker will receive the
input via this method.
Provided Methods
fn connected(&mut self, scope: &WorkerScope<Self>, id: HandlerId)
fn connected(&mut self, scope: &WorkerScope<Self>, id: HandlerId)
New bridge created.
When a new bridge is created by WorkerSpawner::spawn
or WorkerBridge::fork
,
the worker will be notified the HandlerId
of the created bridge via this method.
fn disconnected(&mut self, scope: &WorkerScope<Self>, id: HandlerId)
fn disconnected(&mut self, scope: &WorkerScope<Self>, id: HandlerId)
Existing bridge destroyed.
When a bridge is dropped, the worker will be notified with this method.
fn destroy(
&mut self,
scope: &WorkerScope<Self>,
destruct: WorkerDestroyHandle<Self>
)
fn destroy(
&mut self,
scope: &WorkerScope<Self>,
destruct: WorkerDestroyHandle<Self>
)
Destroys the current worker.
When all bridges are dropped, the method will be invoked.
This method is provided a destroy handle where when it is dropped, the worker is closed. If the worker is closed immediately, then it can ignore the destroy handle. Otherwise hold the destroy handle until the clean up task is finished.
Note
This method will only be called after all bridges are disconnected. Attempting to send messages after this method is called will have no effect.