Files
gitlab.nvim/lua/gitlab/async.lua
Harrison (Harry) Cramer 80b597e56a Feat: Remove Requirement for Dotfile (#84)
This MR removes the requirement for a dotfile (the dotfile is now optional and will override the configuration provided via environment variables). The requirement for providing a project ID is also eliminated, by parsing the namespace and project name from the SSH or HTTPS remote, and then using that to query Gitlab for a matching project.
2023-11-11 23:51:11 -05:00

70 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 = {}
local 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, argTable)
if i > #dependencies then
self.cb(argTable)
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, argTable)
return
end
job.run_job(dependency.endpoint, "GET", dependency.body, function(data)
state[dependency.state] = data[dependency.key]
self:fetch(dependencies, i + 1, argTable)
end)
end
-- Will call APIs in sequence and set global state
M.sequence = function(dependencies, cb)
return function(argTable)
local handler = async:new()
handler:init(cb)
-- Sets configuration for plugin, if not already set
if not state.initialized then
if not state.setPluginConfiguration() then
return
end
end
if state.go_server_running then
handler:fetch(dependencies, 1, argTable)
return
end
server.start(function()
state.go_server_running = true
handler:fetch(dependencies, 1, argTable)
end)
end
end
return M