Skip to content

Commit 91616f6

Browse files
author
Ludo Galabru
committed
feat: introduce terminate function
1 parent 654fead commit 91616f6

File tree

3 files changed

+30
-12
lines changed

3 files changed

+30
-12
lines changed

components/ordhook-sdk-js/lib/index.ts

+8-7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const {
99
ordinalsIndexerRewriteBlocks,
1010
ordinalsIndexerOnBlockApply,
1111
ordinalsIndexerOnBlockUndo,
12+
ordinalsIndexerTerminate,
1213
} = require("../native/index.node");
1314

1415
// import {
@@ -97,11 +98,11 @@ export class OrdinalsIndexer {
9798
return ordinalsIndexerOnBlockUndo.call(this.handle, callback);
9899
}
99100

100-
// /**
101-
// * @summary Terminates the containers
102-
// * @memberof DevnetNetworkOrchestrator
103-
// */
104-
// terminate(): boolean {
105-
// return stacksDevnetTerminate.call(this.handle);
106-
// }
101+
/**
102+
* @summary Terminates indexer
103+
* @memberof DevnetNetworkOrchestrator
104+
*/
105+
terminate() {
106+
return ordinalsIndexerTerminate.call(this.handle);
107+
}
107108
}

components/ordhook-sdk-js/lib/test.ts

+2
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,5 @@ indexer.rewriteBlocks([32103, 32104]);
2626
indexer.syncBlocks();
2727

2828
indexer.replayBlocks([32103, 32104]);
29+
30+
indexer.terminate();

components/ordhook-sdk-js/src/lib.rs

+20-5
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,14 @@ enum IndexerCommand {
4444
DropBlocks(Vec<u64>),
4545
RewriteBlocks(Vec<u64>),
4646
ReplayBlocks(Vec<u64>),
47-
Stop,
47+
Terminate,
4848
}
4949

50+
#[allow(dead_code)]
5051
enum CustomIndexerCommand {
5152
UpdateApplyCallback(Root<JsFunction>),
5253
UpdateUndoCallback(Root<JsFunction>),
54+
Terminate,
5355
}
5456

5557
impl Finalize for OrdinalsIndexer {}
@@ -136,6 +138,9 @@ impl OrdinalsIndexer {
136138
Ok(CustomIndexerCommand::UpdateUndoCallback(callback)) => {
137139
undo_callback = Some(callback);
138140
}
141+
Ok(CustomIndexerCommand::Terminate) => {
142+
return Ok(())
143+
}
139144
_ => {}
140145
}
141146
}
@@ -179,8 +184,8 @@ impl OrdinalsIndexer {
179184
IndexerCommand::SyncBlocks => {
180185
println!("Will sync blocks");
181186
}
182-
IndexerCommand::Stop => {
183-
break;
187+
IndexerCommand::Terminate => {
188+
std::process::exit(0);
184189
}
185190
}
186191
}
@@ -198,6 +203,11 @@ impl OrdinalsIndexer {
198203
Ok(true)
199204
}
200205

206+
fn terminate(&self) -> Result<bool, String> {
207+
let _ = self.command_tx.send(IndexerCommand::Terminate);
208+
Ok(true)
209+
}
210+
201211
fn replay_blocks(&self, blocks: Vec<u64>) -> Result<bool, String> {
202212
let _ = self.command_tx.send(IndexerCommand::ReplayBlocks(blocks));
203213
Ok(true)
@@ -372,8 +382,13 @@ impl OrdinalsIndexer {
372382
Ok(cx.undefined())
373383
}
374384

375-
fn js_terminate(mut _cx: FunctionContext) -> JsResult<JsBoolean> {
376-
unimplemented!();
385+
fn js_terminate(mut cx: FunctionContext) -> JsResult<JsUndefined> {
386+
cx.this()
387+
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
388+
.terminate()
389+
.or_else(|err| cx.throw_error(err.to_string()))?;
390+
391+
Ok(cx.undefined())
377392
}
378393

379394
fn js_on_block_apply(mut cx: FunctionContext) -> JsResult<JsUndefined> {

0 commit comments

Comments
 (0)