annotate Libs/LibDataBroker-1.1/LibDataBroker-1.1.lua @ 25:ac501d71c890 tip

Added tag v8.2.0.024 for changeset 389dcaeebc47
author Tercioo
date Fri, 28 Jun 2019 20:07:27 -0300
parents 9ad7f3c634f1
children
rev   line source
tercio@0 1
tercio@0 2 assert(LibStub, "LibDataBroker-1.1 requires LibStub")
tercio@0 3 assert(LibStub:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0")
tercio@0 4
Tercio@20 5 local lib, oldminor = LibStub:NewLibrary("LibDataBroker-1.1", 4)
tercio@0 6 if not lib then return end
tercio@0 7 oldminor = oldminor or 0
tercio@0 8
tercio@0 9
tercio@0 10 lib.callbacks = lib.callbacks or LibStub:GetLibrary("CallbackHandler-1.0"):New(lib)
tercio@0 11 lib.attributestorage, lib.namestorage, lib.proxystorage = lib.attributestorage or {}, lib.namestorage or {}, lib.proxystorage or {}
tercio@0 12 local attributestorage, namestorage, callbacks = lib.attributestorage, lib.namestorage, lib.callbacks
tercio@0 13
tercio@0 14 if oldminor < 2 then
tercio@0 15 lib.domt = {
tercio@0 16 __metatable = "access denied",
tercio@0 17 __index = function(self, key) return attributestorage[self] and attributestorage[self][key] end,
tercio@0 18 }
tercio@0 19 end
tercio@0 20
tercio@0 21 if oldminor < 3 then
tercio@0 22 lib.domt.__newindex = function(self, key, value)
tercio@0 23 if not attributestorage[self] then attributestorage[self] = {} end
tercio@0 24 if attributestorage[self][key] == value then return end
tercio@0 25 attributestorage[self][key] = value
tercio@0 26 local name = namestorage[self]
tercio@0 27 if not name then return end
tercio@0 28 callbacks:Fire("LibDataBroker_AttributeChanged", name, key, value, self)
tercio@0 29 callbacks:Fire("LibDataBroker_AttributeChanged_"..name, name, key, value, self)
tercio@0 30 callbacks:Fire("LibDataBroker_AttributeChanged_"..name.."_"..key, name, key, value, self)
tercio@0 31 callbacks:Fire("LibDataBroker_AttributeChanged__"..key, name, key, value, self)
tercio@0 32 end
tercio@0 33 end
tercio@0 34
tercio@0 35 if oldminor < 2 then
tercio@0 36 function lib:NewDataObject(name, dataobj)
tercio@0 37 if self.proxystorage[name] then return end
tercio@0 38
tercio@0 39 if dataobj then
tercio@0 40 assert(type(dataobj) == "table", "Invalid dataobj, must be nil or a table")
tercio@0 41 self.attributestorage[dataobj] = {}
tercio@0 42 for i,v in pairs(dataobj) do
tercio@0 43 self.attributestorage[dataobj][i] = v
tercio@0 44 dataobj[i] = nil
tercio@0 45 end
tercio@0 46 end
tercio@0 47 dataobj = setmetatable(dataobj or {}, self.domt)
tercio@0 48 self.proxystorage[name], self.namestorage[dataobj] = dataobj, name
tercio@0 49 self.callbacks:Fire("LibDataBroker_DataObjectCreated", name, dataobj)
tercio@0 50 return dataobj
tercio@0 51 end
tercio@0 52 end
tercio@0 53
tercio@0 54 if oldminor < 1 then
tercio@0 55 function lib:DataObjectIterator()
tercio@0 56 return pairs(self.proxystorage)
tercio@0 57 end
tercio@0 58
tercio@0 59 function lib:GetDataObjectByName(dataobjectname)
tercio@0 60 return self.proxystorage[dataobjectname]
tercio@0 61 end
tercio@0 62
tercio@0 63 function lib:GetNameByDataObject(dataobject)
tercio@0 64 return self.namestorage[dataobject]
tercio@0 65 end
tercio@0 66 end
Tercio@20 67
Tercio@20 68 if oldminor < 4 then
Tercio@20 69 local next = pairs(attributestorage)
Tercio@20 70 function lib:pairs(dataobject_or_name)
Tercio@20 71 local t = type(dataobject_or_name)
Tercio@20 72 assert(t == "string" or t == "table", "Usage: ldb:pairs('dataobjectname') or ldb:pairs(dataobject)")
Tercio@20 73
Tercio@20 74 local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name
Tercio@20 75 assert(attributestorage[dataobj], "Data object not found")
Tercio@20 76
Tercio@20 77 return next, attributestorage[dataobj], nil
Tercio@20 78 end
Tercio@20 79
Tercio@20 80 local ipairs_iter = ipairs(attributestorage)
Tercio@20 81 function lib:ipairs(dataobject_or_name)
Tercio@20 82 local t = type(dataobject_or_name)
Tercio@20 83 assert(t == "string" or t == "table", "Usage: ldb:ipairs('dataobjectname') or ldb:ipairs(dataobject)")
Tercio@20 84
Tercio@20 85 local dataobj = self.proxystorage[dataobject_or_name] or dataobject_or_name
Tercio@20 86 assert(attributestorage[dataobj], "Data object not found")
Tercio@20 87
Tercio@20 88 return ipairs_iter, attributestorage[dataobj], 0
Tercio@20 89 end
Tercio@20 90 end