Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: attach plugin args to the entry method for better future optimization possibilities #627

Merged
merged 1 commit into from
Feb 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions yazi-fm/src/app/commands/plugin.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 13 additions & 4 deletions yazi-plugin/preset/state.lua
Original file line number Diff line number Diff line change
@@ -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,
})
7 changes: 3 additions & 4 deletions yazi-plugin/src/isolate/entry.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()?
Expand Down
Loading