/
OverriddenProperty.lua
122 lines (92 loc) · 3.05 KB
/
OverriddenProperty.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
--[=[
Sets properties on the client and then replicates them to the server
@class OverriddenProperty
]=]
local require = require(script.Parent.loader).load(script)
local BaseObject = require("BaseObject")
local ThrottledFunction = require("ThrottledFunction")
local OverriddenProperty = setmetatable({}, BaseObject)
OverriddenProperty.ClassName = "OverriddenProperty"
OverriddenProperty.__index = OverriddenProperty
--[=[
Constructs a new OverriddenProperty.
@param robloxInstance Instance
@param propertyName string
@param replicateRate number? -- Optional replication rate and callback
@param replicateCallback (T)?
@return OverriddenProperty
]=]
function OverriddenProperty.new(robloxInstance, propertyName, replicateRate, replicateCallback)
local self = setmetatable(BaseObject.new(robloxInstance), OverriddenProperty)
assert(typeof(robloxInstance) == "Instance", "Bad robloxInstance")
assert(type(propertyName) == "string", "Bad propertyName")
self._disconnectCount = 0
self._propertyName = propertyName or error("No propertyName")
self._value = self._obj[self._propertyName]
if replicateRate ~= nil then
assert(type(replicateRate) == "number", "Bad replicateRate")
assert(type(replicateCallback) == "function", "Bad replicateCallback")
self._throttledExecuteReplicate = ThrottledFunction.new(replicateRate, replicateCallback)
self._maid:GiveTask(self._throttledExecuteReplicate)
end
self:_updateListenBinding()
return self
end
--[=[
Sets the property
@param value T
]=]
function OverriddenProperty:Set(value)
assert(typeof(value) == typeof(self._value), "Bad value")
self._value = value
self:_executeSet(true)
end
--[=[
Gets the property
@return T
]=]
function OverriddenProperty:Get()
return self._value
end
function OverriddenProperty:_executeSet(doReplicate)
self:_pushDisconnectChange()
self._obj[self._propertyName] = self._value
if doReplicate and self._throttledExecuteReplicate then
self._throttledExecuteReplicate:Call(self._value)
end
self:_popDisconnectChange()
end
function OverriddenProperty:_pushDisconnectChange()
self._disconnectCount = self._disconnectCount + 1
if self._disconnectCount >= 5 then
warn("[OverriddenProperty._pushDisconnectChange] - Disconnect count is somehow 5+")
end
self:_updateListenBinding()
end
function OverriddenProperty:_popDisconnectChange()
self._disconnectCount = self._disconnectCount - 1
if self._disconnectCount < 0 then
warn("[OverriddenProperty._pushDisconnectChange] - Disconnect count is somehow less than 0")
end
self:_updateListenBinding()
end
function OverriddenProperty:_updateListenBinding()
if self._disconnectCount == 0 then
self:_listenForChange()
else
self:_disconnectListenForChange()
end
end
function OverriddenProperty:_disconnectListenForChange()
self._maid._update = nil
end
function OverriddenProperty:_listenForChange()
if self._maid._update and self._maid._update.Connected then
return
end
self._maid._update = self._obj:GetPropertyChangedSignal(self._propertyName)
:Connect(function()
self:_executeSet(false)
end)
end
return OverriddenProperty