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

Update message type.

Incoming message type.

Outgoing message type.

Required Methods

Creates an instance of a worker.

Receives an update.

This method is called when the worker send messages to itself via WorkerScope::send_message.

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

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.

Existing bridge destroyed.

When a bridge is dropped, the worker will be notified with this method.

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.

Implementors