-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.lua
56 lines (47 loc) · 1.26 KB
/
queue.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
53
54
55
56
local types = import("types.lua")
-----------------
-- Queue Class --
-----------------
local Queue = types.type("Queue")
-- Queue records function calls on an object for later execution on any object.
function Queue:new()
local instance = {}
setmetatable(instance, Queue)
return instance
end
-- WARNING: Do not pass non-trailing nil arguments to Queue; unpack will stop at the first nil and all following
-- arguments will never be passed to the queued function.
function Queue:__index(key)
for k, v in pairs(Queue) do
if k == key then
return v
end
end
return function(...)
local arg = {...}
local include_self = false
if #arg > 0 and tostring(arg[1]) == tostring(self) then
include_self = true
table.remove(arg, 1)
end
table.insert(self, {key = key, include_self = include_self, arg = arg})
end
end
function Queue:_start(target)
if DEBUG then types.force({"table"}, target) end
for key, item in ipairs(self) do
if not target[item.key] then
error(("libuix->Queue:_start(): attempt to call field '%s' (a nil value)"):format(item.key))
end
if item.include_self then
target[item.key](target, unpack(item.arg))
else
target[item.key](unpack(item.arg))
end
self[key] = nil
end
end
-------------
-- Exports --
-------------
return Queue