Update k.lua and others

This commit is contained in:
Anavrins
2020-05-06 02:54:15 -04:00
parent 8db9a89f68
commit 59de8e7f63
4 changed files with 39 additions and 26 deletions

View File

@@ -119,4 +119,4 @@ return {
go = go,
stop = stop,
await = await
}
}

View File

@@ -101,6 +101,15 @@ function addresses(cb, limit, offset)
end, "/addresses?limit="..(limit or 50).."&offset="..(offset or 0))
end
function name(cb, name)
asserttype(cb, "callback", "function")
asserttype(name, "name", "string")
api_request(function(success, data)
cb(success and data and data.ok, data.name or data)
end, "/names/"..name)
end
function rich(cb, limit, offset)
asserttype(cb, "callback", "function")
asserttype(limit, "limit", "number", true)
@@ -165,7 +174,8 @@ local wsEventNameLookup = {
names = "name",
ownNames = "name",
ownWebhooks = "webhook",
motd = "motd"
motd = "motd",
keepalive = "keepalive",
}
local wsEvents = {}
@@ -362,6 +372,7 @@ return {
addressTransactions = addressTransactions,
addressNames = addressNames,
addresses = addresses,
name = name,
rich = rich,
transactions = transactions,
latestTransactions = latestTransactions,
@@ -369,5 +380,4 @@ return {
makeTransaction = makeTransaction,
connect = connect,
parseMeta = parseMeta,
sha256 = sha256,
}
}

View File

@@ -18,13 +18,16 @@ end
local function findID(url)
local found = gfind(url, idPatt)
if found then
return tonumber(found[#found]:sub(found[#found]:find("%d+")))
end
if not found then return nil end
return tonumber(found[#found]:sub(found[#found]:find("%d+")))
end
local function newID()
return #callbackRegistry + 1
for i = 1, math.huge do
if not callbackRegistry[i] then
return i
end
end
end
local function trimID(url)
@@ -36,8 +39,8 @@ end
function request(callback, url, headers, postData)
local id = newID()
local newUrl = url .. "#R" .. id
http.request(newUrl, postData, headers)
callbackRegistry[id] = callback
http.request(newUrl, postData, headers)
end
function init(jua)
@@ -46,7 +49,7 @@ function init(jua)
local id = findID(url)
if id and callbackRegistry[id] then
callbackRegistry[id](true, trimID(url), handle)
table.remove(callbackRegistry, id)
callbackRegistry[id] = nil
end
end)
@@ -54,7 +57,7 @@ function init(jua)
local id = findID(url)
if id and callbackRegistry[id] then
callbackRegistry[id](false, trimID(url), handle)
table.remove(callbackRegistry, id)
callbackRegistry[id] = nil
end
end)
end
@@ -62,4 +65,4 @@ end
return {
request = request,
init = init
}
}

View File

@@ -31,8 +31,8 @@ end
local function findID(url)
local found = gfind(url, idPatt)
local id = tonumber(found[#found]:sub(found[#found]:find("%d+")))
return id
if not found then return nil end
return tonumber(found[#found]:sub(found[#found]:find("%d+")))
end
local function newID()
@@ -71,57 +71,57 @@ function init(jua)
jua = jua
if async then
jua.on("websocket_success", function(event, url, handle)
local success, id = pcall(findID,url)
if success and id and callbackRegistry[id] and callbackRegistry[id].success then
local id = findID(url)
if id and callbackRegistry[id].success then
callbackRegistry[id].success(id, handle)
end
end)
jua.on("websocket_failure", function(event, url)
local success, id = pcall(findID,url)
if success and id and callbackRegistry[id] and callbackRegistry[id].failure then
local id = findID(url)
if id and callbackRegistry[id].failure then
callbackRegistry[id].failure(id)
end
table.remove(callbackRegistry, id)
end)
jua.on("websocket_message", function(event, url, data)
local success, id = pcall(findID,url)
if success and id and callbackRegistry[id] and callbackRegistry[id].message then
local id = findID(url)
if id and callbackRegistry[id].message then
callbackRegistry[id].message(id, data)
end
end)
jua.on("websocket_closed", function(event, url)
local success, id = pcall(findID,url)
if success and id and callbackRegistry[id] and callbackRegistry[id].closed then
local id = findID(url)
if id and callbackRegistry[id].closed then
callbackRegistry[id].closed(id)
end
table.remove(callbackRegistry, id)
end)
else
jua.on("socket_connect", function(event, id)
if id and callbackRegistry[id] and callbackRegistry[id].success then
if id and callbackRegistry[id].success then
callbackRegistry[id].success(id, wsRegistry[id])
end
end)
jua.on("socket_error", function(event, id, msg)
if id and callbackRegistry[id] and callbackRegistry[id].failure then
if id and callbackRegistry[id].failure then
callbackRegistry[id].failure(id, msg)
end
table.remove(callbackRegistry, id)
end)
jua.on("socket_message", function(event, id)
if id and callbackRegistry[id] and callbackRegistry[id].message then
if id and callbackRegistry[id].message then
local data = wsRegistry[id].read()
callbackRegistry[id].message(id, data)
end
end)
jua.on("socket_closed", function(event, id)
if id and callbackRegistry[id] and callbackRegistry[id].closed then
if id and callbackRegistry[id].closed then
callbackRegistry[id].closed(id)
end
table.remove(callbackRegistry, id)