2011-10-24 07:06:29 +00:00
-- beholder.lua - v1.0 (2011-11)
-- Copyright (c) 2011 Enrique García Cota
-- Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
-- The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
2011-10-30 11:57:21 +00:00
-- 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.
2011-10-24 07:06:29 +00:00
2011-10-25 22:18:17 +00:00
local beholder = { }
2011-10-24 07:06:29 +00:00
2011-10-29 00:45:39 +00:00
local function copy ( t )
local c = { }
for i = 1 , # t do c [ i ] = t [ i ] end
return c
end
2011-10-26 22:22:52 +00:00
2011-10-30 11:57:21 +00:00
local function extractEventAndCallbackFromParams ( params )
assert ( # params > 0 , " beholder:observe requires at least one parameter - the callback. You usually want to use two, i.e.: beholder:observe('EVENT', callback) " )
local callback = table.remove ( params , # params )
return params , callback
2011-10-26 22:22:52 +00:00
end
2011-10-28 23:39:19 +00:00
local function findNodeById ( self , id )
return self._nodesById [ id ]
end
2011-10-30 11:57:21 +00:00
local function findOrCreateChildNode ( node , key )
node.children [ key ] = node.children [ key ] or { callbacks = { } , children = { } }
return node.children [ key ]
end
local function findOrCreateDescendantNode ( node , keys )
for i = 1 , # keys do
node = findOrCreateChildNode ( node , keys [ i ] )
2011-10-29 00:45:39 +00:00
end
2011-10-30 11:57:21 +00:00
return node
2011-10-26 22:22:52 +00:00
end
2011-10-30 11:57:21 +00:00
local function executeNodeCallbacks ( node , params )
2011-10-29 00:45:39 +00:00
local counter = 0
2011-10-30 11:33:34 +00:00
params = params or { }
2011-10-30 11:57:21 +00:00
for _ , callback in pairs ( node.callbacks ) do
callback ( unpack ( params ) )
2011-10-30 11:33:34 +00:00
counter = counter + 1
end
return counter
end
2011-10-30 11:57:21 +00:00
local function executeAllCallbacks ( node )
local counter = executeNodeCallbacks ( node )
2011-10-30 11:33:34 +00:00
for _ , child in pairs ( node.children ) do
2011-10-30 11:57:21 +00:00
counter = counter + executeAllCallbacks ( child )
2011-10-30 11:33:34 +00:00
end
return counter
end
2011-10-30 11:57:21 +00:00
local function executeEventCallbacks ( node , event )
2011-10-29 00:45:39 +00:00
local params = copy ( event )
2011-10-30 11:57:21 +00:00
local counter = executeNodeCallbacks ( node , params )
2011-10-29 00:45:39 +00:00
2011-10-30 11:33:34 +00:00
for i = 1 , # event do
node = node.children [ event [ i ] ]
if not node then break end
2011-10-29 00:45:39 +00:00
table.remove ( params , 1 )
2011-10-30 11:57:21 +00:00
counter = counter + executeNodeCallbacks ( node , params )
2011-10-29 00:45:39 +00:00
end
2011-10-30 11:33:34 +00:00
return counter
2011-10-26 22:22:52 +00:00
end
2011-10-30 11:57:21 +00:00
local function addCallbackToNode ( self , node , callback )
2011-10-26 22:22:52 +00:00
local id = { }
2011-10-30 11:57:21 +00:00
node.callbacks [ id ] = callback
2011-10-28 23:24:10 +00:00
self._nodesById [ id ] = node
2011-10-26 22:22:52 +00:00
return id
end
2011-10-30 11:57:21 +00:00
local function removeCallbackFromNode ( node , id )
2011-10-28 23:39:19 +00:00
if not node then return false end
2011-10-30 11:57:21 +00:00
node.callbacks [ id ] = nil
2011-10-28 23:39:19 +00:00
return true
end
2011-10-24 07:06:29 +00:00
function beholder : reset ( )
2011-10-30 11:57:21 +00:00
self._root = { callbacks = { } , children = { } }
2011-10-28 23:24:10 +00:00
self._nodesById = setmetatable ( { } , { __mode = " k " } )
2011-10-24 07:06:29 +00:00
end
2011-10-29 00:45:39 +00:00
function beholder : observe ( ... )
2011-10-30 11:57:21 +00:00
local event , callback = extractEventAndCallbackFromParams ( { ... } )
return addCallbackToNode ( self , findOrCreateDescendantNode ( self._root , event ) , callback )
2011-10-24 07:06:29 +00:00
end
function beholder : stopObserving ( id )
2011-10-30 11:57:21 +00:00
return removeCallbackFromNode ( findNodeById ( self , id ) , id )
2011-10-24 07:06:29 +00:00
end
2011-10-29 00:45:39 +00:00
function beholder : trigger ( ... )
2011-10-30 11:33:34 +00:00
local event = { ... }
2011-10-30 11:57:21 +00:00
local counter = ( # event == 0 ) and executeAllCallbacks ( self._root ) or executeEventCallbacks ( self._root , event )
2011-10-30 11:33:34 +00:00
return counter > 0 and counter
2011-10-24 07:06:29 +00:00
end
2011-10-25 22:18:17 +00:00
beholder : reset ( )
2011-10-24 07:06:29 +00:00
return beholder