added function beholder.stopPropagation

Event can now be stopped from propagating further on.
Usage/Example: https://gist.github.com/LukeMS/5e6bc467a420ee2cc7544f690c3c4fae
This commit is contained in:
Lucas de Morais Siqueira 2017-01-29 11:31:12 -02:00 committed by GitHub
parent 9f1a31eb51
commit a0c0f794dc

View File

@ -49,8 +49,10 @@ end
local function invokeNodeCallbacks(self, params)
-- copy the hash into an array, for safety (self-erasures)
local callbacks, count = hash2array(self.callbacks)
self.stop_propagation = false
for i=1,#callbacks do
callbacks[i](unpack(params))
if self.stop_propagation then break end
end
return count
end
@ -137,6 +139,13 @@ function beholder.observe(...)
return addIdToCurrentGroup(addCallbackToNode(node, callback))
end
-- When called the event will stop and no longer propagate
-- (i.e. loop at invokeNodeCallbacks will break)
function beholder.stopPropagation(id)
local node = findNodeById(id)
node.stop_propagation = true
end
function beholder.stopObserving(id)
local node = findNodeById(id)
if node then removeCallbackFromNode(node, id) end