Skip to content

Commit

Permalink
fix: attach plugin args to the entry method for better future opt…
Browse files Browse the repository at this point in the history
…imization possibilities (#627)
  • Loading branch information
sxyazi authored Feb 4, 2024
1 parent a027c1b commit 715fbf5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
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

0 comments on commit 715fbf5

Please sign in to comment.