@quenty/attributeutils 14.14.0 → 14.15.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 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.15.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/attributeutils@14.14.0...@quenty/attributeutils@14.15.0) (2024-12-15)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add JSONAttributeValue and EncodedAttributeValue ([fbb09eb](https://github.com/Quenty/NevermoreEngine/commit/fbb09ebcbec2e22580918727e5157f0789fafa17))
12
+
13
+
14
+
15
+
16
+
6
17
  # [14.14.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/attributeutils@14.13.0...@quenty/attributeutils@14.14.0) (2024-12-03)
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.14.0",
3
+ "version": "14.15.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": "^14.14.0",
29
- "@quenty/canceltoken": "^11.9.0",
28
+ "@quenty/brio": "^14.15.0",
29
+ "@quenty/canceltoken": "^11.10.0",
30
30
  "@quenty/loader": "^10.7.1",
31
31
  "@quenty/maid": "^3.4.0",
32
- "@quenty/promise": "^10.8.0",
33
- "@quenty/rx": "^13.14.0",
34
- "@quenty/rxsignal": "^7.14.0",
32
+ "@quenty/promise": "^10.9.0",
33
+ "@quenty/rx": "^13.15.0",
34
+ "@quenty/rxsignal": "^7.15.0",
35
35
  "@quenty/symbol": "^3.4.0"
36
36
  },
37
37
  "publishConfig": {
38
38
  "access": "public"
39
39
  },
40
- "gitHead": "66190e48c1ca93b07040326e9cfcf97e8eb4b0e1"
40
+ "gitHead": "0a20ace4dc7d38f8c889bf73b716b33e8a767c54"
41
41
  }
@@ -0,0 +1,112 @@
1
+ --[=[
2
+ Allows access to an attribute like a ValueObject, but also encoded or decoded
3
+
4
+ @class EncodedAttributeValue
5
+ ]=]
6
+
7
+ local require = require(script.Parent.loader).load(script)
8
+
9
+ local RxAttributeUtils = require("RxAttributeUtils")
10
+ local Rx = require("Rx")
11
+ local RxBrioUtils = require("RxBrioUtils")
12
+
13
+ local EncodedAttributeValue = {}
14
+ EncodedAttributeValue.ClassName = "EncodedAttributeValue"
15
+ EncodedAttributeValue.__index = EncodedAttributeValue
16
+
17
+ --[=[
18
+ Constructs a new EncodedAttributeValue. If a defaultValue that is not nil
19
+ is defined, then this value will be set on the Roblox object.
20
+
21
+ @param object Instance
22
+ @param attributeName string
23
+ @param encode (TValue) -> T
24
+ @param decode (T) -> TValue
25
+ @param defaultValue T?
26
+ @return EncodedAttributeValue<T, TValue>
27
+ ]=]
28
+ function EncodedAttributeValue.new(object, attributeName, encode, decode, defaultValue)
29
+ assert(typeof(object) == "Instance", "Bad object")
30
+ assert(type(attributeName) == "string", "Bad attributeName")
31
+ assert(type(decode) == "function", "Bad decode")
32
+ assert(type(encode) == "function", "Bad encode")
33
+
34
+ local self = {
35
+ _object = object;
36
+ _attributeName = attributeName;
37
+ _decode = decode;
38
+ _encode = encode;
39
+ }
40
+
41
+ if defaultValue ~= nil and self._object:GetAttribute(self._attributeName) == nil then
42
+ self._object:SetAttribute(rawget(self, "_attributeName"), encode(defaultValue))
43
+ end
44
+ return setmetatable(self, EncodedAttributeValue)
45
+ end
46
+
47
+ --[=[
48
+ Handles observing the value conditionalli
49
+
50
+ @param condition function | nil
51
+ @return Observable<Brio<any>>
52
+ ]=]
53
+ function EncodedAttributeValue:ObserveBrio(condition)
54
+ return RxAttributeUtils.observeAttributeBrio(self._object, self._attributeName, condition):Pipe({
55
+ RxBrioUtils.map(rawget(self, "_decode"))
56
+ })
57
+ end
58
+
59
+ --[=[
60
+ Observes an attribute on an instance.
61
+ @return Observable<any>
62
+ ]=]
63
+ function EncodedAttributeValue:Observe()
64
+ return RxAttributeUtils.observeAttribute(self._object, self._attributeName, rawget(self, "_defaultValue")):Pipe({
65
+ Rx.map(rawget(self, "_decode"))
66
+ })
67
+ end
68
+
69
+ --[=[
70
+ The current property of the Attribute. Can be assigned to to write
71
+ the attribute.
72
+ @prop Value T
73
+ @within EncodedAttributeValue
74
+ ]=]
75
+
76
+ --[=[
77
+ Signal that fires when the attribute changes
78
+ @readonly
79
+ @prop Changed Signal<()>
80
+ @within EncodedAttributeValue
81
+ ]=]
82
+ function EncodedAttributeValue:__index(index)
83
+ if EncodedAttributeValue[index] then
84
+ return EncodedAttributeValue[index]
85
+ elseif index == "Value" then
86
+ local result = self._object:GetAttribute(rawget(self, "_attributeName"))
87
+ local default = rawget(self, "_defaultValue")
88
+ if result == nil then
89
+ return default
90
+ else
91
+ local decode = rawget(self, "_decode")
92
+ return decode(result)
93
+ end
94
+ elseif index == "Changed" then
95
+ return self._object:GetAttributeChangedSignal(self._attributeName)
96
+ elseif index == "AttributeName" then
97
+ return rawget(self, "_attributeName")
98
+ else
99
+ error(string.format("%q is not a member of EncodedAttributeValue", tostring(index)))
100
+ end
101
+ end
102
+
103
+ function EncodedAttributeValue:__newindex(index, value)
104
+ if index == "Value" then
105
+ local encode = rawget(self, "_encode")
106
+ self._object:SetAttribute(rawget(self, "_attributeName"), encode(value))
107
+ else
108
+ error(string.format("%q is not a member of EncodedAttributeValue", tostring(index)))
109
+ end
110
+ end
111
+
112
+ return EncodedAttributeValue
@@ -0,0 +1,31 @@
1
+ --[=[
2
+ @class JSONAttributeValue
3
+ ]=]
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local HttpService = game:GetService("HttpService")
8
+ local BaseObject = require("BaseObject")
9
+ local EncodedAttributeValue = require("EncodedAttributeValue")
10
+
11
+ local JSONAttributeValue = setmetatable({}, BaseObject)
12
+ JSONAttributeValue.ClassName = "JSONAttributeValue"
13
+ JSONAttributeValue.__index = JSONAttributeValue
14
+
15
+ function JSONAttributeValue.new(object, attributeName, defaultValue)
16
+ return EncodedAttributeValue.new(object, attributeName, function(value)
17
+ if type(value) == "table" or type(value) == "string" then
18
+ return HttpService:JSONEncode(value)
19
+ else
20
+ return nil
21
+ end
22
+ end, function(value)
23
+ if type(value) == "string" then
24
+ return HttpService:JSONDecode(value)
25
+ else
26
+ return nil
27
+ end
28
+ end, defaultValue)
29
+ end
30
+
31
+ return JSONAttributeValue