mirror of
https://github.com/sxyazi/yazi.git
synced 2026-07-25 08:41:05 +00:00
fix: attach plugin args to the entry method for better future optimization possibilities (#627)
This commit is contained in:
parent
a027c1be12
commit
715fbf5b65
3 changed files with 23 additions and 12 deletions
|
|
@ -1,6 +1,6 @@
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
use mlua::{ExternalError, ExternalResult, IntoLua, Table, TableExt, Variadic};
|
use mlua::{ExternalError, ExternalResult, Table, TableExt};
|
||||||
use tracing::warn;
|
use tracing::warn;
|
||||||
use yazi_plugin::{LOADED, LUA};
|
use yazi_plugin::{LOADED, LUA};
|
||||||
use yazi_shared::{emit, event::Cmd, Layer};
|
use yazi_shared::{emit, event::Cmd, Layer};
|
||||||
|
|
@ -35,20 +35,23 @@ impl App {
|
||||||
Err(e) => return warn!("{e}"),
|
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, |_| {
|
let result = Lives::scope(&self.cx, |_| {
|
||||||
LUA.globals().set("YAZI_PLUGIN_NAME", LUA.create_string(&opt.name)?)?;
|
LUA.globals().set("YAZI_PLUGIN_NAME", LUA.create_string(&opt.name)?)?;
|
||||||
|
|
||||||
let mut plugin: Option<Table> = None;
|
let mut plugin: Option<Table> = None;
|
||||||
if let Some(b) = LOADED.read().get(&opt.name) {
|
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 {
|
let Some(plugin) = plugin else {
|
||||||
return Err("plugin not found".into_lua_err());
|
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 {
|
let Some(tx) = opt.data.tx else {
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,25 @@
|
||||||
local cache = {}
|
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({
|
state = setmetatable({}, {
|
||||||
clear = function() cache[YAZI_PLUGIN_NAME] = nil end,
|
|
||||||
}, {
|
|
||||||
__index = function(_, k)
|
__index = function(_, k)
|
||||||
local bucket = YAZI_PLUGIN_NAME
|
local bucket = YAZI_PLUGIN_NAME
|
||||||
return cache[bucket] and cache[bucket][k]
|
return cache[bucket] and cache[bucket][k]
|
||||||
end,
|
end,
|
||||||
|
|
||||||
__newindex = function(_, k, v)
|
__newindex = function(_, k, v)
|
||||||
local bucket = YAZI_PLUGIN_NAME
|
local bucket = YAZI_PLUGIN_NAME
|
||||||
cache[bucket] = cache[bucket] or {}
|
cache[bucket] = cache[bucket] or {}
|
||||||
cache[bucket][k] = v
|
cache[bucket][k] = v
|
||||||
end,
|
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 tokio::runtime::Handle;
|
||||||
|
|
||||||
use super::slim_lua;
|
use super::slim_lua;
|
||||||
|
|
@ -11,14 +11,13 @@ pub async fn entry(name: String, args: Vec<ValueSendable>) -> mlua::Result<()> {
|
||||||
let lua = slim_lua()?;
|
let lua = slim_lua()?;
|
||||||
lua.globals().set("YAZI_PLUGIN_NAME", lua.create_string(&name)?)?;
|
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) {
|
let plugin: Table = if let Some(b) = LOADED.read().get(&name) {
|
||||||
lua.load(b).call(args)?
|
lua.load(b).call(())?
|
||||||
} else {
|
} else {
|
||||||
return Err("unloaded plugin".into_lua_err());
|
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
|
.await
|
||||||
.into_lua_err()?
|
.into_lua_err()?
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue