|
| 1 | + |
| 2 | +assert(LibStub, "LibDataBroker-1.1 requires LibStub") |
| 3 | +assert(LibStub:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0") |
| 4 | + |
| 5 | +local lib, oldminor = LibStub:NewLibrary("LibDataBroker-1.1", 3) |
| 6 | +if not lib then return end |
| 7 | +oldminor = oldminor or 0 |
| 8 | + |
| 9 | + |
| 10 | +lib.callbacks = lib.callbacks or LibStub:GetLibrary("CallbackHandler-1.0"):New(lib) |
| 11 | +lib.attributestorage, lib.namestorage, lib.proxystorage = lib.attributestorage or {}, lib.namestorage or {}, lib.proxystorage or {} |
| 12 | +local attributestorage, namestorage, callbacks = lib.attributestorage, lib.namestorage, lib.callbacks |
| 13 | + |
| 14 | +if oldminor < 2 then |
| 15 | + lib.domt = { |
| 16 | + __metatable = "access denied", |
| 17 | + __index = function(self, key) return attributestorage[self] and attributestorage[self][key] end, |
| 18 | + } |
| 19 | +end |
| 20 | + |
| 21 | +if oldminor < 3 then |
| 22 | + lib.domt.__newindex = function(self, key, value) |
| 23 | + if not attributestorage[self] then attributestorage[self] = {} end |
| 24 | + if attributestorage[self][key] == value then return end |
| 25 | + attributestorage[self][key] = value |
| 26 | + local name = namestorage[self] |
| 27 | + if not name then return end |
| 28 | + callbacks:Fire("LibDataBroker_AttributeChanged", name, key, value, self) |
| 29 | + callbacks:Fire("LibDataBroker_AttributeChanged_"..name, name, key, value, self) |
| 30 | + callbacks:Fire("LibDataBroker_AttributeChanged_"..name.."_"..key, name, key, value, self) |
| 31 | + callbacks:Fire("LibDataBroker_AttributeChanged__"..key, name, key, value, self) |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +if oldminor < 2 then |
| 36 | + function lib:NewDataObject(name, dataobj) |
| 37 | + if self.proxystorage[name] then return end |
| 38 | + |
| 39 | + if dataobj then |
| 40 | + assert(type(dataobj) == "table", "Invalid dataobj, must be nil or a table") |
| 41 | + self.attributestorage[dataobj] = {} |
| 42 | + for i,v in pairs(dataobj) do |
| 43 | + self.attributestorage[dataobj][i] = v |
| 44 | + dataobj[i] = nil |
| 45 | + end |
| 46 | + end |
| 47 | + dataobj = setmetatable(dataobj or {}, self.domt) |
| 48 | + self.proxystorage[name], self.namestorage[dataobj] = dataobj, name |
| 49 | + self.callbacks:Fire("LibDataBroker_DataObjectCreated", name, dataobj) |
| 50 | + return dataobj |
| 51 | + end |
| 52 | +end |
| 53 | + |
| 54 | +if oldminor < 1 then |
| 55 | + function lib:DataObjectIterator() |
| 56 | + return pairs(self.proxystorage) |
| 57 | + end |
| 58 | + |
| 59 | + function lib:GetDataObjectByName(dataobjectname) |
| 60 | + return self.proxystorage[dataobjectname] |
| 61 | + end |
| 62 | + |
| 63 | + function lib:GetNameByDataObject(dataobject) |
| 64 | + return self.namestorage[dataobject] |
| 65 | + end |
| 66 | +end |
0 commit comments