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>;

    // Required methods
    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
    );

    // Provided methods
    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>
    ) { ... }
}
Available on crate feature worker only.
Expand description

Declares the behaviour of a worker.

Required Associated Types§

source

type Message

Update message type.

source

type Input: Serialize + for<'de> Deserialize<'de>

Incoming message type.

source

type Output: Serialize + for<'de> Deserialize<'de>

Outgoing message type.

Required Methods§

source

fn create(scope: &WorkerScope<Self>) -> Self

Creates an instance of a worker.

source

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.

source

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§

source

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.

source

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.

source

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.

Implementors§