yazi/yazi-plugin/src/isolate/preload.rs
2025-03-17 16:12:43 +08:00

31 lines
891 B
Rust

use mlua::{ExternalResult, IntoLua, ObjectLike};
use tokio::runtime::Handle;
use yazi_config::LAYOUT;
use yazi_dds::Sendable;
use yazi_shared::event::Cmd;
use super::slim_lua;
use crate::{Error, elements::Rect, file::File, loader::LOADER};
pub async fn preload(
cmd: &'static Cmd,
file: yazi_fs::File,
) -> mlua::Result<(bool, Option<Error>)> {
LOADER.ensure(&cmd.name, |_| ()).await.into_lua_err()?;
tokio::task::spawn_blocking(move || {
let lua = slim_lua(&cmd.name)?;
let plugin = LOADER.load_once(&lua, &cmd.name)?;
let job = lua.create_table_from([
("area", Rect::from(LAYOUT.get().preview).into_lua(&lua)?),
("args", Sendable::args_to_table_ref(&lua, &cmd.args)?.into_lua(&lua)?),
("file", File(file).into_lua(&lua)?),
("skip", 0.into_lua(&lua)?),
])?;
Handle::current().block_on(plugin.call_async_method("preload", job))
})
.await
.into_lua_err()?
}