mirror of
https://github.com/sxyazi/yazi.git
synced 2024-12-25 09:46:37 +03:00
fix: attach plugin args
to the entry
method for better future optimization possibilities (#627)
This commit is contained in:
parent
a027c1be12
commit
715fbf5b65
@ -1,6 +1,6 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use mlua::{ExternalError, ExternalResult, IntoLua, Table, TableExt, Variadic};
|
||||
use mlua::{ExternalError, ExternalResult, Table, TableExt};
|
||||
use tracing::warn;
|
||||
use yazi_plugin::{LOADED, LUA};
|
||||
use yazi_shared::{emit, event::Cmd, Layer};
|
||||
@ -35,20 +35,23 @@ impl App {
|
||||
Err(e) => return warn!("{e}"),
|
||||
};
|
||||
|
||||
let args = Variadic::from_iter(opt.data.args.into_iter().filter_map(|v| v.into_lua(&LUA).ok()));
|
||||
let result = Lives::scope(&self.cx, |_| {
|
||||
LUA.globals().set("YAZI_PLUGIN_NAME", LUA.create_string(&opt.name)?)?;
|
||||
|
||||
let mut plugin: Option<Table> = None;
|
||||
if let Some(b) = LOADED.read().get(&opt.name) {
|
||||
plugin = LUA.load(b).call(args)?;
|
||||
plugin = LUA.load(b).call(())?;
|
||||
}
|
||||
|
||||
let Some(plugin) = plugin else {
|
||||
return Err("plugin not found".into_lua_err());
|
||||
};
|
||||
|
||||
if let Some(cb) = opt.data.cb { cb(plugin) } else { plugin.call_method("entry", ()) }
|
||||
if let Some(cb) = opt.data.cb {
|
||||
cb(plugin)
|
||||
} else {
|
||||
plugin.call_method("entry", opt.data.args)
|
||||
}
|
||||
});
|
||||
|
||||
let Some(tx) = opt.data.tx else {
|
||||
|
@ -1,16 +1,25 @@
|
||||
local cache = {}
|
||||
local sub_mt = {
|
||||
__index = function(target, k)
|
||||
local bucket = rawget(target, "__yazi_bucket")
|
||||
return cache[bucket] and cache[bucket][k]
|
||||
end,
|
||||
__newindex = function(target, k, v)
|
||||
local bucket = rawget(target, "__yazi_bucket")
|
||||
cache[bucket] = cache[bucket] or {}
|
||||
cache[bucket][k] = v
|
||||
end,
|
||||
}
|
||||
|
||||
state = setmetatable({
|
||||
clear = function() cache[YAZI_PLUGIN_NAME] = nil end,
|
||||
}, {
|
||||
state = setmetatable({}, {
|
||||
__index = function(_, k)
|
||||
local bucket = YAZI_PLUGIN_NAME
|
||||
return cache[bucket] and cache[bucket][k]
|
||||
end,
|
||||
|
||||
__newindex = function(_, k, v)
|
||||
local bucket = YAZI_PLUGIN_NAME
|
||||
cache[bucket] = cache[bucket] or {}
|
||||
cache[bucket][k] = v
|
||||
end,
|
||||
__call = function() return setmetatable({ __yazi_bucket = YAZI_PLUGIN_NAME }, sub_mt) end,
|
||||
})
|
||||
|
@ -1,4 +1,4 @@
|
||||
use mlua::{ExternalError, ExternalResult, IntoLua, Table, TableExt, Variadic};
|
||||
use mlua::{ExternalError, ExternalResult, Table, TableExt};
|
||||
use tokio::runtime::Handle;
|
||||
|
||||
use super::slim_lua;
|
||||
@ -11,14 +11,13 @@ pub async fn entry(name: String, args: Vec<ValueSendable>) -> mlua::Result<()> {
|
||||
let lua = slim_lua()?;
|
||||
lua.globals().set("YAZI_PLUGIN_NAME", lua.create_string(&name)?)?;
|
||||
|
||||
let args = Variadic::from_iter(args.into_iter().filter_map(|v| v.into_lua(&lua).ok()));
|
||||
let plugin: Table = if let Some(b) = LOADED.read().get(&name) {
|
||||
lua.load(b).call(args)?
|
||||
lua.load(b).call(())?
|
||||
} else {
|
||||
return Err("unloaded plugin".into_lua_err());
|
||||
};
|
||||
|
||||
Handle::current().block_on(plugin.call_async_method("entry", ()))
|
||||
Handle::current().block_on(plugin.call_async_method("entry", args))
|
||||
})
|
||||
.await
|
||||
.into_lua_err()?
|
||||
|
Loading…
Reference in New Issue
Block a user