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
12099local token_handlers = {}
121100local token_nth_tick_handlers = {}
122- local function_handlers = {}
123- local function_nth_tick_handlers = {}
124101
125102Global .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
301274end
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 )
327279end
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 )
352284end
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
404336end
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 )
430341end
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 )
455346end
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
500377end
501378
0 commit comments