mocklogger/
conn.rs

1// Author: D.S. Ljungmark <spider@skuggor.se>, Modio AB
2// SPDX-License-Identifier: AGPL-3.0-or-later
3use async_std::task;
4
5/// Creates an connection to the bus, and spawns a task to poll it's connection.
6pub async fn make_connection(session: bool) -> zbus::Result<zbus::Connection> {
7    let build = if session {
8        zbus::ConnectionBuilder::session()?
9    } else {
10        zbus::ConnectionBuilder::system()?
11    };
12
13    let conn = build.internal_executor(false).build().await?;
14    let conn_clone = conn.clone();
15    // Set up a tick-loop for the connections executor
16    // Otherwise it spawns one on it's own (internal_executor = true)
17    // or deadlocks (internal_executor= false)
18    task::spawn(async move {
19        loop {
20            conn_clone.executor().tick().await;
21        }
22    });
23    Ok(conn)
24}