Skip to content

Commit b5dfd2a

Browse files
authored
Loop handlers backwards (#1485)
* Loop handlers backwards * Deprecate event's removable functions
1 parent 151c3cd commit b5dfd2a

File tree

2 files changed

+19
-142
lines changed

2 files changed

+19
-142
lines changed

utils/event.lua

Lines changed: 12 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -48,27 +48,6 @@
4848
-- It's not an error to register the same token multiple times to the same event, however when
4949
-- removing only the first occurrence is removed.
5050
--
51-
-- ** Event.add_removable_function(event_name, func) **
52-
--
53-
-- Only use this function if you can't use Event.add_removable. i.e you are registering the handler at the console.
54-
-- The same restrictions that apply to Event.add_removable also apply to Event.add_removable_function.
55-
-- func cannot be a closure in this case, as there is no safe way to store closures in the global table.
56-
-- A closure is a function that uses a local variable not defined in the function.
57-
--
58-
-- @usage
59-
-- local Event = require 'utils.event'
60-
--
61-
-- If you want to remove the handler you will need to keep a reference to it.
62-
-- storage.handler = function(event)
63-
-- game.print(serpent.block(event)) -- prints the content of the event table to console.
64-
-- end
65-
--
66-
-- The below code would typically be used at the command console.
67-
-- Event.add_removable_function(defines.events.on_built_entity, storage.handler)
68-
--
69-
-- When you no longer need the handler.
70-
-- Event.remove_removable_function(defines.events.on_built_entity, storage.handler)
71-
--
7251
-- ** Other Events **
7352
--
7453
-- Use Event.on_init(handler) for script.on_init(handler)
@@ -119,21 +98,15 @@ local on_nth_tick_event_handlers = EventCore.get_on_nth_tick_event_handlers()
11998

12099
local token_handlers = {}
121100
local token_nth_tick_handlers = {}
122-
local function_handlers = {}
123-
local function_nth_tick_handlers = {}
124101

125102
Global.register(
126103
{
127104
token_handlers = token_handlers,
128105
token_nth_tick_handlers = token_nth_tick_handlers,
129-
function_handlers = function_handlers,
130-
function_nth_tick_handlers = function_nth_tick_handlers
131106
},
132107
function(tbl)
133108
token_handlers = tbl.token_handlers
134109
token_nth_tick_handlers = tbl.token_nth_tick_handlers
135-
function_handlers = tbl.function_handlers
136-
function_nth_tick_handlers = tbl.function_nth_tick_handlers
137110
end
138111
)
139112

@@ -300,55 +273,14 @@ function Event.remove_removable(event_name, token)
300273
end
301274
end
302275

303-
--- Register a handler that can be safely added and removed at runtime.
304-
-- The handler must not be a closure, as that is a desync risk.
305-
-- Do NOT call this method during on_load.
306-
-- See documentation at top of file for details on using events.
307-
-- @param event_name<number>
308-
-- @param func<function>
309-
function Event.add_removable_function(event_name, func)
310-
if _LIFECYCLE == stage_load then
311-
error('cannot call during on_load', 2)
312-
end
313-
if type(func) ~= 'function' then
314-
error('func must be a function', 2)
315-
end
316-
317-
local funcs = function_handlers[event_name]
318-
if not funcs then
319-
function_handlers[event_name] = {func}
320-
else
321-
funcs[#funcs + 1] = func
322-
end
323-
324-
if handlers_added then
325-
core_add(event_name, func)
326-
end
276+
---@deprecated
277+
function Event.add_removable_function()
278+
error('Attempting to call deprecated method of Event.', 2)
327279
end
328280

329-
--- Removes a handler for the given event_name.
330-
-- Do NOT call this method during on_load.
331-
-- See documentation at top of file for details on using events.
332-
-- @param event_name<number>
333-
-- @param func<function>
334-
function Event.remove_removable_function(event_name, func)
335-
if _LIFECYCLE == stage_load then
336-
error('cannot call during on_load', 2)
337-
end
338-
local funcs = function_handlers[event_name]
339-
340-
if not funcs then
341-
return
342-
end
343-
344-
local handlers = event_handlers[event_name]
345-
346-
remove(funcs, func)
347-
remove(handlers, func)
348-
349-
if #handlers == 0 then
350-
script_on_event(event_name, nil)
351-
end
281+
---@deprecated
282+
function Event.remove_removable_function()
283+
error('Attempting to call deprecated method of Event.', 2)
352284
end
353285

354286
--- Register a token handler for the nth tick that can be safely added and removed at runtime.
@@ -403,55 +335,14 @@ function Event.remove_removable_nth_tick(tick, token)
403335
end
404336
end
405337

406-
--- Register a handler for the nth tick that can be safely added and removed at runtime.
407-
-- The handler must not be a closure, as that is a desync risk.
408-
-- Do NOT call this method during on_load.
409-
-- See documentation at top of file for details on using events.
410-
-- @param tick<number>
411-
-- @param func<function>
412-
function Event.add_removable_nth_tick_function(tick, func)
413-
if _LIFECYCLE == stage_load then
414-
error('cannot call during on_load', 2)
415-
end
416-
if type(func) ~= 'function' then
417-
error('func must be a function', 2)
418-
end
419-
420-
local funcs = function_nth_tick_handlers[tick]
421-
if not funcs then
422-
function_nth_tick_handlers[tick] = {func}
423-
else
424-
funcs[#funcs + 1] = func
425-
end
426-
427-
if handlers_added then
428-
core_on_nth_tick(tick, func)
429-
end
338+
---@deprecated
339+
function Event.add_removable_nth_tick_function()
340+
error('Attempting to call deprecated method of Event.', 2)
430341
end
431342

432-
--- Removes a handler for the nth tick.
433-
-- Do NOT call this method during on_load.
434-
-- See documentation at top of file for details on using events.
435-
-- @param tick<number>
436-
-- @param func<function>
437-
function Event.remove_removable_nth_tick_function(tick, func)
438-
if _LIFECYCLE == stage_load then
439-
error('cannot call during on_load', 2)
440-
end
441-
local funcs = function_nth_tick_handlers[tick]
442-
443-
if not funcs then
444-
return
445-
end
446-
447-
local handlers = on_nth_tick_event_handlers[tick]
448-
449-
remove(funcs, func)
450-
remove(handlers, func)
451-
452-
if #handlers == 0 then
453-
script_on_nth_tick(tick, nil)
454-
end
343+
---@deprecated
344+
function Event.remove_removable_nth_tick_function()
345+
error('Attempting to call deprecated method of Event.', 2)
455346
end
456347

457348
--- Generate a new, unique event ID.
@@ -475,27 +366,13 @@ local function add_handlers()
475366
end
476367
end
477368

478-
for event_name, funcs in pairs(function_handlers) do
479-
for i = 1, #funcs do
480-
local handler = funcs[i]
481-
core_add(event_name, handler)
482-
end
483-
end
484-
485369
for tick, tokens in pairs(token_nth_tick_handlers) do
486370
for i = 1, #tokens do
487371
local handler = Token.get(tokens[i])
488372
core_on_nth_tick(tick, handler)
489373
end
490374
end
491375

492-
for tick, funcs in pairs(function_nth_tick_handlers) do
493-
for i = 1, #funcs do
494-
local handler = funcs[i]
495-
core_on_nth_tick(tick, handler)
496-
end
497-
end
498-
499376
handlers_added = true
500377
end
501378

utils/event_core.lua

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@ local script_on_nth_tick = script.on_nth_tick
2222
local call_handlers
2323
if _DEBUG then
2424
function call_handlers(handlers, event)
25-
for i = 1, #handlers do
25+
for i = #handlers, 1, -1 do
2626
local handler = handlers[i]
2727
handler(event)
2828
end
2929
end
3030
else
3131
function call_handlers(handlers, event)
32-
for i = 1, #handlers do
32+
for i = #handlers, 1, -1 do
3333
local handler = handlers[i]
3434
local success, error = pcall(handler, event)
3535
if not success then
@@ -89,7 +89,7 @@ function Public.add(event_name, handler)
8989
event_handlers[event_name] = {handler}
9090
script_on_event(event_name, on_event)
9191
else
92-
table.insert(handlers, handler)
92+
table.insert(handlers, 1, handler)
9393
if #handlers == 1 then
9494
script_on_event(event_name, on_event)
9595
end
@@ -103,7 +103,7 @@ function Public.on_init(handler)
103103
event_handlers[init_event_name] = {handler}
104104
script.on_init(on_init)
105105
else
106-
table.insert(handlers, handler)
106+
table.insert(handlers, 1, handler)
107107
if #handlers == 1 then
108108
script.on_init(on_init)
109109
end
@@ -117,7 +117,7 @@ function Public.on_load(handler)
117117
event_handlers[load_event_name] = {handler}
118118
script.on_load(on_load)
119119
else
120-
table.insert(handlers, handler)
120+
table.insert(handlers, 1, handler)
121121
if #handlers == 1 then
122122
script.on_load(on_load)
123123
end
@@ -131,7 +131,7 @@ function Public.on_configuration_changed(handler)
131131
event_handlers[configuration_changed_name] = {handler}
132132
script.on_configuration_changed(configuration_changed)
133133
else
134-
table.insert(handlers, handler)
134+
table.insert(handlers, 1, handler)
135135
if #handlers == 1 then
136136
script.on_configuration_changed(configuration_changed)
137137
end
@@ -145,7 +145,7 @@ function Public.on_nth_tick(tick, handler)
145145
on_nth_tick_event_handlers[tick] = {handler}
146146
script_on_nth_tick(tick, on_nth_tick_event)
147147
else
148-
table.insert(handlers, handler)
148+
table.insert(handlers, 1, handler)
149149
if #handlers == 1 then
150150
script_on_nth_tick(tick, on_nth_tick_event)
151151
end

0 commit comments

Comments
 (0)