2 assert(LibStub
, "LibDataBroker-1.1 requires LibStub")
3 assert(LibStub
:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0")
5 local lib
, oldminor
= LibStub
:NewLibrary("LibDataBroker-1.1", 4)
6 if not lib
then return end
7 oldminor
= oldminor
or 0
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
16 __metatable
= "access denied",
17 __index
= function(self
, key
) return attributestorage
[self
] and attributestorage
[self
][key
] end,
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
)
36 function lib
:NewDataObject(name
, dataobj
)
37 if self
.proxystorage
[name
] then return end
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
47 dataobj
= setmetatable(dataobj
or {}, self
.domt
)
48 self
.proxystorage
[name
], self
.namestorage
[dataobj
] = dataobj
, name
49 self
.callbacks
:Fire("LibDataBroker_DataObjectCreated", name
, dataobj
)
55 function lib
:DataObjectIterator()
56 return pairs(self
.proxystorage
)
59 function lib
:GetDataObjectByName(dataobjectname
)
60 return self
.proxystorage
[dataobjectname
]
63 function lib
:GetNameByDataObject(dataobject
)
64 return self
.namestorage
[dataobject
]
69 local next = pairs(attributestorage
)
70 function lib
:pairs(dataobject_or_name
)
71 local t
= type(dataobject_or_name
)
72 assert(t
== "string" or t
== "table", "Usage: ldb:pairs('dataobjectname') or ldb:pairs(dataobject)")
74 local dataobj
= self
.proxystorage
[dataobject_or_name
] or dataobject_or_name
75 assert(attributestorage
[dataobj
], "Data object not found")
77 return next, attributestorage
[dataobj
], nil
80 local ipairs_iter
= ipairs(attributestorage
)
81 function lib
:ipairs(dataobject_or_name
)
82 local t
= type(dataobject_or_name
)
83 assert(t
== "string" or t
== "table", "Usage: ldb:ipairs('dataobjectname') or ldb:ipairs(dataobject)")
85 local dataobj
= self
.proxystorage
[dataobject_or_name
] or dataobject_or_name
86 assert(attributestorage
[dataobj
], "Data object not found")
88 return ipairs_iter
, attributestorage
[dataobj
], 0