mirror of
https://github.com/Keyslam-Group/Concord.git
synced 2025-09-02 12:24:11 -04:00
Added beforeEmit and afterEmit World callbacks (#54)
* Added beforeEmit and afterEmit World callbacks * Fixed beforeEmit/afterEmit to handle recursive/nested emits; * Added preventDefaults in beforeEmit
This commit is contained in:
parent
429a448ab6
commit
1e4132be21
1 changed files with 16 additions and 1 deletions
|
@ -52,6 +52,8 @@ function World.new()
|
||||||
__systemLookup = {},
|
__systemLookup = {},
|
||||||
|
|
||||||
__isWorld = true,
|
__isWorld = true,
|
||||||
|
|
||||||
|
__ignoreEmits = false
|
||||||
}, World.__mt)
|
}, World.__mt)
|
||||||
|
|
||||||
-- Optimization: We deep copy the World class into our instance of a world.
|
-- Optimization: We deep copy the World class into our instance of a world.
|
||||||
|
@ -320,7 +322,14 @@ function World:emit(functionName, ...)
|
||||||
|
|
||||||
self.__emitSDepth = self.__emitSDepth + 1
|
self.__emitSDepth = self.__emitSDepth + 1
|
||||||
|
|
||||||
local listeners = self.__events[functionName]
|
local listeners = self.__events[functionName]
|
||||||
|
|
||||||
|
if not self.__ignoreEmits and Type.isCallable(self.beforeEmit) then
|
||||||
|
self.__ignoreEmits = true
|
||||||
|
local preventDefaults = self:beforeEmit(functionName, listeners, ...)
|
||||||
|
self.__ignoreEmits = false
|
||||||
|
if preventDefaults then return end
|
||||||
|
end
|
||||||
|
|
||||||
if listeners then
|
if listeners then
|
||||||
for i = 1, #listeners do
|
for i = 1, #listeners do
|
||||||
|
@ -336,6 +345,12 @@ function World:emit(functionName, ...)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
if not self.__ignoreEmits and Type.isCallable(self.afterEmit) then
|
||||||
|
self.__ignoreEmits = true
|
||||||
|
self:afterEmit(functionName, listeners, ...)
|
||||||
|
self.__ignoreEmits = false
|
||||||
|
end
|
||||||
|
|
||||||
self.__emitSDepth = self.__emitSDepth - 1
|
self.__emitSDepth = self.__emitSDepth - 1
|
||||||
|
|
||||||
return self
|
return self
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue