matcher: Use gears.object.
This way there's some signals. It can be useful if the module using the matcher needs to act when something happens.
This commit is contained in:
parent
d8b53dac5d
commit
886f8ea4e3
|
@ -35,10 +35,45 @@
|
||||||
local gtable = require("gears.table")
|
local gtable = require("gears.table")
|
||||||
local gsort = require("gears.sort")
|
local gsort = require("gears.sort")
|
||||||
local gdebug = require("gears.debug")
|
local gdebug = require("gears.debug")
|
||||||
|
local gobject = require("gears.object")
|
||||||
local protected_call = require("gears.protected_call")
|
local protected_call = require("gears.protected_call")
|
||||||
|
|
||||||
local matcher = {}
|
local matcher = {}
|
||||||
|
|
||||||
|
--- A rule has been added to a set of matching rules.
|
||||||
|
-- @signal rule::appended
|
||||||
|
-- @tparam table gears.matcher The matcher.
|
||||||
|
-- @tparam table rule The rule.
|
||||||
|
-- @tparam table source The matching rules source name.
|
||||||
|
-- @tparam table content The matching rules source content.
|
||||||
|
-- @see append_rule
|
||||||
|
-- @see append_rules
|
||||||
|
|
||||||
|
--- A rule has been removed to a set of matching rules.
|
||||||
|
-- @signal rule::removed
|
||||||
|
-- @tparam table gears.matcher The matcher.
|
||||||
|
-- @tparam table rule The rule.
|
||||||
|
-- @tparam table source The matching rules source name.
|
||||||
|
-- @tparam table content The matching rules source content.
|
||||||
|
-- @see remove_rule
|
||||||
|
|
||||||
|
--- A matching source function has been added.
|
||||||
|
-- @signal matching_function::added
|
||||||
|
-- @tparam table gears.matcher The matcher.
|
||||||
|
-- @tparam function callback The callback.
|
||||||
|
-- @see add_matching_function
|
||||||
|
|
||||||
|
--- A matching source table has been added.
|
||||||
|
-- @signal matching_rules::added
|
||||||
|
-- @tparam table gears.matcher The matcher.
|
||||||
|
-- @tparam function callback The callback.
|
||||||
|
-- @see add_matching_rules
|
||||||
|
|
||||||
|
--- A matching source function has been removed.
|
||||||
|
-- @signal matching_source::removed
|
||||||
|
-- @tparam table gears.matcher The matcher.
|
||||||
|
-- @see remove_matching_source
|
||||||
|
|
||||||
-- Check if an object matches a rule.
|
-- Check if an object matches a rule.
|
||||||
-- @param o The object.
|
-- @param o The object.
|
||||||
-- #tparam table rule The rule to check.
|
-- #tparam table rule The rule to check.
|
||||||
|
@ -162,6 +197,8 @@ function matcher:add_matching_rules(name, rules, depends_on, precede)
|
||||||
|
|
||||||
self._matching_rules[name] = rules
|
self._matching_rules[name] = rules
|
||||||
|
|
||||||
|
self:emit_signal("matching_rules::added", rules)
|
||||||
|
|
||||||
return self:add_matching_function(name, matching_fct, depends_on, precede)
|
return self:add_matching_function(name, matching_fct, depends_on, precede)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -230,6 +267,8 @@ function matcher:add_matching_function(name, callback, depends_on, precede)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
self:emit_signal("matching_function::added", callback)
|
||||||
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -245,11 +284,13 @@ function matcher:remove_matching_source(name)
|
||||||
|
|
||||||
for k, v in ipairs(self._matching_source) do
|
for k, v in ipairs(self._matching_source) do
|
||||||
if v.name == name then
|
if v.name == name then
|
||||||
|
self:emit_signal("matching_source::removed", v)
|
||||||
table.remove(self._matching_source, k)
|
table.remove(self._matching_source, k)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
self._matching_rules[name] = nil
|
self._matching_rules[name] = nil
|
||||||
|
|
||||||
return false
|
return false
|
||||||
|
@ -306,6 +347,7 @@ function matcher:append_rule(source, rule)
|
||||||
self:add_matching_rules(source, {}, {}, {})
|
self:add_matching_rules(source, {}, {}, {})
|
||||||
end
|
end
|
||||||
table.insert(self._matching_rules[source], rule)
|
table.insert(self._matching_rules[source], rule)
|
||||||
|
self:emit_signal("rule::appended", rule, source, self._matching_rules[source])
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Add a new rules to the default set.
|
--- Add a new rules to the default set.
|
||||||
|
@ -329,6 +371,7 @@ function matcher:remove_rule(source, rule)
|
||||||
for k, v in ipairs(self._matching_rules[source]) do
|
for k, v in ipairs(self._matching_rules[source]) do
|
||||||
if v == rule then
|
if v == rule then
|
||||||
table.remove(self._matching_rules[source], k)
|
table.remove(self._matching_rules[source], k)
|
||||||
|
self:emit_signal("rule::removed", rule, source, self._matching_rules[source])
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -343,7 +386,9 @@ local module = {}
|
||||||
-- @return A new rule solver object.
|
-- @return A new rule solver object.
|
||||||
|
|
||||||
local function new()
|
local function new()
|
||||||
local ret = {}
|
local ret = gobject()
|
||||||
|
|
||||||
|
rawset(ret, "_private", {rules = {}})
|
||||||
|
|
||||||
-- Contains the sources.
|
-- Contains the sources.
|
||||||
-- The elements are ordered "first in, first executed". Thus, the higher the
|
-- The elements are ordered "first in, first executed". Thus, the higher the
|
||||||
|
|
Loading…
Reference in New Issue