Created
November 9, 2018 06:19
-
-
Save njs50/c79454b822d85a415582eddbb92dbadc to your computer and use it in GitHub Desktop.
trigger helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local oKillTrigger = killTrigger | |
tempTriggers = tempTriggers or {} | |
local wrapTrigger = function (triggerCommand) | |
local oCommand = triggerCommand | |
return function (regex, code, expireAfter) | |
local trigId = nil | |
local oCode = code | |
local expires = expireAfter | |
local newCode = function (...) | |
if (tempTriggers[trigId]) then | |
tempTriggers[trigId].calls = tempTriggers[trigId].calls + 1 | |
if (tempTriggers[trigId].calls == expires) then | |
tempTriggers[trigId] = nil | |
cecho('\n<orange>trigger died of natural causes - ' .. trigId .. ' : ' .. regex .. '\n') | |
end | |
end | |
return oCode(...) | |
end | |
trigId = oCommand(regex, newCode, expires) | |
tempTriggers[trigId] = tempTriggers[trigId] or { calls = 0, regex = regex } | |
return trigId | |
end | |
end | |
tempRegexTrigger = wrapTrigger(tempRegexTrigger) | |
tempTrigger = wrapTrigger(tempTrigger) | |
killTrigger = function(id) | |
if (tempTriggers[id] ) then | |
tempTriggers[id] = nil | |
end | |
return oKillTrigger(id) | |
end | |
displayTempTriggers = function() | |
display(tempTriggers) | |
end | |
common.timedTempTrigger = function (timeout, maxTriggerCount, pattern, callback, failcallback) | |
local fcb = failcallback | |
local ocb = callback | |
local cb = function () | |
fcb = nil; | |
if (ocb) then | |
ocb() | |
end | |
end | |
local triggerId = tempRegexTrigger(pattern, cb, maxTriggerCount) | |
local tempTimerId = tempTimer(timeout, function () | |
-- echo('\n -- killing temp timed trigger\n') | |
killTrigger(triggerId) | |
if (fcb) then | |
fcb() | |
end | |
end) | |
end | |
timedTempTrigger = common.timedTempTrigger |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment