Skip to content

Commit bd32682

Browse files
author
L'In20Cible
committed
Delay instances created by Entity.delay are now removed from the global dictionary upon execution.
1 parent 2315e41 commit bd32682

File tree

1 file changed

+17
-1
lines changed
  • addons/source-python/packages/source-python/entities

1 file changed

+17
-1
lines changed

addons/source-python/packages/source-python/entities/entity.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,8 +497,24 @@ def delay(self, delay, callback, *args, **kwargs):
497497
:return: The delay instance.
498498
:rtype: Delay
499499
"""
500+
# TODO: Ideally, we want to subclass Delay and cleanup on cancel() too
501+
# in case the caller manually cancel the returned Delay.
502+
def _callback(*args, **kwargs):
503+
"""Called when the delay is executed."""
504+
# Remove the delay from the global dictionary...
505+
_entity_delays[self.index].remove(delay)
506+
507+
# Was this the last pending delay for the entity?
508+
if not _entity_delays[self.index]:
509+
510+
# Remove the entity from the dictionary...
511+
del _entity_delays[self.index]
512+
513+
# Call the callback...
514+
callback(*args, **kwargs)
515+
500516
# Get the delay instance...
501-
delay = Delay(delay, callback, *args, **kwargs)
517+
delay = Delay(delay, _callback, *args, **kwargs)
502518

503519
# Add the delay to the dictionary...
504520
_entity_delays[self.index].add(delay)

0 commit comments

Comments
 (0)