Struct InvocationContext

Source
pub struct InvocationContext {
    pub client: SignalRClient,
    /* private fields */
}
Expand description

The context for an invocation, providing access to arguments, the ability to complete the invocation, and a client for additional hub interactions.

The InvocationContext struct is used within callback handlers to retrieve arguments sent by the hub, return results back to the hub, and call other methods on the hub using the cloned client.

§Fields

  • client - A clone of the original SignalRClient, which can be used to call other methods of the hub inside the callback handler block.
  • invocation - The invocation details.

§Examples

let c1 = client.register("callback1".to_string(), |ctx| {
    // Retrieve the first argument as a TestEntity
    let result = ctx.argument::<TestEntity>(0);
    match result {
        Ok(entity) => {
            info!("Callback results entity: {}, {}", entity.text, entity.number);
        }
        Err(e) => {
            error!("Failed to retrieve argument: {}", e);
        }
    }
});

let c2 = client.register("callback2".to_string(), |mut ctx| {
    // Retrieve the first argument as a TestEntity
    let result = ctx.argument::<TestEntity>(0);
    match result {
        Ok(entity) => {
            info!("Callback2 results entity: {}, {}", entity.text, entity.number);
            let e2 = entity.clone();
            spawn(async move {
                // Complete the callback with a result
                let _ = ctx.complete(e2).await;
            });
        }
        Err(e) => {
            error!("Failed to retrieve argument: {}", e);
        }
    }
});

§Methods

§argument

Retrieves the argument of the given type from the invocation context.

The argument index should be a zero-based order of the argument provided by the hub call.

§Arguments

  • index - A usize specifying the zero-based index of the argument to retrieve.

§Returns

  • Result<T, String> - On success, returns the argument of type T. On failure, returns an error message as a String.

§Type Parameters

  • T - The type of the argument, which must implement DeserializeOwned and Unpin.

§Examples

let result = ctx.argument::<TestEntity>(0);
match result {
    Ok(entity) => {
        info!("Received entity: {}, {}", entity.text, entity.number);
    }
    Err(e) => {
        error!("Failed to retrieve argument: {}", e);
    }
}

§complete

Returns a specific result from the callback to the hub.

This method should be used only when the hub invokes the callback and awaits the response to arrive.

§Arguments

  • result - The result to return, which must implement Serialize.

§Returns

  • Result<(), String> - On success, returns Ok(()). On failure, returns an error message as a String.

§Type Parameters

  • T - The type of the result, which must implement Serialize.

§Examples

let result = ctx.complete(TestEntity {
    text: "completed".to_string(),
    number: 123,
}).await;
match result {
    Ok(_) => {
        info!("Callback completed successfully");
    }
    Err(e) => {
        error!("Failed to complete callback: {}", e);
    }
}

Fields§

§client: SignalRClient

Implementations§

Source§

impl InvocationContext

Source

pub fn argument<T: DeserializeOwned + Unpin>( &self, index: usize, ) -> Result<T, String>

Retrieves the argument of the given type from the invocation context.

The argument index should be a zero-based order of the argument provided by the hub call.

§Arguments
  • index - A usize specifying the zero-based index of the argument to retrieve.
§Returns
  • Result<T, String> - On success, returns the argument of type T. On failure, returns an error message as a String.
§Type Parameters
  • T - The type of the argument, which must implement DeserializeOwned and Unpin.
§Examples
let result = ctx.argument::<TestEntity>(0);
match result {
    Ok(entity) => {
        info!("Received entity: {}, {}", entity.text, entity.number);
    }
    Err(e) => {
        error!("Failed to retrieve argument: {}", e);
    }
}
Source

pub async fn complete<T: Serialize>(&mut self, result: T) -> Result<(), String>

Returns a specific result from the callback to the hub.

This method should be used only when the hub invokes the callback and awaits the response to arrive.

§Arguments
  • result - The result to return, which must implement Serialize.
§Returns
  • Result<(), String> - On success, returns Ok(()). On failure, returns an error message as a String.
§Type Parameters
  • T - The type of the result, which must implement Serialize.
§Examples
let result = ctx.complete(TestEntity {
    text: "completed".to_string(),
    number: 123,
}).await;
match result {
    Ok(_) => {
        info!("Callback completed successfully");
    }
    Err(e) => {
        error!("Failed to complete callback: {}", e);
    }
}
Source

pub fn spawn<F>(future: F)
where F: Future<Output = ()> + Send + 'static,

Spawns the given async block into a new thread.

This method is a convenience method for writing cross-platform code, as the package supports both Tokio and WASM.

§Arguments
  • future - The async block to spawn.
§Type Parameters
  • F - The type of the future, which must implement Future + Send + 'static.
§Examples
InvocationContext::spawn(async {
    // Your async code here
    info!("Async block executed");
});

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> ErasedDestructor for T
where T: 'static,