public
Description: WoW Powerleveling addon
Homepage: http://www.tekkub.net/
Clone URL: git://github.com/tekkub/tourguide.git
Click here to lend your support to: tourguide and make a donation at www.pledgie.com !
tourguide / LibDataBroker-1.1 / LibDataBroker-1.1.lua
100644 67 lines (55 sloc) 2.276 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
assert(LibStub, "LibDataBroker-1.1 requires LibStub")
assert(LibStub:GetLibrary("CallbackHandler-1.0", true), "LibDataBroker-1.1 requires CallbackHandler-1.0")
 
local lib, oldminor = LibStub:NewLibrary("LibDataBroker-1.1", 3)
if not lib then return end
oldminor = oldminor or 0
 
 
lib.callbacks = lib.callbacks or LibStub:GetLibrary("CallbackHandler-1.0"):New(lib)
lib.attributestorage, lib.namestorage, lib.proxystorage = lib.attributestorage or {}, lib.namestorage or {}, lib.proxystorage or {}
local attributestorage, namestorage, callbacks = lib.attributestorage, lib.namestorage, lib.callbacks
 
if oldminor < 2 then
  lib.domt = {
    __metatable = "access denied",
    __index = function(self, key) return attributestorage[self] and attributestorage[self][key] end,
  }
end
 
if oldminor < 3 then
  lib.domt.__newindex = function(self, key, value)
    if not attributestorage[self] then attributestorage[self] = {} end
    if attributestorage[self][key] == value then return end
    attributestorage[self][key] = value
    local name = namestorage[self]
    if not name then return end
    callbacks:Fire("LibDataBroker_AttributeChanged", name, key, value, self)
    callbacks:Fire("LibDataBroker_AttributeChanged_"..name, name, key, value, self)
    callbacks:Fire("LibDataBroker_AttributeChanged_"..name.."_"..key, name, key, value, self)
    callbacks:Fire("LibDataBroker_AttributeChanged__"..key, name, key, value, self)
  end
end
 
if oldminor < 2 then
  function lib:NewDataObject(name, dataobj)
    if self.proxystorage[name] then return end
 
    if dataobj then
      assert(type(dataobj) == "table", "Invalid dataobj, must be nil or a table")
      self.attributestorage[dataobj] = {}
      for i,v in pairs(dataobj) do
        self.attributestorage[dataobj][i] = v
        dataobj[i] = nil
      end
    end
    dataobj = setmetatable(dataobj or {}, self.domt)
    self.proxystorage[name], self.namestorage[dataobj] = dataobj, name
    self.callbacks:Fire("LibDataBroker_DataObjectCreated", name, dataobj)
    return dataobj
  end
end
 
if oldminor < 1 then
  function lib:DataObjectIterator()
    return pairs(self.proxystorage)
  end
 
  function lib:GetDataObjectByName(dataobjectname)
    return self.proxystorage[dataobjectname]
  end
 
  function lib:GetNameByDataObject(dataobject)
    return self.namestorage[dataobject]
  end
end