From bfe53477247adacec7e04cfd391673c30b071994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bastian=20K=C3=B6cher?= Date: Tue, 2 Jul 2019 10:39:41 +0200 Subject: [PATCH] Adds a `SpawnTaskHandle` to core service (#2992) * Adds a `SpawnTaskHandle` to core service * Fixes compilation * Remove `into()` --- substrate/core/service/src/lib.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/substrate/core/service/src/lib.rs b/substrate/core/service/src/lib.rs index 4e6fb35508..3cadd1d406 100644 --- a/substrate/core/service/src/lib.rs +++ b/substrate/core/service/src/lib.rs @@ -110,6 +110,19 @@ pub fn new_client(config: &FactoryFullConfi Ok(client) } +/// An handle for spawning tasks in the service. +#[derive(Clone)] +pub struct SpawnTaskHandle { + sender: mpsc::UnboundedSender + Send>>, +} + +impl SpawnTaskHandle { + /// Spawn a task to run the given future. + pub fn spawn_task(&self, task: impl Future + Send + 'static) { + let _ = self.sender.unbounded_send(Box::new(task)); + } +} + /// Stream of events for connection established to a telemetry server. pub type TelemetryOnConnectNotifications = mpsc::UnboundedReceiver<()>; @@ -512,8 +525,15 @@ impl Service { } /// Spawns a task in the background that runs the future passed as parameter. - pub fn spawn_task(&self, task: Box + Send>) { - let _ = self.to_spawn_tx.unbounded_send(task); + pub fn spawn_task(&self, task: impl Future + Send + 'static) { + let _ = self.to_spawn_tx.unbounded_send(Box::new(task)); + } + + /// Returns a handle for spawning tasks. + pub fn spawn_task_handle(&self) -> SpawnTaskHandle { + SpawnTaskHandle { + sender: self.to_spawn_tx.clone(), + } } /// Get shared client instance.