BREAKING CHANGE: This MR addresses an underlying issue with the original implementation in regards to detecting line numbers for comments. As such, this is a major breaking change. The setup function signature has changed, please review the `README.md` for the new arguments. The delta pager has also been added as a dependency: https://github.com/dandavison/delta There will be future work to implement a native solution for parsing changes and line numbers.
68 lines
1.5 KiB
Lua
68 lines
1.5 KiB
Lua
-- This module is responsible for calling APIs in sequence. It provides
|
|
-- an abstraction around the APIs that lets us ensure state.
|
|
local server = require("gitlab.server")
|
|
local job = require("gitlab.job")
|
|
local state = require("gitlab.state")
|
|
|
|
local M = {}
|
|
|
|
Async = {
|
|
cb = nil
|
|
}
|
|
|
|
function Async:new(o)
|
|
o = o or {}
|
|
setmetatable(o, self)
|
|
self.__index = self
|
|
return o
|
|
end
|
|
|
|
function Async:init(cb)
|
|
self.cb = cb
|
|
end
|
|
|
|
function Async:fetch(dependencies, i)
|
|
if i > #dependencies then
|
|
self:cb()
|
|
return
|
|
end
|
|
|
|
local dependency = dependencies[i]
|
|
|
|
-- Do not call endpoint unless refresh is required
|
|
if state[dependency.state] ~= nil and not dependency.refresh then
|
|
self:fetch(dependencies, i + 1)
|
|
return
|
|
end
|
|
|
|
job.run_job(dependency.endpoint, "GET", dependency.body, function(data)
|
|
state[dependency.state] = data[dependency.key]
|
|
self:fetch(dependencies, i + 1)
|
|
end)
|
|
end
|
|
|
|
-- Will call APIs in sequence and set global state
|
|
M.sequence = function(dependencies, cb)
|
|
return function()
|
|
local handler = Async:new()
|
|
handler:init(cb)
|
|
|
|
if not state.is_gitlab_project then
|
|
vim.notify("The gitlab.nvim state was not set. Do you have a .gitlab.nvim file configured?", vim.log.levels.ERROR)
|
|
return
|
|
end
|
|
|
|
if state.go_server_running then
|
|
handler:fetch(dependencies, 1)
|
|
return
|
|
end
|
|
|
|
server.start_server(function()
|
|
state.go_server_running = true
|
|
handler:fetch(dependencies, 1)
|
|
end)
|
|
end
|
|
end
|
|
|
|
return M
|