flickerstreak@104: flickerstreak@104: assert(LibStub, "LibDataBroker-1.1 requires LibStub") flickerstreak@104: assert(LibStub:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0") flickerstreak@104: flickerstreak@104: local lib, oldminor = LibStub:NewLibrary("LibDataBroker-1.1", 4) flickerstreak@104: if not lib then return end flickerstreak@104: oldminor = oldminor or 0 flickerstreak@104: flickerstreak@104: flickerstreak@104: lib.callbacks = lib.callbacks or LibStub:GetLibrary("CallbackHandler-1.0"):New(lib) flickerstreak@104: lib.attributestorage, lib.namestorage, lib.proxystorage = lib.attributestorage or {}, lib.namestorage or {}, lib.proxystorage or {} flickerstreak@104: local attributestorage, namestorage, callbacks = lib.attributestorage, lib.namestorage, lib.callbacks flickerstreak@104: flickerstreak@104: if oldminor < 2 then flickerstreak@104: lib.domt = { flickerstreak@104: __metatable = "access denied", flickerstreak@104: __index = function(self, key) return attributestorage[self] and attributestorage[self][key] end, flickerstreak@104: } flickerstreak@104: end flickerstreak@104: flickerstreak@104: if oldminor < 3 then flickerstreak@104: lib.domt.__newindex = function(self, key, value) flickerstreak@104: if not attributestorage[self] then attributestorage[self] = {} end flickerstreak@104: if attributestorage[self][key] == value then return end flickerstreak@104: attributestorage[self][key] = value flickerstreak@104: local name = namestorage[self] flickerstreak@104: if not name then return end flickerstreak@104: callbacks:Fire("LibDataBroker_AttributeChanged", name, key, value, self) flickerstreak@104: callbacks:Fire("LibDataBroker_AttributeChanged_"..name, name, key, value, self) flickerstreak@104: callbacks:Fire("LibDataBroker_AttributeChanged_"..name.."_"..key, name, key, value, self) flickerstreak@104: callbacks:Fire("LibDataBroker_AttributeChanged__"..key, name, key, value, self) flickerstreak@104: end flickerstreak@104: end flickerstreak@104: flickerstreak@104: if oldminor < 2 then flickerstreak@104: function lib:NewDataObject(name, dataobj) flickerstreak@104: if self.proxystorage[name] then return end flickerstreak@104: flickerstreak@104: if dataobj then flickerstreak@104: assert(type(dataobj) == "table", "Invalid dataobj, must be nil or a table") flickerstreak@104: self.attributestorage[dataobj] = {} flickerstreak@104: for i,v in pairs(dataobj) do flickerstreak@104: self.attributestorage[dataobj][i] = v flickerstreak@104: dataobj[i] = nil flickerstreak@104: end flickerstreak@104: end flickerstreak@104: dataobj = setmetatable(dataobj or {}, self.domt) flickerstreak@104: self.proxystorage[name], self.namestorage[dataobj] = dataobj, name flickerstreak@104: self.callbacks:Fire("LibDataBroker_DataObjectCreated", name, dataobj) flickerstreak@104: return dataobj flickerstreak@104: end flickerstreak@104: end flickerstreak@104: flickerstreak@104: if oldminor < 1 then flickerstreak@104: function lib:DataObjectIterator() flickerstreak@104: return pairs(self.proxystorage) flickerstreak@104: end flickerstreak@104: flickerstreak@104: function lib:GetDataObjectByName(dataobjectname) flickerstreak@104: return self.proxystorage[dataobjectname] flickerstreak@104: end flickerstreak@104: flickerstreak@104: function lib:GetNameByDataObject(dataobject) flickerstreak@104: return self.namestorage[dataobject] flickerstreak@104: end flickerstreak@104: end flickerstreak@104: flickerstreak@104: if oldminor < 4 then flickerstreak@104: local next = pairs(attributestorage) flickerstreak@104: function lib:pairs(dataobject_or_name) flickerstreak@104: local t = type(dataobject_or_name) flickerstreak@104: assert(t == "string" or t == "table", "Usage: ldb:pairs('dataobjectname') or ldb:pairs(dataobject)") flickerstreak@104: flickerstreak@104: local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name flickerstreak@104: assert(attributestorage[dataobj], "Data object not found") flickerstreak@104: flickerstreak@104: return next, attributestorage[dataobj], nil flickerstreak@104: end flickerstreak@104: flickerstreak@104: local ipairs_iter = ipairs(attributestorage) flickerstreak@104: function lib:ipairs(dataobject_or_name) flickerstreak@104: local t = type(dataobject_or_name) flickerstreak@104: assert(t == "string" or t == "table", "Usage: ldb:ipairs('dataobjectname') or ldb:ipairs(dataobject)") flickerstreak@104: flickerstreak@104: local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name flickerstreak@104: assert(attributestorage[dataobj], "Data object not found") flickerstreak@104: flickerstreak@104: return ipairs_iter, attributestorage[dataobj], 0 flickerstreak@104: end flickerstreak@104: end