Skip to content

Commit

Permalink
Callback on delete messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Emojigit committed Jan 20, 2025
1 parent b5f5e5b commit 1284817
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 3 deletions.
1 change: 1 addition & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ echo.send_event_to(player_name, {
* `echo.register_on_send_event(function(event, notification))`: Called when an event is sent
* You can get the target player in `event.to`.
* `echo.register_on_read_message(function(event, notification))`: Called when an notification is read
* `echo.register_on_delete_message(function(event, notification))`: Called when an notification is deleted
8 changes: 8 additions & 0 deletions src/api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ function echo.register_on_read_message(func)
echo.registered_on_read_message[#echo.registered_on_read_message + 1] = func
end

echo.registered_on_delete_message = {}
function echo.register_on_delete_message(func)
assert(type(func) == "function", string.format(
"Invalid type fo argument func (function expected, got %s)", type(func)))

echo.registered_on_delete_message[#echo.registered_on_delete_message + 1] = func
end

-- source: https://gist.github.com/jrus/3197011
local random = math.random
local function new_uuid()
Expand Down
7 changes: 7 additions & 0 deletions src/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,14 @@ local function render_notif_row(event, notification)
on_event = function(e_player)
local e_name = e_player:get_player_name()
local storage = echo.get_notifications_entry(e_name)
event = echo.get_event(event)

for i, s_event in ipairs(storage) do
if s_event == event then
table.remove(storage, i)
for _, func in ipairs(echo.registered_on_delete_message) do
func(event, echo.event_to_notification(event))
end
echo.log_notifications_modification(e_name)
return true
end
Expand Down Expand Up @@ -185,6 +189,9 @@ echo.echo_gui = flow.make_gui(function(player, ctx)

for i=#e_storage, 1, -1 do
local e_event = e_storage[i]
for _, func in ipairs(echo.registered_on_delete_message) do
func(e_event, echo.event_to_notification(e_event))
end
if os.date("%Y/%m/%d", e_event.time) == event_day then
table.remove(e_storage, i)
end
Expand Down
7 changes: 5 additions & 2 deletions src/on_send.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ echo.register_on_send_event(function(event, notification)
end
end)

echo.register_on_read_message(function(event)
local function simple_update(event)
local name = event.to
local player = core.get_player_by_name(name)
if player and huds[name] then
update_hud(player)
end
end)
end

echo.register_on_read_message(simple_update)
echo.register_on_delete_message(simple_update)
5 changes: 4 additions & 1 deletion src/storage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ end
---@param name string The name of the player
function echo.clear_notifications_entry(name)
local storage = echo.get_notifications_entry(name)
for i in ipairs(storage) do
for i, event in ipairs(storage) do
for _, func in ipairs(echo.registered_on_delete_message) do
func(event, echo.event_to_notification(event))
end
storage[i] = nil
end
echo.log_notifications_modification(name)
Expand Down

0 comments on commit 1284817

Please sign in to comment.