-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathdispatch.lua
52 lines (48 loc) · 1.3 KB
/
dispatch.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
local parser = require("overseer.parser")
local Dispatch = {
desc = "Dispatch an event",
long_desc = "Events can be subscribed to using the parser:subscribe() method.",
doc_args = {
{
name = "name",
type = "string",
desc = "Event name",
},
{
name = "arg",
type = "any|fun()",
desc = "A value to send with the event, or a function that creates a value",
vararg = true,
},
},
examples = {
{
desc = [[clear_results will clear all current results from the parser. Pass `true` to only clear the results under the current key]],
code = [[{"dispatch", "clear_results"}]],
},
{
desc = [[set_results is used by the on_output_parse component to immediately set the current results on the task]],
code = [[{"dispatch", "set_results"}]],
},
},
}
function Dispatch.new(name, ...)
return setmetatable({
event_name = name,
args = { ... },
}, { __index = Dispatch })
end
function Dispatch:reset() end
function Dispatch:ingest(line, ctx)
local params = {}
for _, v in ipairs(self.args) do
if type(v) == "function" then
table.insert(params, v(line, ctx))
else
table.insert(params, v)
end
end
ctx.dispatch(self.event_name, unpack(params))
return parser.STATUS.SUCCESS
end
return Dispatch