pub struct ServiceContext<S: Service> { /* private fields */ }
Expand description
Backing context for a service which handles storing the reciever for messaging and the original link for spawning copies
Implementations§
Source§impl<S: Service> ServiceContext<S>
impl<S: Service> ServiceContext<S>
Sourcepub fn attach_sink<Si, I>(&self, sink: Si) -> SinkLink<I>
pub fn attach_sink<Si, I>(&self, sink: Si) -> SinkLink<I>
Attaches a sink to this service and provides a link to the service so that it can be used to write messages
sink
The sink to attach
Source§impl<S> ServiceContext<S>where
S: Service,
impl<S> ServiceContext<S>where
S: Service,
Sourcepub fn attach_stream<St>(&self, stream: St, stop: bool)
pub fn attach_stream<St>(&self, stream: St, stop: bool)
Attaches a streaming reciever to the service context
implement the StreamHandler trait on your service with the item as the Item type of the provided stream in order to handle accepting items from the stream
stream
The stream to accept from
stop
Whether to stop the main service when this stream service ends
Source§impl<S> ServiceContext<S>where
S: Service,
impl<S> ServiceContext<S>where
S: Service,
Sourcepub fn stop(&mut self)
pub fn stop(&mut self)
Stop the context directly by closing the reciever the reciever will drain any existing messages until there are none remaining
Returns a reference to the shared link used by this context for creating new links. You can use this to access the service link without creating a clone of it
Sourcepub fn run_later<F>(&self, duration: Duration, action: F) -> JoinHandle<()>
pub fn run_later<F>(&self, duration: Duration, action: F) -> JoinHandle<()>
Executes the provided action
function on the service and service context
after the provided duration
has elapsed. This function returns a JoinHandle
which you can use to stop the task by calling .abort() on the handle
use interlink::prelude::*;
use std::time::Duration;
use tokio::time::sleep;
struct Test {
value: u32,
}
impl Service for Test {
fn started(&mut self, ctx: &mut ServiceContext<Self>) {
ctx.run_later(Duration::from_secs(1), |service, _ctx| {
println!("Hello 1 second later from the service: {}", service.value);
service.value += 1;
});
}
}
#[tokio::test]
async fn test() {
let link = Test { value: 1 }.start();
sleep(Duration::from_secs(5)).await;
}
Sourcepub fn run_interval<F>(&self, duration: Duration, action: F) -> JoinHandle<()>
pub fn run_interval<F>(&self, duration: Duration, action: F) -> JoinHandle<()>
Executes the provided action
function on the service and service context
every time the duration
is elapsed. This function returns a JoinHandle
which you can use to stop the task by calling .abort() on the handle
use interlink::prelude::*;
use std::time::Duration;
use tokio::time::sleep;
struct Test {
value: u32,
}
impl Service for Test {
fn started(&mut self, ctx: &mut ServiceContext<Self>) {
ctx.run_interval(Duration::from_secs(1), |service, _ctx| {
println!(
"Hello at 1 second interval from the service: {}",
service.value
);
service.value += 1;
});
}
}
#[tokio::test]
async fn test() {
let link = Test { value: 1 }.start();
sleep(Duration::from_secs(15)).await;
}