Skip to content

Commit 6669715

Browse files
committed
Add method to start the node without tokio
Required for users without a `Runtime`, e.g. the bindings. ref: https://github.com/bitcoindevkit/bdk-ffi/blob/master/bdk-ffi/src/kyoto.rs#L70
1 parent 5699e6e commit 6669715

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/lib.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,37 @@ impl LightClient<Idle> {
141141
};
142142
(client, requester, logging)
143143
}
144+
145+
/// Start the client on a detached operating system thread. This method is used when there is
146+
/// no [`tokio::runtime::Runtime`] available.
147+
///
148+
/// # Returns
149+
///
150+
/// - A [`LightClient`] in [`Active`] state.
151+
/// - A [`Requester`] to publish transactions or fetch data.
152+
/// - [`LoggingSubscribers`] to process messages from the sync process.
153+
pub fn start_detached_thread(mut self) -> (LightClient<Active>, Requester, LoggingSubscribers) {
154+
let logging = core::mem::take(&mut self.logging).expect("node starts once");
155+
let requester = core::mem::take(&mut self.requester).expect("node starts once");
156+
let node = core::mem::take(&mut self.node).expect("node starts once");
157+
std::thread::spawn(|| {
158+
tokio::runtime::Builder::new_multi_thread()
159+
.enable_all()
160+
.build()
161+
.unwrap()
162+
.block_on(async move {
163+
let _ = node.run().await;
164+
})
165+
});
166+
let client = LightClient {
167+
requester: None,
168+
logging: None,
169+
update_subscriber: self.update_subscriber,
170+
node: None,
171+
_marker: core::marker::PhantomData,
172+
};
173+
(client, requester, logging)
174+
}
144175
}
145176

146177
impl LightClient<Active> {

0 commit comments

Comments
 (0)