mirror of
https://github.com/kikito/beholder.lua.git
synced 2024-12-16 00:34:21 +00:00
added self-checks
This commit is contained in:
parent
1565149a8b
commit
46377b7ca3
25
beholder.lua
25
beholder.lua
@ -7,6 +7,15 @@
|
||||
|
||||
local beholder = {}
|
||||
|
||||
local function initialize(self)
|
||||
self._root = { callbacks={}, children={} }
|
||||
self._nodesById = setmetatable({}, {__mode="k"})
|
||||
end
|
||||
|
||||
local function checkSelf(self, methodName)
|
||||
assert(type(self)=="table" and self._root and self._nodesById, "Use beholder:" .. methodName .. " instead of beholder." .. methodName)
|
||||
end
|
||||
|
||||
local function falseIfZero(n)
|
||||
return n > 0 and n
|
||||
end
|
||||
@ -83,28 +92,34 @@ local function removeCallbackFromNode(node, id)
|
||||
return true
|
||||
end
|
||||
|
||||
function beholder:reset()
|
||||
self._root = { callbacks={}, children={} }
|
||||
self._nodesById = setmetatable({}, {__mode="k"})
|
||||
end
|
||||
-------
|
||||
|
||||
function beholder:observe(...)
|
||||
checkSelf(self, 'observe')
|
||||
local event, callback = extractEventAndCallbackFromParams({...})
|
||||
return addCallbackToNode(self, findOrCreateDescendantNode(self._root, event), callback)
|
||||
end
|
||||
|
||||
function beholder:stopObserving(id)
|
||||
checkSelf(self, 'stopObserving')
|
||||
return removeCallbackFromNode(findNodeById(self, id), id)
|
||||
end
|
||||
|
||||
function beholder:trigger(...)
|
||||
checkSelf(self, 'trigger')
|
||||
return falseIfZero( executeEventCallbacks(self._root, {...}) )
|
||||
end
|
||||
|
||||
function beholder:triggerAll(...)
|
||||
checkSelf(self, 'triggerAll')
|
||||
return falseIfZero( executeAllCallbacks(self._root, {...}) )
|
||||
end
|
||||
|
||||
beholder:reset()
|
||||
function beholder:reset()
|
||||
checkSelf(self, 'reset')
|
||||
initialize(self)
|
||||
end
|
||||
|
||||
initialize(beholder)
|
||||
|
||||
return beholder
|
||||
|
@ -7,6 +7,10 @@ describe("Unit", function()
|
||||
end)
|
||||
|
||||
describe(":observe", function()
|
||||
it("checks self", function()
|
||||
assert_error(function() beholder.observe("X", function() end) end)
|
||||
end)
|
||||
|
||||
it("notices simple events so that trigger works", function()
|
||||
local counter = 0
|
||||
beholder:observe("EVENT", function() counter = counter + 1 end)
|
||||
@ -44,6 +48,9 @@ describe("Unit", function()
|
||||
end)
|
||||
|
||||
describe(":stopObserving", function()
|
||||
it("checks self", function()
|
||||
assert_error(function() beholder.stopObserving() end)
|
||||
end)
|
||||
it("stops noticing events so trigger doesn't work any more", function()
|
||||
local counter = 0
|
||||
local id = beholder:observe("EVENT", function() counter = counter + 1 end)
|
||||
@ -97,6 +104,9 @@ describe("Unit", function()
|
||||
end)
|
||||
|
||||
describe(":trigger", function()
|
||||
it("checks self", function()
|
||||
assert_error(function() beholder.trigger() end)
|
||||
end)
|
||||
it("does not error on random stuff", function()
|
||||
assert_not_error(function() beholder:trigger("FOO") end)
|
||||
end)
|
||||
@ -128,6 +138,9 @@ describe("Unit", function()
|
||||
end)
|
||||
|
||||
describe(":triggerAll", function()
|
||||
it("checks self", function()
|
||||
assert_error(function() beholder.triggerAll() end)
|
||||
end)
|
||||
it("calls all registered callbacks", function()
|
||||
local counter = 0
|
||||
beholder:observe("X", function() counter = counter + 1 end)
|
||||
@ -148,7 +161,12 @@ describe("Unit", function()
|
||||
beholder:observe("Y", function() end)
|
||||
assert_equal(2, beholder:triggerAll())
|
||||
end)
|
||||
end)
|
||||
|
||||
describe(":reset", function()
|
||||
it("checks self", function()
|
||||
assert_error(function() beholder.reset() end)
|
||||
end)
|
||||
end)
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user