mirror of
https://github.com/jiriks74/presence.nvim
synced 2024-12-29 19:32:05 +01:00
parent
a695e5d653
commit
f2a0dd50c6
1 changed files with 23 additions and 6 deletions
|
@ -54,7 +54,12 @@ end
|
||||||
-- Make a remote procedure call to Discord
|
-- Make a remote procedure call to Discord
|
||||||
-- Callback argument in format: on_response(error[, response_table])
|
-- Callback argument in format: on_response(error[, response_table])
|
||||||
function Discord:call(opcode, payload, on_response)
|
function Discord:call(opcode, payload, on_response)
|
||||||
self.encode_json(payload, function(body)
|
self.encode_json(payload, function(success, body)
|
||||||
|
if not success then
|
||||||
|
self.log:warn(string.format("Failed to encode payload: %s", vim.inspect(body)))
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
-- Start reading for the response
|
-- Start reading for the response
|
||||||
self.pipe:read_start(function(...)
|
self.pipe:read_start(function(...)
|
||||||
self:read_message(payload.nonce, on_response, ...)
|
self:read_message(payload.nonce, on_response, ...)
|
||||||
|
@ -86,11 +91,11 @@ function Discord:read_message(nonce, on_response, err, chunk)
|
||||||
on_response(err_message)
|
on_response(err_message)
|
||||||
|
|
||||||
elseif chunk then
|
elseif chunk then
|
||||||
local header_size = 9
|
-- Strip header from the chunk
|
||||||
local message = chunk:sub(header_size)
|
local message = chunk:match("({.+)")
|
||||||
local response_opcode = struct.unpack("<ii", chunk)
|
local response_opcode = struct.unpack("<ii", chunk)
|
||||||
|
|
||||||
self.decode_json(message, function(response)
|
self.decode_json(message, function(success, response)
|
||||||
-- Check for a non-frame opcode in the response
|
-- Check for a non-frame opcode in the response
|
||||||
if response_opcode ~= self.opcodes.frame then
|
if response_opcode ~= self.opcodes.frame then
|
||||||
local err_format = "Received unexpected opcode - %s (code %s)"
|
local err_format = "Received unexpected opcode - %s (code %s)"
|
||||||
|
@ -99,6 +104,14 @@ function Discord:read_message(nonce, on_response, err, chunk)
|
||||||
return on_response(err_message)
|
return on_response(err_message)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- Unable to decode the response
|
||||||
|
if not success then
|
||||||
|
-- Indetermine state at this point, no choice but to simply warn on the parse failure
|
||||||
|
-- but invoke empty response callback as request may still have succeeded
|
||||||
|
self.log:warn(string.format("Failed to decode payload: %s", vim.inspect(message)))
|
||||||
|
return on_response()
|
||||||
|
end
|
||||||
|
|
||||||
-- Check for an error event response
|
-- Check for an error event response
|
||||||
if response.evt == self.events.ERROR then
|
if response.evt == self.events.ERROR then
|
||||||
local data = response.data
|
local data = response.data
|
||||||
|
@ -170,13 +183,17 @@ end
|
||||||
|
|
||||||
function Discord.decode_json(t, on_done)
|
function Discord.decode_json(t, on_done)
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
on_done(vim.fn.json_decode(t))
|
on_done(pcall(function()
|
||||||
|
return vim.fn.json_decode(t)
|
||||||
|
end))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
function Discord.encode_json(t, on_done)
|
function Discord.encode_json(t, on_done)
|
||||||
vim.schedule(function()
|
vim.schedule(function()
|
||||||
on_done(vim.fn.json_encode(t))
|
on_done(pcall(function()
|
||||||
|
return vim.fn.json_encode(t)
|
||||||
|
end))
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue