-
-
Notifications
You must be signed in to change notification settings - Fork 170
Expand file tree
/
Copy pathnotify.lua
More file actions
40 lines (32 loc) · 778 Bytes
/
notify.lua
File metadata and controls
40 lines (32 loc) · 778 Bytes
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
local log = require('plenary.log')
local M = {}
M.STATUS = 'status'
M.MESSAGE = 'message'
M.listeners = {}
--- Publish an event with a message
---@param event_name string
---@param data any
function M.publish(event_name, data)
if M.listeners[event_name] then
if data and data ~= '' then
log.debug(event_name .. ':', data)
end
for _, callback in ipairs(M.listeners[event_name]) do
callback(data)
end
end
end
--- Listen for an event
---@param event_name string
---@param callback fun(data:any)
function M.listen(event_name, callback)
if not M.listeners[event_name] then
M.listeners[event_name] = {}
end
table.insert(M.listeners[event_name], callback)
end
--- Clear all listeners
function M.clear()
M.listeners = {}
end
return M