/
BoundChildCollection.lua
163 lines (132 loc) · 3.41 KB
/
BoundChildCollection.lua
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
--[=[
Tracks child of type of a binder.
@class BoundChildCollection
]=]
local require = require(script.Parent.loader).load(script)
local BaseObject = require("BaseObject")
local Signal = require("Signal")
local BoundChildCollection = setmetatable({}, BaseObject)
BoundChildCollection.ClassName = "BoundChildCollection"
BoundChildCollection.__index = BoundChildCollection
--[=[
Constructcs a new BoundChildCollection.
@param binder Binder<T>
@param parent Instance
@return BoundChildCollection<T>
]=]
function BoundChildCollection.new(binder, parent)
local self = setmetatable(BaseObject.new(), BoundChildCollection)
self._binder = binder or error("No binder")
self._parent = parent or error("No parent")
--[=[
Fires on class addition
@prop ClassAdded Signal<T>
@within BoundChildCollection
]=]
self.ClassAdded = Signal.new() -- :Fire(class)
self._maid:GiveTask(self.ClassAdded)
--[=[
Fires on class removal
@prop ClassRemoved Signal<T>
@within BoundChildCollection
]=]
self.ClassRemoved = Signal.new() -- :Fire(class)
self._maid:GiveTask(self.ClassRemoved)
self._classes = {} -- [class] = true
self._size = 0
self._maid:GiveTask(self._binder:GetClassAddedSignal():Connect(function(...)
self:_handleNewClassBound(...)
end))
self._maid:GiveTask(self._binder:GetClassRemovingSignal():Connect(function(class)
self:_removeClass(class)
end))
self:_startTracking()
return self
end
--[=[
Returns whether the track has the class
@param class T
@return boolean? -- true if the class exists, nil otherwise
]=]
function BoundChildCollection:HasClass(class)
return self._classes[class]
end
--[=[
Gets the size
@return number
]=]
function BoundChildCollection:GetSize()
return self._size
end
--[=[
Returns the raw classes variable as [class] = true.
:::warning
Do not modify the set
:::
@return { [T] = true } -- The set
]=]
function BoundChildCollection:GetSet()
return self._classes
end
--[=[
Slow than :GetSet(), but adds them in an ordered list
@return { T }
]=]
function BoundChildCollection:GetClasses()
local list = {}
for class, _ in pairs(self._classes) do
table.insert(list, class)
end
return list
end
function BoundChildCollection:_startTracking()
self._maid:GiveTask(self._parent.ChildAdded:Connect(function(child)
self:_addChild(child)
end))
self._maid:GiveTask(self._parent.ChildRemoved:Connect(function(child)
self:_removeChild(child)
end))
for _, child in pairs(self._parent:GetChildren()) do
-- Specifically do not fire on init because nothing is listening
self:_addChild(child, true)
end
end
function BoundChildCollection:_addChild(inst, doNotFire)
local class = self._binder:Get(inst)
if not class then
return
end
self:_addClass(class, doNotFire)
end
function BoundChildCollection:_handleNewClassBound(class, inst)
if inst.Parent ~= self._parent then
return
end
self:_addClass(class)
end
function BoundChildCollection:_removeChild(inst)
local class = self._binder:Get(inst)
if not class then
return
end
self:_removeClass(class)
end
function BoundChildCollection:_addClass(class, doNotFire)
if self._classes[class] then
return
end
self._classes[class] = true
self._size = self._size + 1
if not doNotFire then
self.ClassAdded:Fire(class)
end
end
function BoundChildCollection:_removeClass(class)
if not self._classes[class] then
return
end
self._classes[class] = nil
self._size = self._size - 1
self.ClassRemoved:Fire(class)
end
return BoundChildCollection