/
BinderGroupProvider.lua
95 lines (75 loc) · 2.23 KB
/
BinderGroupProvider.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
--[=[
Provides a basis for binderGroups that can be retrieved anywhere
@class BinderGroupProvider
]=]
local require = require(script.Parent.loader).load(script)
local Promise = require("Promise")
local BinderGroupProvider = {}
BinderGroupProvider.ClassName = "BinderGroupProvider"
BinderGroupProvider.ServiceName = "BinderGroupProvider"
BinderGroupProvider.__index = BinderGroupProvider
--[=[
Constructs a new BinderGroupProvider
@param initMethod (BinderGroupProvider) -> ()
@return BinderGroupProvider
]=]
function BinderGroupProvider.new(initMethod)
local self = setmetatable({}, BinderGroupProvider)
self._initMethod = initMethod or error("No initMethod")
self._groupsAddedPromise = Promise.new()
self._init = false
self._binderGroups = {}
return self
end
--[=[
Returns a promise that will resolve once groups are added.
@return Promise
]=]
function BinderGroupProvider:PromiseGroupsAdded()
return self._groupsAddedPromise
end
--[=[
Starts the binder provider. Should be called via ServiceBag.
@param ... ServiceBag | any
]=]
function BinderGroupProvider:Init(...)
assert(not self._init, "Already initialized")
self._initMethod(self, ...)
self._init = true
self._groupsAddedPromise:Resolve()
end
--[=[
Starts the binder provider. Should be called via ServiceBag.
]=]
function BinderGroupProvider:Start()
-- Do nothing
end
function BinderGroupProvider:__index(index)
if BinderGroupProvider[index] then
return BinderGroupProvider[index]
end
error(("%q Not a valid index"):format(tostring(index)))
end
--[=[
Returns a binder group given the binderName
@param groupName string
@return BinderGroup?
]=]
function BinderGroupProvider:Get(groupName)
assert(type(groupName) == "string", "groupName must be a string")
return rawget(self, groupName)
end
--[=[
Adds a new group at the given name
@param groupName string
@param binderGroup BinderGroup
]=]
function BinderGroupProvider:Add(groupName, binderGroup)
assert(type(groupName) == "string", "Bad groupName")
assert(type(binderGroup) == "table", "Bad binderGroup")
assert(not self._init, "Already initialized")
assert(not self:Get(groupName), "Duplicate groupName")
table.insert(self._binderGroups, binderGroup)
self[groupName] = binderGroup
end
return BinderGroupProvider