@quenty/attributeutils 14.17.0 → 14.17.1-canary.545.2374fb2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/package.json +10 -10
- package/src/Shared/AttributeUtils.lua +29 -27
- package/src/Shared/AttributeValue.lua +36 -14
- package/src/Shared/RxAttributeUtils.lua +18 -12
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,17 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [14.17.1-canary.545.2374fb2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/attributeutils@14.17.0...@quenty/attributeutils@14.17.1-canary.545.2374fb2.0) (2025-04-05)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Bug Fixes
|
|
10
|
+
|
|
11
|
+
* Add types to packages ([2374fb2](https://github.com/Quenty/NevermoreEngine/commit/2374fb2b043cfbe0e9b507b3316eec46a4e353a0))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [14.17.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/attributeutils@14.16.2...@quenty/attributeutils@14.17.0) (2025-04-02)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @quenty/attributeutils
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/attributeutils",
|
|
3
|
-
"version": "14.17.0",
|
|
3
|
+
"version": "14.17.1-canary.545.2374fb2.0",
|
|
4
4
|
"description": "Provides utility functions to work with attributes in Roblox",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -25,17 +25,17 @@
|
|
|
25
25
|
"Quenty"
|
|
26
26
|
],
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@quenty/brio": "
|
|
29
|
-
"@quenty/canceltoken": "
|
|
30
|
-
"@quenty/loader": "
|
|
31
|
-
"@quenty/maid": "
|
|
32
|
-
"@quenty/promise": "
|
|
33
|
-
"@quenty/rx": "
|
|
34
|
-
"@quenty/rxsignal": "
|
|
35
|
-
"@quenty/symbol": "
|
|
28
|
+
"@quenty/brio": "14.17.1-canary.545.2374fb2.0",
|
|
29
|
+
"@quenty/canceltoken": "11.11.2-canary.545.2374fb2.0",
|
|
30
|
+
"@quenty/loader": "10.8.1-canary.545.2374fb2.0",
|
|
31
|
+
"@quenty/maid": "3.4.1-canary.545.2374fb2.0",
|
|
32
|
+
"@quenty/promise": "10.10.2-canary.545.2374fb2.0",
|
|
33
|
+
"@quenty/rx": "13.17.1-canary.545.2374fb2.0",
|
|
34
|
+
"@quenty/rxsignal": "7.17.1-canary.545.2374fb2.0",
|
|
35
|
+
"@quenty/symbol": "3.4.1-canary.545.2374fb2.0"
|
|
36
36
|
},
|
|
37
37
|
"publishConfig": {
|
|
38
38
|
"access": "public"
|
|
39
39
|
},
|
|
40
|
-
"gitHead": "
|
|
40
|
+
"gitHead": "2374fb2b043cfbe0e9b507b3316eec46a4e353a0"
|
|
41
41
|
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
--!strict
|
|
1
2
|
--[=[
|
|
2
3
|
Provides utility functions to work with attributes in Roblox
|
|
3
4
|
@class AttributeUtils
|
|
@@ -17,26 +18,28 @@ end
|
|
|
17
18
|
|
|
18
19
|
local AttributeUtils = {}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
["
|
|
24
|
-
["
|
|
25
|
-
["
|
|
26
|
-
["
|
|
27
|
-
["
|
|
28
|
-
["
|
|
29
|
-
["
|
|
30
|
-
["
|
|
31
|
-
["
|
|
32
|
-
["
|
|
33
|
-
["
|
|
34
|
-
["
|
|
35
|
-
["
|
|
36
|
-
["
|
|
37
|
-
["
|
|
38
|
-
["
|
|
39
|
-
|
|
21
|
+
type ValidAttributeMap = { [string]: true }
|
|
22
|
+
|
|
23
|
+
local VALID_ATTRIBUTE_TYPES: ValidAttributeMap = table.freeze({
|
|
24
|
+
["nil"] = true,
|
|
25
|
+
["string"] = true,
|
|
26
|
+
["boolean"] = true,
|
|
27
|
+
["number"] = true,
|
|
28
|
+
["UDim"] = true,
|
|
29
|
+
["UDim2"] = true,
|
|
30
|
+
["BrickColor"] = true,
|
|
31
|
+
["CFrame"] = true,
|
|
32
|
+
["Color3"] = true,
|
|
33
|
+
["Vector2"] = true,
|
|
34
|
+
["Vector3"] = true,
|
|
35
|
+
["NumberSequence"] = true,
|
|
36
|
+
["ColorSequence"] = true,
|
|
37
|
+
["IntValue"] = true,
|
|
38
|
+
["NumberRange"] = true,
|
|
39
|
+
["Rect"] = true,
|
|
40
|
+
["Font"] = true,
|
|
41
|
+
["EnumItem"] = true,
|
|
42
|
+
} :: ValidAttributeMap)
|
|
40
43
|
|
|
41
44
|
--[=[
|
|
42
45
|
Returns whether the attribute is a valid type or not for an attribute.
|
|
@@ -48,7 +51,7 @@ local VALID_ATTRIBUTE_TYPES = {
|
|
|
48
51
|
@param valueType string
|
|
49
52
|
@return boolean
|
|
50
53
|
]=]
|
|
51
|
-
function AttributeUtils.isValidAttributeType(valueType)
|
|
54
|
+
function AttributeUtils.isValidAttributeType(valueType: string): boolean
|
|
52
55
|
return VALID_ATTRIBUTE_TYPES[valueType] == true
|
|
53
56
|
end
|
|
54
57
|
|
|
@@ -61,7 +64,7 @@ end
|
|
|
61
64
|
@param cancelToken CancelToken
|
|
62
65
|
@return Promise<any>
|
|
63
66
|
]=]
|
|
64
|
-
function AttributeUtils.promiseAttribute(instance, attributeName, predicate, cancelToken)
|
|
67
|
+
function AttributeUtils.promiseAttribute(instance: Instance, attributeName: string, predicate, cancelToken)
|
|
65
68
|
assert(typeof(instance) == "Instance", "Bad instance")
|
|
66
69
|
assert(type(attributeName) == "string", "Bad attributeName")
|
|
67
70
|
assert(CancelToken.isCancelToken(cancelToken) or cancelToken == nil, "Bad cancelToken")
|
|
@@ -99,7 +102,6 @@ function AttributeUtils.promiseAttribute(instance, attributeName, predicate, can
|
|
|
99
102
|
return promise
|
|
100
103
|
end
|
|
101
104
|
|
|
102
|
-
|
|
103
105
|
--[=[
|
|
104
106
|
Whenever the attribute is true, the binder will be bound, and when the
|
|
105
107
|
binder is bound, the attribute will be true.
|
|
@@ -109,7 +111,7 @@ end
|
|
|
109
111
|
@param binder Binder<T>
|
|
110
112
|
@return Maid
|
|
111
113
|
]=]
|
|
112
|
-
function AttributeUtils.bindToBinder(instance, attributeName, binder)
|
|
114
|
+
function AttributeUtils.bindToBinder(instance: Instance, attributeName: string, binder): Maid.Maid
|
|
113
115
|
assert(binder, "Bad binder")
|
|
114
116
|
assert(typeof(instance) == "Instance", "Bad instance")
|
|
115
117
|
assert(type(attributeName) == "string", "Bad attributeName")
|
|
@@ -174,7 +176,7 @@ end
|
|
|
174
176
|
@param default any
|
|
175
177
|
@return any? -- The value of the attribute
|
|
176
178
|
]=]
|
|
177
|
-
function AttributeUtils.initAttribute(instance, attributeName, default)
|
|
179
|
+
function AttributeUtils.initAttribute(instance: Instance, attributeName: string, default: any): any
|
|
178
180
|
assert(typeof(instance) == "Instance", "Bad instance")
|
|
179
181
|
assert(typeof(attributeName) == "string", "Bad attributeName")
|
|
180
182
|
|
|
@@ -194,7 +196,7 @@ end
|
|
|
194
196
|
@param default T?
|
|
195
197
|
@return T?
|
|
196
198
|
]=]
|
|
197
|
-
function AttributeUtils.getAttribute(instance, attributeName, default)
|
|
199
|
+
function AttributeUtils.getAttribute(instance: Instance, attributeName: string, default: any): any
|
|
198
200
|
local value = instance:GetAttribute(attributeName)
|
|
199
201
|
if value == nil then
|
|
200
202
|
return default
|
|
@@ -211,7 +213,7 @@ end
|
|
|
211
213
|
function AttributeUtils.removeAllAttributes(instance: Instance)
|
|
212
214
|
assert(typeof(instance) == "Instance", "Bad instance")
|
|
213
215
|
|
|
214
|
-
for key, _ in
|
|
216
|
+
for key, _ in instance:GetAttributes() do
|
|
215
217
|
instance:SetAttribute(key, nil)
|
|
216
218
|
end
|
|
217
219
|
end
|
|
@@ -20,11 +20,28 @@
|
|
|
20
20
|
local require = require(script.Parent.loader).load(script)
|
|
21
21
|
|
|
22
22
|
local RxAttributeUtils = require("RxAttributeUtils")
|
|
23
|
+
local _Observable = require("Observable")
|
|
24
|
+
local _Brio = require("Brio")
|
|
25
|
+
local _Rx = require("Rx")
|
|
23
26
|
|
|
24
27
|
local AttributeValue = {}
|
|
25
28
|
AttributeValue.ClassName = "AttributeValue"
|
|
26
29
|
AttributeValue.__index = AttributeValue
|
|
27
30
|
|
|
31
|
+
export type AttributeValue<T> = typeof(setmetatable(
|
|
32
|
+
{} :: {
|
|
33
|
+
_object: Instance,
|
|
34
|
+
_attributeName: string,
|
|
35
|
+
_defaultValue: T?,
|
|
36
|
+
|
|
37
|
+
-- Public
|
|
38
|
+
Value: T?,
|
|
39
|
+
AttributeName: string,
|
|
40
|
+
Changed: RBXScriptSignal<(T)>,
|
|
41
|
+
},
|
|
42
|
+
AttributeValue
|
|
43
|
+
))
|
|
44
|
+
|
|
28
45
|
--[=[
|
|
29
46
|
Constructs a new AttributeValue. If a defaultValue that is not nil
|
|
30
47
|
is defined, then this value will be set on the Roblox object.
|
|
@@ -34,21 +51,21 @@ AttributeValue.__index = AttributeValue
|
|
|
34
51
|
@param defaultValue T?
|
|
35
52
|
@return AttributeValue<T>
|
|
36
53
|
]=]
|
|
37
|
-
function AttributeValue.new(object, attributeName, defaultValue)
|
|
54
|
+
function AttributeValue.new<T>(object: Instance, attributeName: string, defaultValue: any?): AttributeValue<T>
|
|
38
55
|
assert(typeof(object) == "Instance", "Bad object")
|
|
39
56
|
assert(type(attributeName) == "string", "Bad attributeName")
|
|
40
57
|
|
|
41
58
|
local self = {
|
|
42
|
-
_object = object
|
|
43
|
-
_attributeName = attributeName
|
|
44
|
-
_defaultValue = defaultValue
|
|
59
|
+
_object = object,
|
|
60
|
+
_attributeName = attributeName,
|
|
61
|
+
_defaultValue = defaultValue,
|
|
45
62
|
}
|
|
46
63
|
|
|
47
64
|
if defaultValue ~= nil and self._object:GetAttribute(self._attributeName) == nil then
|
|
48
65
|
self._object:SetAttribute(rawget(self, "_attributeName"), defaultValue)
|
|
49
66
|
end
|
|
50
67
|
|
|
51
|
-
return setmetatable(self, AttributeValue)
|
|
68
|
+
return setmetatable(self, AttributeValue) :: any
|
|
52
69
|
end
|
|
53
70
|
|
|
54
71
|
--[=[
|
|
@@ -57,7 +74,10 @@ end
|
|
|
57
74
|
@param condition function | nil
|
|
58
75
|
@return Observable<Brio<any>>
|
|
59
76
|
]=]
|
|
60
|
-
function AttributeValue
|
|
77
|
+
function AttributeValue.ObserveBrio<T>(
|
|
78
|
+
self: AttributeValue<T>,
|
|
79
|
+
condition: _Rx.Predicate<T>?
|
|
80
|
+
): _Observable.Observable<_Brio.Brio<any>>
|
|
61
81
|
return RxAttributeUtils.observeAttributeBrio(self._object, self._attributeName, condition)
|
|
62
82
|
end
|
|
63
83
|
|
|
@@ -65,8 +85,8 @@ end
|
|
|
65
85
|
Observes an attribute on an instance.
|
|
66
86
|
@return Observable<any>
|
|
67
87
|
]=]
|
|
68
|
-
function AttributeValue
|
|
69
|
-
return RxAttributeUtils.observeAttribute(self._object, self._attributeName, rawget(self, "_defaultValue"))
|
|
88
|
+
function AttributeValue.Observe<T>(self: AttributeValue<T>): _Observable.Observable<T?>
|
|
89
|
+
return RxAttributeUtils.observeAttribute(self._object, self._attributeName, rawget(self :: any, "_defaultValue"))
|
|
70
90
|
end
|
|
71
91
|
|
|
72
92
|
--[=[
|
|
@@ -82,12 +102,12 @@ end
|
|
|
82
102
|
@prop Changed Signal<()>
|
|
83
103
|
@within AttributeValue
|
|
84
104
|
]=]
|
|
85
|
-
function AttributeValue
|
|
105
|
+
function AttributeValue.__index<T>(self: AttributeValue<T>, index)
|
|
86
106
|
if AttributeValue[index] then
|
|
87
107
|
return AttributeValue[index]
|
|
88
108
|
elseif index == "Value" then
|
|
89
|
-
local result = self._object:GetAttribute(rawget(self, "_attributeName"))
|
|
90
|
-
local default = rawget(self, "_defaultValue")
|
|
109
|
+
local result = self._object:GetAttribute(rawget(self :: any, "_attributeName"))
|
|
110
|
+
local default = rawget(self :: any, "_defaultValue")
|
|
91
111
|
if result == nil then
|
|
92
112
|
return default
|
|
93
113
|
else
|
|
@@ -96,15 +116,17 @@ function AttributeValue:__index(index)
|
|
|
96
116
|
elseif index == "Changed" then
|
|
97
117
|
return self._object:GetAttributeChangedSignal(self._attributeName)
|
|
98
118
|
elseif index == "AttributeName" then
|
|
99
|
-
return rawget(self, "_attributeName")
|
|
119
|
+
return rawget(self :: any, "_attributeName")
|
|
100
120
|
else
|
|
101
121
|
error(string.format("%q is not a member of AttributeValue", tostring(index)))
|
|
102
122
|
end
|
|
103
123
|
end
|
|
104
124
|
|
|
105
|
-
function AttributeValue
|
|
125
|
+
function AttributeValue.__newindex<T>(self: AttributeValue<T>, index, value)
|
|
106
126
|
if index == "Value" then
|
|
107
|
-
self._object:SetAttribute(rawget(self, "_attributeName"), value)
|
|
127
|
+
self._object:SetAttribute(rawget(self :: any, "_attributeName"), value)
|
|
128
|
+
elseif index == "AttributeName" then
|
|
129
|
+
error("Cannot set AttributeName")
|
|
108
130
|
else
|
|
109
131
|
error(string.format("%q is not a member of AttributeValue", tostring(index)))
|
|
110
132
|
end
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
--!strict
|
|
1
2
|
--[=[
|
|
2
3
|
Utility functions involving attributes.
|
|
3
4
|
@class RxAttributeUtils
|
|
@@ -9,6 +10,7 @@ local Brio = require("Brio")
|
|
|
9
10
|
local Maid = require("Maid")
|
|
10
11
|
local Observable = require("Observable")
|
|
11
12
|
local Symbol = require("Symbol")
|
|
13
|
+
local _Rx = require("Rx")
|
|
12
14
|
|
|
13
15
|
local UNSET_VALUE = Symbol.named("unsetValue")
|
|
14
16
|
|
|
@@ -21,7 +23,11 @@ local RxAttributeUtils = {}
|
|
|
21
23
|
@param defaultValue any?
|
|
22
24
|
@return Observable<any>
|
|
23
25
|
]=]
|
|
24
|
-
function RxAttributeUtils.observeAttribute(
|
|
26
|
+
function RxAttributeUtils.observeAttribute<T>(
|
|
27
|
+
instance: Instance,
|
|
28
|
+
attributeName: string,
|
|
29
|
+
defaultValue: T?
|
|
30
|
+
): Observable.Observable<T>
|
|
25
31
|
assert(typeof(instance) == "Instance", "Bad instance")
|
|
26
32
|
assert(type(attributeName) == "string", "Bad attributeName")
|
|
27
33
|
|
|
@@ -39,7 +45,7 @@ function RxAttributeUtils.observeAttribute(instance, attributeName, defaultValue
|
|
|
39
45
|
handleAttributeChanged()
|
|
40
46
|
|
|
41
47
|
return connection
|
|
42
|
-
end)
|
|
48
|
+
end) :: any
|
|
43
49
|
end
|
|
44
50
|
|
|
45
51
|
--[=[
|
|
@@ -47,15 +53,15 @@ end
|
|
|
47
53
|
@param instance Instance
|
|
48
54
|
@return Observable<Brio<string>>
|
|
49
55
|
]=]
|
|
50
|
-
function RxAttributeUtils.observeAttributeKeysBrio(instance)
|
|
56
|
+
function RxAttributeUtils.observeAttributeKeysBrio(instance: Instance): Observable.Observable<Brio.Brio<string>>
|
|
51
57
|
assert(typeof(instance) == "Instance", "Bad instance")
|
|
52
58
|
|
|
53
59
|
return Observable.new(function(sub)
|
|
54
60
|
local maid = Maid.new()
|
|
55
61
|
|
|
56
|
-
local attributeNameToBrio = {}
|
|
62
|
+
local attributeNameToBrio: { [string]: any } = {}
|
|
57
63
|
|
|
58
|
-
local function handleAttributeChanged(attributeName, attributeValue)
|
|
64
|
+
local function handleAttributeChanged(attributeName: string, attributeValue: any)
|
|
59
65
|
if attributeValue == nil then
|
|
60
66
|
local brio = attributeNameToBrio[attributeName]
|
|
61
67
|
if brio then
|
|
@@ -64,7 +70,7 @@ function RxAttributeUtils.observeAttributeKeysBrio(instance)
|
|
|
64
70
|
end
|
|
65
71
|
else
|
|
66
72
|
if not attributeNameToBrio[attributeName] then
|
|
67
|
-
local brio = Brio.new(attributeName)
|
|
73
|
+
local brio: any = Brio.new(attributeName)
|
|
68
74
|
attributeNameToBrio[attributeName] = brio
|
|
69
75
|
maid[brio] = brio
|
|
70
76
|
sub:Fire(brio)
|
|
@@ -86,7 +92,7 @@ function RxAttributeUtils.observeAttributeKeysBrio(instance)
|
|
|
86
92
|
end
|
|
87
93
|
|
|
88
94
|
return maid
|
|
89
|
-
end)
|
|
95
|
+
end) :: any
|
|
90
96
|
end
|
|
91
97
|
|
|
92
98
|
--[=[
|
|
@@ -95,7 +101,7 @@ end
|
|
|
95
101
|
@param instance Instance
|
|
96
102
|
@return Observable<string>
|
|
97
103
|
]=]
|
|
98
|
-
function RxAttributeUtils.observeAttributeKeys(instance)
|
|
104
|
+
function RxAttributeUtils.observeAttributeKeys(instance: Instance): Observable.Observable<string>
|
|
99
105
|
assert(typeof(instance) == "Instance", "Bad instance")
|
|
100
106
|
|
|
101
107
|
return Observable.new(function(sub)
|
|
@@ -114,7 +120,7 @@ function RxAttributeUtils.observeAttributeKeys(instance)
|
|
|
114
120
|
end
|
|
115
121
|
|
|
116
122
|
return maid
|
|
117
|
-
end)
|
|
123
|
+
end) :: any
|
|
118
124
|
end
|
|
119
125
|
|
|
120
126
|
--[=[
|
|
@@ -124,7 +130,7 @@ end
|
|
|
124
130
|
@param condition function | nil
|
|
125
131
|
@return Observable<Brio<any>>
|
|
126
132
|
]=]
|
|
127
|
-
function RxAttributeUtils.observeAttributeBrio(instance, attributeName, condition)
|
|
133
|
+
function RxAttributeUtils.observeAttributeBrio<T>(instance: Instance, attributeName: string, condition: _Rx.Predicate<T>?): Observable.Observable<Brio.Brio<T>>
|
|
128
134
|
assert(typeof(instance) == "Instance", "Bad instance")
|
|
129
135
|
assert(type(attributeName) == "string", "Bad attributeName")
|
|
130
136
|
|
|
@@ -141,7 +147,7 @@ function RxAttributeUtils.observeAttributeBrio(instance, attributeName, conditio
|
|
|
141
147
|
if lastValue ~= attributeValue then
|
|
142
148
|
lastValue = attributeValue
|
|
143
149
|
|
|
144
|
-
if not condition or condition(attributeValue) then
|
|
150
|
+
if not condition or condition(attributeValue :: T) then
|
|
145
151
|
local brio = Brio.new(attributeValue)
|
|
146
152
|
maid._lastBrio = brio
|
|
147
153
|
|
|
@@ -159,7 +165,7 @@ function RxAttributeUtils.observeAttributeBrio(instance, attributeName, conditio
|
|
|
159
165
|
handleAttributeChanged()
|
|
160
166
|
|
|
161
167
|
return maid
|
|
162
|
-
end)
|
|
168
|
+
end) :: any
|
|
163
169
|
end
|
|
164
170
|
|
|
165
171
|
return RxAttributeUtils
|