From 59de8e7f63d544351e13cd62c7f8d89967976754 Mon Sep 17 00:00:00 2001 From: Anavrins Date: Wed, 6 May 2020 02:54:15 -0400 Subject: [PATCH] Update k.lua and others --- swshop/apis/jua.lua | 2 +- swshop/apis/k.lua | 16 +++++++++++++--- swshop/apis/r.lua | 19 +++++++++++-------- swshop/apis/w.lua | 28 ++++++++++++++-------------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/swshop/apis/jua.lua b/swshop/apis/jua.lua index 273b9ec..4ee5b73 100644 --- a/swshop/apis/jua.lua +++ b/swshop/apis/jua.lua @@ -119,4 +119,4 @@ return { go = go, stop = stop, await = await -} \ No newline at end of file +} diff --git a/swshop/apis/k.lua b/swshop/apis/k.lua index 47ad499..4adf49e 100644 --- a/swshop/apis/k.lua +++ b/swshop/apis/k.lua @@ -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, -} \ No newline at end of file +} diff --git a/swshop/apis/r.lua b/swshop/apis/r.lua index 3982634..f967776 100644 --- a/swshop/apis/r.lua +++ b/swshop/apis/r.lua @@ -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 -} \ No newline at end of file +} diff --git a/swshop/apis/w.lua b/swshop/apis/w.lua index 85fadf7..7fe1e3e 100644 --- a/swshop/apis/w.lua +++ b/swshop/apis/w.lua @@ -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)