|
1 | 1 | use std::marker::PhantomData; |
2 | 2 |
|
3 | 3 | use anyhow::Result; |
4 | | -use mlua::{IntoLua, Value}; |
5 | | -use tracing::error; |
| 4 | +use mlua::{ErrorContext, ExternalError, IntoLua, Value}; |
6 | 5 | use yazi_binding::runtime_mut; |
7 | | -use yazi_dds::{LOCAL, body::Body}; |
8 | | -use yazi_macro::succ; |
| 6 | +use yazi_dds::{LOCAL, spark::{Spark, SparkKind}}; |
9 | 7 | use yazi_plugin::LUA; |
10 | | -use yazi_shared::event::Data; |
11 | 8 |
|
12 | | -use crate::{Actor, Ctx, lives::Lives}; |
| 9 | +use crate::{Ctx, lives::Lives}; |
13 | 10 |
|
14 | 11 | pub struct Preflight<'a> { |
15 | 12 | _lifetime: PhantomData<&'a ()>, |
16 | 13 | } |
17 | 14 |
|
18 | | -impl<'a> Actor for Preflight<'a> { |
19 | | - type Options = (&'static str, Body<'a>); |
20 | | - |
21 | | - const NAME: &'static str = "preflight"; |
22 | | - |
23 | | - fn act(cx: &mut Ctx, opt: Self::Options) -> Result<Data> { |
| 15 | +impl<'a> Preflight<'a> { |
| 16 | + pub fn act(cx: &mut Ctx, opt: (SparkKind, Spark<'a>)) -> Result<Spark<'a>> { |
24 | 17 | let kind = opt.0; |
25 | | - let Some(handlers) = LOCAL.read().get(kind).filter(|&m| !m.is_empty()).cloned() else { |
26 | | - succ!(false) |
| 18 | + let Some(handlers) = LOCAL.read().get(kind.as_ref()).filter(|&m| !m.is_empty()).cloned() else { |
| 19 | + return Ok(opt.1); |
27 | 20 | }; |
28 | 21 |
|
29 | | - succ!(Lives::scope(cx.core, || { |
30 | | - let body = opt.1.into_lua(&LUA)?; |
| 22 | + Ok(Lives::scope(cx.core, || { |
| 23 | + let mut body = opt.1.into_lua(&LUA)?; |
31 | 24 | for (id, cb) in handlers { |
32 | 25 | runtime_mut!(LUA)?.push(&id); |
33 | | - let result = cb.call::<Value>(body.clone()); |
| 26 | + let result = cb.call::<Value>(&body); |
34 | 27 | runtime_mut!(LUA)?.pop(); |
35 | 28 |
|
36 | 29 | match result { |
37 | | - Ok(Value::Boolean(true)) => return Ok(true), |
38 | | - Ok(Value::Nil | Value::Boolean(false)) => {} |
39 | | - Ok(v) => { |
40 | | - error!("Unexpected return type from `{kind}` event handler in `{id}` plugin: {v:?}") |
41 | | - } |
42 | | - Err(e) => error!("Failed to run `{kind}` event handler in `{id}` plugin: {e}"), |
43 | | - } |
| 30 | + Ok(Value::Nil) => Err( |
| 31 | + format!("Cancelled by `{kind}` event handler in `{id}` plugin on preflight") |
| 32 | + .into_lua_err(), |
| 33 | + )?, |
| 34 | + Ok(v) => body = v, |
| 35 | + Err(e) => Err( |
| 36 | + format!("Failed to run `{kind}` event handler in `{id}` plugin: {e}").into_lua_err(), |
| 37 | + )?, |
| 38 | + }; |
44 | 39 | } |
45 | | - Ok(false) |
| 40 | + |
| 41 | + Spark::from_lua(&LUA, kind, body) |
| 42 | + .with_context(|e| format!("Unexpected return type from `{kind}` event handlers: {e}")) |
46 | 43 | })?) |
47 | 44 | } |
48 | 45 | } |
0 commit comments