-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathanotifier.lua
111 lines (96 loc) · 2.18 KB
/
anotifier.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
#! /usr/bin/env lua
--
-- anotifier.lua
-- Copyright (C) 2021 Shewer Lu <[email protected]>
--
-- Distributed under terms of the MIT license.
--
require('tools/metatable')
require("tools/object")
local Notifier=Class("Notifier")
function Notifier:_initialize(notifier)
self:setup(notifier)
return self
end
function Notifier:setup(notifier)
self:reset()
self._list=metatable()
self._mainfunc=self:_gener_mainfunc(self._list)
self:connect(notifier)
end
function Notifier:_gener_mainfunc(list)
return function(...)
for _,ffunc in ipairs(list) do
if type(ffunc) == "function" and ffunc(...) then
break
end
end
end
end
function Notifier:disconnect()
if self._connection then
self._connection:disconnect()
self._connection=nil
end
end
function Notifier:connect(notifier)
self:disconnect()
self._connection= self._mainfunc and notifier:connect( self._mainfunc ) or nil
end
function Notifier:reset()
self:disconnect()
self._list=nil
end
function Notifier:append(func)
self._list:insert(func)
end
function Notifier:remove(func)
return self._list:delete(func) --index and self._list:remove(index)
end
function Notifier:funcs()
return self._list
end
function Notifier:callback(...)
self._mainfunc(...)
end
local notifier_name= {
commit = "commit_notifier",
select = "select_notifier",
update = "update_notifier",
delete = "delete_notifier",
option = "option_update_notifier",
property = "property_update_notifier",
unhandled = "unhandled_key_notifier",
}
local Notifiers=Class("Notifiers")
function Notifiers:_initialize(ctx)
if ctx then
self:setup(ctx)
end
return self
end
function Notifiers:setup(ctx)
for k,v in pairs(notifier_name) do
self[k] = Notifier( ctx[v] )
end
if log then
log.error( "------ delete notifier " .. tostring(ctx) .. "---" .. tostring(self.delete))
end
end
function Notifiers:reset()
for k,v in pairs(notifier_name) do
self[k]:reset()
self[k]=nil
end
end
function Notifiers:connect()
for k,v in pairs(notifier_name) do
self[k]:connect()
end
end
function Notifiers:disconnect()
for k,v in pairs(notifier_name) do
self[k]:disconnect()
end
end
return Notifiers