-
-
Notifications
You must be signed in to change notification settings - Fork 379
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
one thing to consider is if we want to add filtering to the listeners by list? or should we move the listeners to the list?
- Loading branch information
1 parent
b22cb4a
commit 6fdff8b
Showing
5 changed files
with
84 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
---@alias HarpoonListener fun(type: string, args: any[] | any | nil): nil | ||
|
||
---@class HarpoonListeners | ||
---@field listeners (HarpoonListener)[] | ||
---@field listenersByType (table<string, HarpoonListener>)[] | ||
local HarpoonListeners = {} | ||
|
||
HarpoonListeners.__index = HarpoonListeners | ||
|
||
function HarpoonListeners:new() | ||
return setmetatable({ | ||
listeners = {}, | ||
listenersByType = {} | ||
}, self) | ||
end | ||
|
||
---@param cbOrType HarpoonListener | string | ||
---@param cbOrNil HarpoonListener | string | ||
function HarpoonListeners:add_listener(cbOrType, cbOrNil) | ||
if (type(cbOrType) == "string") then | ||
if not self.listenersByType[cbOrType] then | ||
self.listenersByType[cbOrType] = {} | ||
end | ||
table.insert(self.listenersByType[cbOrType], cbOrNil) | ||
else | ||
table.insert(self.listeners, cbOrType) | ||
end | ||
end | ||
|
||
function HarpoonListeners:clear_listeners() | ||
self.listeners = {} | ||
end | ||
|
||
---@param type string | ||
---@param args any[] | any | nil | ||
function HarpoonListeners:emit(type, args) | ||
for _, cb in ipairs(self.listeners) do | ||
cb(type, args) | ||
end | ||
|
||
local listeners = self.listenersByType[type] | ||
if listeners ~= nil then | ||
for _, cb in ipairs(listeners) do | ||
cb(type, args) | ||
end | ||
end | ||
end | ||
|
||
return { | ||
listeners = HarpoonListeners:new(), | ||
event_names = { | ||
ADD = "ADD", | ||
SELECT = "SELECT", | ||
REMOVE = "REMOVE", | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters