mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
small refactorings
This commit is contained in:
parent
1aa51a9ac5
commit
88d5b837e8
44
beholder.lua
44
beholder.lua
@ -5,17 +5,13 @@
|
|||||||
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||||
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN callback OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN callback OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
|
||||||
local function falseIfZero(n)
|
|
||||||
return n > 0 and n
|
|
||||||
end
|
|
||||||
|
|
||||||
local function copy(t)
|
local function copy(t)
|
||||||
local c={}
|
local c={}
|
||||||
for i=1,#t do c[i]=t[i] end
|
for i=1,#t do c[i]=t[i] end
|
||||||
return c
|
return c
|
||||||
end
|
end
|
||||||
|
|
||||||
-- private node-exclusive functions
|
-- private Node class
|
||||||
|
|
||||||
local Node = {
|
local Node = {
|
||||||
_nodesById = setmetatable({}, {__mode="k"})
|
_nodesById = setmetatable({}, {__mode="k"})
|
||||||
@ -42,7 +38,7 @@ function Node:findOrCreateDescendant(keys)
|
|||||||
return node
|
return node
|
||||||
end
|
end
|
||||||
|
|
||||||
function Node:executeCallbacks(params)
|
function Node:invokeCallbacks(params)
|
||||||
local counter = 0
|
local counter = 0
|
||||||
for _,callback in pairs(self.callbacks) do
|
for _,callback in pairs(self.callbacks) do
|
||||||
callback(unpack(params))
|
callback(unpack(params))
|
||||||
@ -51,34 +47,29 @@ function Node:executeCallbacks(params)
|
|||||||
return counter
|
return counter
|
||||||
end
|
end
|
||||||
|
|
||||||
function Node:executeAllCallbacks(params)
|
function Node:invokeAllCallbacksInSubTree(params)
|
||||||
local counter = self:executeCallbacks(params)
|
local counter = self:invokeCallbacks(params)
|
||||||
for _,child in pairs(self.children) do
|
for _,child in pairs(self.children) do
|
||||||
counter = counter + child:executeAllCallbacks(params)
|
counter = counter + child:invokeAllCallbacksInSubTree(params)
|
||||||
end
|
end
|
||||||
return counter
|
return counter
|
||||||
end
|
end
|
||||||
|
|
||||||
function Node:executeEventCallbacks(event)
|
function Node:invokeCallbacksFromPath(path)
|
||||||
local params = copy(event)
|
|
||||||
local counter = self:executeCallbacks(params)
|
|
||||||
local node = self
|
local node = self
|
||||||
|
local params = copy(path)
|
||||||
|
local counter = node:invokeCallbacks(params)
|
||||||
|
|
||||||
for i=1, #event do
|
for i=1, #path do
|
||||||
node = node.children[event[i]]
|
node = node.children[path[i]]
|
||||||
if not node then break end
|
if not node then break end
|
||||||
table.remove(params, 1)
|
table.remove(params, 1)
|
||||||
counter = counter + node:executeCallbacks(params)
|
counter = counter + node:invokeCallbacks(params)
|
||||||
end
|
end
|
||||||
|
|
||||||
return counter
|
return counter
|
||||||
end
|
end
|
||||||
|
|
||||||
function Node:removeCallback(id)
|
|
||||||
self.callbacks[id] = nil
|
|
||||||
Node._nodesById[id] = nil
|
|
||||||
end
|
|
||||||
|
|
||||||
function Node:addCallback(callback)
|
function Node:addCallback(callback)
|
||||||
local id = {}
|
local id = {}
|
||||||
self.callbacks[id] = callback
|
self.callbacks[id] = callback
|
||||||
@ -86,8 +77,17 @@ function Node:addCallback(callback)
|
|||||||
return id
|
return id
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function Node:removeCallback(id)
|
||||||
|
self.callbacks[id] = nil
|
||||||
|
Node._nodesById[id] = nil
|
||||||
|
end
|
||||||
|
|
||||||
-- beholder private functions
|
-- beholder private functions
|
||||||
|
|
||||||
|
local function falseIfZero(n)
|
||||||
|
return n > 0 and n
|
||||||
|
end
|
||||||
|
|
||||||
local function checkSelf(self, methodName)
|
local function checkSelf(self, methodName)
|
||||||
assert(type(self)=="table" and self._root, "Use beholder:" .. methodName .. " instead of beholder." .. methodName)
|
assert(type(self)=="table" and self._root, "Use beholder:" .. methodName .. " instead of beholder." .. methodName)
|
||||||
end
|
end
|
||||||
@ -122,12 +122,12 @@ end
|
|||||||
|
|
||||||
function beholder:trigger(...)
|
function beholder:trigger(...)
|
||||||
checkSelf(self, 'trigger')
|
checkSelf(self, 'trigger')
|
||||||
return falseIfZero( self._root:executeEventCallbacks({...}) )
|
return falseIfZero( self._root:invokeCallbacksFromPath({...}) )
|
||||||
end
|
end
|
||||||
|
|
||||||
function beholder:triggerAll(...)
|
function beholder:triggerAll(...)
|
||||||
checkSelf(self, 'triggerAll')
|
checkSelf(self, 'triggerAll')
|
||||||
return falseIfZero( self._root:executeAllCallbacks({...}) )
|
return falseIfZero( self._root:invokeAllCallbacksInSubTree({...}) )
|
||||||
end
|
end
|
||||||
|
|
||||||
function beholder:reset()
|
function beholder:reset()
|
||||||
|
Loading…
Reference in New Issue
Block a user