Skip to content

Commit

Permalink
feat: state manager
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts committed Mar 15, 2024
1 parent 087ee79 commit 28971f6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 42 deletions.
27 changes: 11 additions & 16 deletions lua/your-plugin-name/init.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
local M = require("your-plugin-name.main")
local C = require("your-plugin-name.config")

local YourPluginName = {}

-- Toggle the plugin by calling the `enable`/`disable` methods respectively.
--- Toggle the plugin by calling the `enable`/`disable` methods respectively.
function YourPluginName.toggle()
-- when the config is not set to the global object, we set it
if _G.YourPluginName.config == nil then
_G.YourPluginName.config = require("your-plugin-name.config").options
_G.YourPluginName.config = C.options
end

_G.YourPluginName.state = M.toggle()
M.toggle("publicAPI_toggle")
end

-- starts YourPluginName and set internal functions and state.
--- Initializes the plugin, sets event listeners and internal state.
function YourPluginName.enable()
if _G.YourPluginName.config == nil then
_G.YourPluginName.config = require("your-plugin-name.config").options
end

local state = M.enable()

if state ~= nil then
_G.YourPluginName.state = state
_G.YourPluginName.config = C.options
end

return state
M.enable("publicAPI_enable")
end

-- disables YourPluginName and reset internal functions and state.
--- Disables the plugin, clear highlight groups and autocmds, closes side buffers and resets the internal state.
function YourPluginName.disable()
_G.YourPluginName.state = M.disable()
M.disable("publicAPI_disable")
end

-- setup YourPluginName options and merge them with user provided ones.
function YourPluginName.setup(opts)
_G.YourPluginName.config = require("your-plugin-name.config").setup(opts)
_G.YourPluginName.config = C.setup(opts)
end

_G.YourPluginName = YourPluginName
Expand Down
58 changes: 32 additions & 26 deletions lua/your-plugin-name/main.lua
Original file line number Diff line number Diff line change
@@ -1,49 +1,55 @@
local S = require("your-plugin-name.state")
local D = require("your-plugin-name.util.debug")

-- internal methods
local YourPluginName = {}

-- state
local S = {
-- Boolean determining if the plugin is enabled or not.
enabled = false,
}

---Toggle the plugin by calling the `enable`/`disable` methods respectively.
-- Toggle the plugin by calling the `enable`/`disable` methods respectively.
--
--- @param scope string: internal identifier for logging purposes.
---@private
function YourPluginName.toggle()
if S.enabled then
return YourPluginName.disable()
function YourPluginName.toggle(scope)
if S.getEnabled(S) then
return YourPluginName.disable(scope)
end

return YourPluginName.enable()
return YourPluginName.enable(scope)
end

---Initializes the plugin.
--- Initializes the plugin, sets event listeners and internal state.
---
--- @param scope string: internal identifier for logging purposes.
---@private
function YourPluginName.enable()
if S.enabled then
return S
function YourPluginName.enable(scope)
if S.getEnabled(S) then
D.log(scope, "Plugin is already enabled.")

return
end

S.enabled = true
-- sets the plugin as `enabled`
S.setEnabled(S)

return S
-- saves the state globally to `_G.YourPluginName.state`
S.save(S)
end

---Disables the plugin and reset the internal state.
--- Disables the plugin for the given tab, clear highlight groups and autocmds, closes side buffers and resets the internal state.
---
--- @param scope string: internal identifier for logging purposes.
---@private
function YourPluginName.disable()
if not S.enabled then
return S
function YourPluginName.disable(scope)
if not S.getEnabled(S) then
D.log(scope, "Plugin is already disabled.")

return
end

-- reset the state
S = {
enabled = false,
}
-- resets the state to its initial value
S.init(S)

return S
-- saves the state globally to `_G.YourPluginName.state`
S.save(S)
end

return YourPluginName
36 changes: 36 additions & 0 deletions lua/your-plugin-name/state.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
local D = require("your-plugin-name.util.debug")

local State = { enabled = false }

---Sets the state to its original value.
---
---@private
function State:init()
self.enabled = false
end

---Saves the state in the global _G.YourPluginName.state object.
---
---@private
function State:save()
D.log("state.save", "saving state globally to _G.YourPluginName.state")

_G.YourPluginName.state = self
end

---Whether the YourPluginName is enabled or not.
---
---@private
function State:setEnabled()
self.enabled = true
end

---Whether the YourPluginName is enabled or not.
---
---@return boolean: the `enabled` state value.
---@private
function State:getEnabled()
return self.enabled
end

return State

0 comments on commit 28971f6

Please sign in to comment.