|  Developer
Updated on November 15, 2021

reset_arguments

  • Reset timer arguments by ID. Does nothing if timer doesn’t exist
Edit
Edit
fields type required description
timer_id string + ID which uniquely identifies the timer
arguments table New timer arguments
Edit
fields type description
none
Edit
				
					local timer = require("timer")

timer.set_interval_with_id(1000, "timer_id", "HUB:module_name", { arg_name = "arg_value" })

timer.reset_arguments("timer_id", { new_arg_name = "new_arg_value" })				
			

Sometimes we have to send step-by-step several requests via some plugin (following request has to be sent after report of previous request). This functionality can be easily implemented via timer.set_timeout_with_id and timer.reset_arguments.

				
					-- file "add_request". It can be executed any times with any request ids

local plugin_request_id = ...
print("add request with plugin id " .. plugin_request_id)

local timer = require "timer"
local arguments = timer.get_arguments(my_timer_id)

if arguments then
    -- perform request and start timer
    arguments = {
      current = plugin_request_id
      following = {}
    }

    local plugin = require "my_plugin"
    plugin.request_my_value(plugin_request_id)
    timer.set_timeout_with_id(5000, "my_timer_id", "HUB:timeouts/my_timeout_script", arguments)
elseif arguments.current ~= plugin_request_id and not arguments[plugin_request_id] then
    -- just add request to table
    arguments[plugin_request_id] = true
    timer.reset_arguments("my_timer_id", arguments)
end


-- file "on_report_received"

local reported_request_id, value = ...
print("report is received for " .. reported_request_id .. ". Value = " .. value)

local timer = require "timer"
local arguments = timer.get_arguments("my_timer_id")

if arguments and arguments.current == reported_request_id and next(arguments.following) then
    -- do next request
    local next_id = next(arguments.following)
    arguments.following[next_id] = nil
    arguments.current = next_id

    local plugin = require "my_plugin"
    plugin.request_my_value(next_id)
    timer.set_timeout_with_id(5000, "my_timer_id", "HUB:timeouts/my_timeout_script", arguments)
end


-- file "my_timeout_script"

local arguments = ...
print("timeout: report is not received for " .. arguments.current)

if next(arguments.following) then
    -- do next request
    local next_id = next(arguments.following)
    arguments.following[next_id] = nil
    arguments.current = next_id

    local plugin = require "my_plugin"
    local timer = require "timer"
    plugin.request_my_value(next_id)
    timer.set_timeout_with_id(5000, "my_timer_id", "HUB:timeouts/my_timeout_script", arguments)
end