Bugfix: State Initialization

This ensures that the state for the plugin has been initialized prior to
calling any of the other jobs. This ensures that they won't error if
called quickly after Neovim starts up
This commit is contained in:
Harrison Cramer
2023-08-12 13:05:43 -04:00
parent 7e4ca89198
commit feff814b56

View File

@@ -1,4 +1,3 @@
local curl = require("plenary.curl")
local state = require("gitlab.state") local state = require("gitlab.state")
local discussions = require("gitlab.discussions") local discussions = require("gitlab.discussions")
local summary = require("gitlab.summary") local summary = require("gitlab.summary")
@@ -7,16 +6,30 @@ local comment = require("gitlab.comment")
local job = require("gitlab.job") local job = require("gitlab.job")
local u = require("gitlab.utils") local u = require("gitlab.utils")
-- Ensures the plugin's state is initialized prior to running other calls. This state contains the basic information about the current merge request, like description, author, etc
local ensureState = function(callback)
return function()
if type(state.INFO) ~= "table" then
job.run_job("info", "GET", nil, function(data)
state.INFO = data.info
callback()
end)
else
callback()
end
end
end
-- Root Module Scope -- Root Module Scope
local M = {} local M = {}
M.summary = summary.summary M.summary = ensureState(summary.summary)
M.approve = job.approve M.approve = ensureState(job.approve)
M.revoke = job.revoke M.revoke = ensureState(job.revoke)
M.create_comment = comment.create_comment M.create_comment = ensureState(comment.create_comment)
M.list_discussions = discussions.list_discussions M.list_discussions = ensureState(discussions.list_discussions)
M.edit_comment = comment.edit_comment M.edit_comment = ensureState(comment.edit_comment)
M.delete_comment = comment.delete_comment M.delete_comment = ensureState(comment.delete_comment)
M.reply = discussions.reply M.reply = ensureState(discussions.reply)
M.state = state M.state = state
-- Builds the binary (if not built); starts the Go server; calls the /info endpoint, -- Builds the binary (if not built); starts the Go server; calls the /info endpoint,
@@ -51,11 +64,8 @@ M.setup = function(args)
vim.notify("Could not start gitlab.nvim binary", vim.log.levels.ERROR) vim.notify("Could not start gitlab.nvim binary", vim.log.levels.ERROR)
return return
else else
job.run_job("info", "GET", nil, function(data) keymaps.set_keymap_keys(args.keymaps)
state.INFO = data.info keymaps.set_keymaps()
keymaps.set_keymap_keys(args.keymaps)
keymaps.set_keymaps()
end)
end end
end, end,
on_stderr = function(_, error) on_stderr = function(_, error)