mirror of
https://github.com/Refactorio/RedMew.git
synced 2024-12-12 10:04:40 +02:00
Merge pull request #857 from grilledham/server/get_data_timeout
Added function to cancel a Server.try_get_data requests
This commit is contained in:
commit
048fa5aef6
@ -1,6 +1,7 @@
|
||||
--- See documentation at https://github.com/Refactorio/RedMew/pull/469
|
||||
|
||||
local Token = require 'utils.token'
|
||||
local Task = require 'utils.task'
|
||||
local Global = require 'utils.global'
|
||||
local Event = require 'utils.event'
|
||||
local Game = require 'utils.game'
|
||||
@ -13,6 +14,7 @@ local concat = table.concat
|
||||
local remove = table.remove
|
||||
local tostring = tostring
|
||||
local raw_print = Print.raw_print
|
||||
local next = next
|
||||
|
||||
local serialize_options = {sparse = true, compact = true}
|
||||
|
||||
@ -20,12 +22,17 @@ local Public = {}
|
||||
|
||||
local server_time = {secs = nil, tick = 0}
|
||||
ErrorLogging.server_time = server_time
|
||||
local requests = {}
|
||||
|
||||
Global.register(
|
||||
server_time,
|
||||
{
|
||||
server_time = server_time,
|
||||
requests = requests
|
||||
},
|
||||
function(tbl)
|
||||
server_time = tbl
|
||||
ErrorLogging.server_time = tbl
|
||||
server_time = tbl.server_time
|
||||
ErrorLogging.server_time = server_time
|
||||
requests = tbl.requests
|
||||
end
|
||||
)
|
||||
|
||||
@ -217,6 +224,50 @@ function Public.set_data(data_set, key, value)
|
||||
raw_print(message)
|
||||
end
|
||||
|
||||
local function validate_arguments(data_set, key, callback_token)
|
||||
if type(data_set) ~= 'string' then
|
||||
error('data_set must be a string', 3)
|
||||
end
|
||||
if type(key) ~= 'string' then
|
||||
error('key must be a string', 3)
|
||||
end
|
||||
if type(callback_token) ~= 'number' then
|
||||
error('callback_token must be a number', 3)
|
||||
end
|
||||
end
|
||||
|
||||
local function send_try_get_data(data_set, key, callback_token)
|
||||
data_set = double_escape(data_set)
|
||||
key = double_escape(key)
|
||||
|
||||
local message = concat {data_get_tag, callback_token, ' {', 'data_set:"', data_set, '",key:"', key, '"}'}
|
||||
raw_print(message)
|
||||
end
|
||||
|
||||
local cancelable_callback_token =
|
||||
Token.register(
|
||||
function(data)
|
||||
local data_set = data.data_set
|
||||
local keys = requests[data_set]
|
||||
if not keys then
|
||||
return
|
||||
end
|
||||
|
||||
local key = data.key
|
||||
local callbacks = keys[key]
|
||||
if not callbacks then
|
||||
return
|
||||
end
|
||||
|
||||
keys[key] = nil
|
||||
|
||||
for c, _ in next, callbacks do
|
||||
local func = Token.get(c)
|
||||
func(data)
|
||||
end
|
||||
end
|
||||
)
|
||||
|
||||
--- Gets data from the web server's persistent data storage.
|
||||
-- The callback is passed a table {data_set: string, key: string, value: any}.
|
||||
-- If the value is nil, it means there is no stored data for that data_set key pair.
|
||||
@ -240,21 +291,131 @@ end
|
||||
--
|
||||
-- Server.try_get_data('my data set', 'key 1', callback)
|
||||
function Public.try_get_data(data_set, key, callback_token)
|
||||
if type(data_set) ~= 'string' then
|
||||
error('data_set must be a string', 2)
|
||||
end
|
||||
if type(key) ~= 'string' then
|
||||
error('key must be a string', 2)
|
||||
end
|
||||
if type(callback_token) ~= 'number' then
|
||||
error('callback_token must be a number', 2)
|
||||
validate_arguments(data_set, key, callback_token)
|
||||
|
||||
send_try_get_data(data_set, key, callback_token)
|
||||
end
|
||||
|
||||
local function try_get_data_cancelable(data_set, key, callback_token)
|
||||
local keys = requests[data_set]
|
||||
if not keys then
|
||||
keys = {}
|
||||
requests[data_set] = keys
|
||||
end
|
||||
|
||||
data_set = double_escape(data_set)
|
||||
key = double_escape(key)
|
||||
local callbacks = keys[key]
|
||||
if not callbacks then
|
||||
callbacks = {}
|
||||
keys[key] = callbacks
|
||||
end
|
||||
|
||||
local message = concat {data_get_tag, callback_token, ' {', 'data_set:"', data_set, '",key:"', key, '"}'}
|
||||
raw_print(message)
|
||||
if callbacks[callback_token] then
|
||||
return
|
||||
end
|
||||
|
||||
if next(callbacks) then
|
||||
callbacks[callback_token] = true
|
||||
else
|
||||
callbacks[callback_token] = true
|
||||
send_try_get_data(data_set, key, cancelable_callback_token)
|
||||
end
|
||||
end
|
||||
|
||||
--- Same Server.try_get_data but the request can be cancelled by calling
|
||||
-- Server.cancel_try_get_data(data_set, key, callback_token)
|
||||
-- If the request is cancelled before it is complete the callback will be called with nil.
|
||||
-- It is safe to cancel a non-existent or completed request, in either case the callback will not be called.
|
||||
-- There can only be one request per data_set, key, callback_token combo. If there is already an ongoing request
|
||||
-- an attempt to make a new one will be ignored.
|
||||
-- @param data_set<string>
|
||||
-- @param key<string>
|
||||
-- @param callback_token<token>
|
||||
function Public.try_get_data_cancelable(data_set, key, callback_token)
|
||||
validate_arguments(data_set, key, callback_token)
|
||||
|
||||
try_get_data_cancelable(data_set, key, callback_token)
|
||||
end
|
||||
|
||||
local function cancel_try_get_data(data_set, key, callback_token)
|
||||
local keys = requests[data_set]
|
||||
if not keys then
|
||||
return false
|
||||
end
|
||||
|
||||
local callbacks = keys[key]
|
||||
if not callbacks then
|
||||
return false
|
||||
end
|
||||
|
||||
if callbacks[callback_token] then
|
||||
callbacks[callback_token] = nil
|
||||
|
||||
local func = Token.get(callback_token)
|
||||
func()
|
||||
|
||||
return true
|
||||
else
|
||||
return false
|
||||
end
|
||||
end
|
||||
|
||||
--- Cancels the request. Returns false if the request could not be cnacled, either because there is no request
|
||||
-- to cancel or it has been completed or cancled already. Otherwise returns true.
|
||||
-- If the request is cancelled before it is complete the callback will be called with nil.
|
||||
-- It is safe to cancel a non-existent or completed request, in either case the callback will not be called.
|
||||
-- @param data_set<string>
|
||||
-- @param key<string>
|
||||
-- @param callback_token<token>
|
||||
function Public.cancel_try_get_data(data_set, key, callback_token)
|
||||
validate_arguments(data_set, key, callback_token)
|
||||
|
||||
return cancel_try_get_data(data_set, key, callback_token)
|
||||
end
|
||||
|
||||
local timeout_token =
|
||||
Token.register(
|
||||
function(data)
|
||||
cancel_try_get_data(data.data_set, data.key, data.callback_token)
|
||||
end
|
||||
)
|
||||
|
||||
--- Same as Server.try_get_data but the request is cancelled if the timeout expires before the request is complete.
|
||||
-- If the request is cancelled before it is complete the callback will be called with nil.
|
||||
-- There can only be one request per data_set, key, callback_token combo. If there is already an ongoing request
|
||||
-- an attempt to make a new one will be ignored.
|
||||
-- @param data_set<string>
|
||||
-- @param key<string>
|
||||
-- @param callback_token<token>
|
||||
-- @usage
|
||||
-- local Server = require 'features.server'
|
||||
-- local Token = require 'utils.token'
|
||||
--
|
||||
-- local callback =
|
||||
-- Token.register(
|
||||
-- function(data)
|
||||
-- if not data then
|
||||
-- return -- The request timed out.
|
||||
-- end
|
||||
--
|
||||
-- local data_set = data.data_set
|
||||
-- local key = data.key
|
||||
-- local value = data.value -- will be nil if no data
|
||||
--
|
||||
-- game.print(data_set .. ':' .. key .. ':' .. tostring(value))
|
||||
-- end
|
||||
-- )
|
||||
--
|
||||
-- Server.try_get_data_timeout('my data set', 'key 1', callback, 60)
|
||||
function Public.try_get_data_timeout(data_set, key, callback_token, timeout_ticks)
|
||||
validate_arguments(data_set, key, callback_token)
|
||||
|
||||
try_get_data_cancelable(data_set, key, callback_token)
|
||||
|
||||
Task.set_timeout_in_ticks(
|
||||
timeout_ticks,
|
||||
timeout_token,
|
||||
{data_set = data_set, key = key, callback_token = callback_token}
|
||||
)
|
||||
end
|
||||
|
||||
--- Gets all the data for the data_set from the web server's persistent data storage.
|
||||
|
Loading…
Reference in New Issue
Block a user