[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Obtaining references to a given object?
- From: Andrew Starks <andrew.starks@...>
- Date: Fri, 13 Sep 2013 22:58:02 -0500
On Fri, Sep 13, 2013 at 9:15 PM, Marc Lepage <mlepage@antimeta.com> wrote:
>
> So, in that context, any suggestions? Some way to automate the hook ups or the actual update of objects referring to objects when the referred objects change? (Or their properties?)
Here is a silly toy version of what I mean:
local _model = {}
local subs = {}
local model = setmetatable({},
{
__index = _model,
__newindex = function(t, i, v)
_model[i] = v
if subs[i] then
for i, cb in ipairs(subs[i]) do
local success, msg = cb(v)
--or local success, msg = coroutine.resume(cb, v)
if not success then return nil, msg end
end
if not v then
subs[i] = nil
end
elseif v then
subs[i] = {} --make new table for callbacks
end
end
})
model.foo = "bar"
for x = 1, 3 do
subs.foo[#subs.foo + 1] = function(v)
print("CB " .. x ..": Got:",v)
return true
end
end
model.foo = "baz"
model.foo = "dizzle"
model.foo = nil
assert(subs.foo == nil, "subs.foo should be gone.")