-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrequest.lua
More file actions
53 lines (45 loc) · 1.06 KB
/
Copy pathrequest.lua
File metadata and controls
53 lines (45 loc) · 1.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
local Job = require("plenary.job")
local uv = vim.uv
local M = {}
--store current jobs
M.current_jobs = {}
function M.add_request(job)
table.insert(M.current_jobs, job)
end
function M.end_request(job)
if not uv.kill(job.pid) then
return false
end
end
function M.end_all()
for _, job in ipairs(M.current_jobs) do
M.end_request(job)
end
M.current_jobs = {}
end
function M.remove_request(job)
for i, ojob in ipairs(M.current_jobs) do
if job.pid == ojob.pid then
table.remove(M.current_jobs, i)
end
end
end
function M.send(request_args, callback)
local result = {}
local job = Job:new({
command = "curl",
args = request_args,
on_stdout = function(_, line)
table.insert(result, line)
end,
on_exit = function()
if callback then
-- Join all the output lines and send to callback
callback(table.concat(result, "\n"))
end
end,
})
job:start()
M.add_request(job)
end
return M