diff --git a/beholder.lua b/beholder.lua index dc655b3..7693f98 100644 --- a/beholder.lua +++ b/beholder.lua @@ -18,7 +18,8 @@ local Node = { } function Node:new() - return setmetatable( { callbacks = {}, children = {} }, { __index = Node } ) + local node = { callbacks = {}, children = setmetatable({}, {__mode="k"}) } + return setmetatable( node, { __index = Node } ) end function Node:findById(id) diff --git a/spec/unit.lua b/spec/unit.lua index 31a4e26..9178df9 100644 --- a/spec/unit.lua +++ b/spec/unit.lua @@ -45,6 +45,17 @@ describe("Unit", function() it("throws an error if called without at least one parameter", function() assert_error(function() beholder:observe() end) end) + + it("does not store hard references to variables", function() + local counter = 0 + local x = {} + beholder:observe(x, function() counter = counter + 1 end) + beholder:triggerAll() + x = nil + collectgarbage("collect") + beholder:triggerAll() + assert_equal(1, counter) + end) end) describe(":stopObserving", function()