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

perf: cache loaded plugins #710

Merged
merged 1 commit into from
Feb 21, 2024
Merged

perf: cache loaded plugins #710

merged 1 commit into from
Feb 21, 2024

Conversation

sxyazi
Copy link
Owner

@sxyazi sxyazi commented Feb 21, 2024

Resolves #703.

This PR removes the global variable state and passes it as a parameter, sync plugin:

return {
-  entry = function()
+  entry = function(state)
    state.foo = 1;
  end
}

Also sync block:

- local get_hovered_url = ya.sync(function()
+ local get_hovered_url = ya.sync(function(state)
  state.foo = 1;
end)

This allows the plugin to access the state during initialization:

-- ~/.config/yazi/plugins/test.yazi/init.lua
return {
  setup = function(state, opts)
    state.my_opt1 = opts.opt1
  end
}
-- ~/.config/yazi/init.lua
require("test"):setup {
  opt1 = "value1"
}

@dedukun
Copy link
Contributor

dedukun commented Feb 21, 2024

Hey, I tested a bit and the issue that was introduced in #702 is fixed, and I'm also able to get access to the state variable as you described , however something seems wrong with state inside the entry function, I can't access variables defined from setup in entry, however if I call a ya.sync method in entry I can access that variable.

The issue can be seen in the following test plugin, the print inside entry returns nil, but all other prints have the correct value for state.opt.

local sync = ya.sync(function(state)
	print("SYNC: " .. tostring(state.opt))
end)

local sync2 = ya.sync(function(state)
	ya.err("SYNC2: " .. tostring(state.opt))
end)

return {
	entry = function(state)
		ya.err("ENTRY: " .. tostring(state.opt))

		sync2()
	end,
	setup = function(state)
		state.opt = "test"

		sync()
	end,
}

EDIT: Nevermind, that's how it works 😄, so yes, it's working correctly

@sxyazi
Copy link
Owner Author

sxyazi commented Feb 21, 2024

Thanks for testing! Let's merge then

@sxyazi sxyazi merged commit d954784 into main Feb 21, 2024
5 checks passed
@sxyazi sxyazi deleted the pr-f60176c2 branch February 21, 2024 23:01
@sibouras sibouras mentioned this pull request Mar 10, 2024
59 tasks
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 7, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Acessing state in plugin's setup function
2 participants