-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfilter.lua
113 lines (92 loc) · 2.61 KB
/
filter.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
local _, ns = ...
local oGlow = ns.oGlow
local argcheck = oGlow.argcheck
local pipesTable = ns.pipesTable
local filtersTable = {}
local activeFilters = {}
local numFilters = 0
function oGlow:RegisterFilter(name, type, filter, desc)
argcheck(name, 2, 'string')
argcheck(type, 3, 'string')
argcheck(filter, 4, 'function')
argcheck(desc, 5, 'string', 'nil')
if(filtersTable[name]) then return nil, 'Filter function is already registered.' end
filtersTable[name] = {type, filter, name, desc}
numFilters = numFilters + 1
return true
end
do
local function iter(_, n)
local n, t = next(filtersTable, n)
if(t) then
return n, t[1], t[4]
end
end
function oGlow.IterateFilters()
return iter, nil, nil
end
end
-- TODO: Validate that the display we try to use actually exists.
function oGlow:RegisterFilterOnPipe(pipe, filter)
argcheck(pipe, 2, 'string')
argcheck(filter, 3, 'string')
if(not pipesTable[pipe]) then return nil, 'Pipe does not exist.' end
if(not filtersTable[filter]) then return nil, 'Filter does not exist.' end
-- XXX: Clean up this logic.
if(not activeFilters[pipe]) then
local filterTable = filtersTable[filter]
local display = filterTable[1]
activeFilters[pipe] = {}
activeFilters[pipe][display] = {}
table.insert(activeFilters[pipe][display], filterTable)
else
local filterTable = filtersTable[filter]
local ref = activeFilters[pipe][filterTable[1]]
for _, func in next, ref do
if(func == filter) then
return nil, 'Filter function is already registered.'
end
end
table.insert(ref, filterTable)
end
if(not oGlowDB.EnabledFilters[filter]) then
oGlowDB.EnabledFilters[filter] = {}
end
oGlowDB.EnabledFilters[filter][pipe] = true
return true
end
oGlow.IterateFiltersOnPipe = function(pipe)
local t = activeFilters[pipe]
return coroutine.wrap(function()
if(t) then
for _, sub in next, t do
for k, v in next, sub do
coroutine.yield(v[3], v[1], v[4])
end
end
end
end)
end
function oGlow:UnregisterFilterOnPipe(pipe, filter)
argcheck(pipe, 2, 'string')
argcheck(filter, 3, 'string')
if(not pipesTable[pipe]) then return nil, 'Pipe does not exist.' end
if(not filtersTable[filter]) then return nil, 'Filter does not exist.' end
--- XXX: Be more defensive here.
local filterTable = filtersTable[filter]
local ref = activeFilters[pipe][filterTable[1]]
if(ref) then
for k, func in next, ref do
if(func == filterTable) then
table.remove(ref, k)
oGlowDB.EnabledFilters[filter][pipe] = nil
return true
end
end
end
end
function oGlow:GetNumFilters()
return numFilters
end
ns.filtersTable = filtersTable
ns.activeFilters = activeFilters