| 29 | } |
| 30 | |
| 31 | fn execute(&self, args: &str, ctx: &RustCommandContext) -> RustCommandOutput { |
| 32 | let handler = self.handler.clone(); |
| 33 | let args = args.to_string(); |
| 34 | let ctx = ctx.clone(); |
| 35 | let (tx, rx) = std::sync::mpsc::sync_channel::<Result<String, napi::Status>>(1); |
| 36 | |
| 37 | handler.call_with_return_value( |
| 38 | (args, ctx), |
| 39 | ThreadsafeFunctionCallMode::Blocking, |
| 40 | move |ret: napi::JsUnknown| { |
| 41 | let value = ret.coerce_to_string()?.into_utf8()?.into_owned()?; |
| 42 | let _ = tx.send(Ok(value)); |
| 43 | Ok(()) |
| 44 | }, |
| 45 | ); |
| 46 | |
| 47 | match rx.recv() { |
| 48 | Ok(Ok(value)) => RustCommandOutput::text(value), |
| 49 | Ok(Err(status)) => { |
| 50 | RustCommandOutput::text(format!("Command '{}' failed: {:?}", self.name, status)) |
| 51 | } |
| 52 | Err(_) => RustCommandOutput::text(format!( |
| 53 | "Command '{}' failed: handler did not return a value", |
| 54 | self.name |
| 55 | )), |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /// Convert Rust CommandContext to JavaScript object. |