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 originalSignalRClient
, 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
- Ausize
specifying the zero-based index of the argument to retrieve.
§Returns
Result<T, String>
- On success, returns the argument of typeT
. On failure, returns an error message as aString
.
§Type Parameters
T
- The type of the argument, which must implementDeserializeOwned
andUnpin
.
§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 implementSerialize
.
§Returns
Result<(), String>
- On success, returnsOk(())
. On failure, returns an error message as aString
.
§Type Parameters
T
- The type of the result, which must implementSerialize
.
§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
impl InvocationContext
Sourcepub fn argument<T: DeserializeOwned + Unpin>(
&self,
index: usize,
) -> Result<T, String>
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
- Ausize
specifying the zero-based index of the argument to retrieve.
§Returns
Result<T, String>
- On success, returns the argument of typeT
. On failure, returns an error message as aString
.
§Type Parameters
T
- The type of the argument, which must implementDeserializeOwned
andUnpin
.
§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);
}
}
Sourcepub async fn complete<T: Serialize>(&mut self, result: T) -> Result<(), String>
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 implementSerialize
.
§Returns
Result<(), String>
- On success, returnsOk(())
. On failure, returns an error message as aString
.
§Type Parameters
T
- The type of the result, which must implementSerialize
.
§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);
}
}
Sourcepub fn spawn<F>(future: F)
pub fn spawn<F>(future: F)
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 implementFuture
+Send
+'static
.
§Examples
InvocationContext::spawn(async {
// Your async code here
info!("Async block executed");
});