@quenty/attributeutils 6.1.0 → 6.2.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,18 @@
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
+ # [6.2.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/attributeutils@6.1.0...@quenty/attributeutils@6.2.0) (2022-07-31)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add AttributeUtils.isValidAttributeType(valueType) ([b77392b](https://github.com/Quenty/NevermoreEngine/commit/b77392b4e373c297fdc7b79bd26ef218ac327f18))
12
+ * Add RxAttributeUtils.observeAttributeBrio API call ([e0e827f](https://github.com/Quenty/NevermoreEngine/commit/e0e827fcf9a17c4ff0ecad0209bcc976912ae3b5))
13
+
14
+
15
+
16
+
17
+
6
18
  # [6.1.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/attributeutils@6.0.0...@quenty/attributeutils@6.1.0) (2022-06-21)
7
19
 
8
20
  **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": "6.1.0",
3
+ "version": "6.2.0",
4
4
  "description": "Provides utility functions to work with attributes in Roblox",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -25,12 +25,14 @@
25
25
  "Quenty"
26
26
  ],
27
27
  "dependencies": {
28
+ "@quenty/brio": "^6.2.0",
28
29
  "@quenty/loader": "^5.0.0",
29
- "@quenty/maid": "^2.3.0",
30
- "@quenty/rx": "^5.1.0"
30
+ "@quenty/maid": "^2.4.0",
31
+ "@quenty/rx": "^5.2.0",
32
+ "@quenty/symbol": "^2.1.0"
31
33
  },
32
34
  "publishConfig": {
33
35
  "access": "public"
34
36
  },
35
- "gitHead": "c8732cc5dea767b3ff362db43137e2a16da7bc0d"
37
+ "gitHead": "e31b3a35aa475bb5699a24898a8639e107165b36"
36
38
  }
@@ -11,6 +11,37 @@ local Maid = require("Maid")
11
11
 
12
12
  local AttributeUtils = {}
13
13
 
14
+ local VALID_ATTRIBUTE_TYPES = {
15
+ ["nil"] = true;
16
+ ["string"] = true;
17
+ ["boolean"] = true;
18
+ ["number"] = true;
19
+ ["UDim"] = true;
20
+ ["UDim2"] = true;
21
+ ["BrickColor"] = true;
22
+ ["Color3"] = true;
23
+ ["Vector2"] = true;
24
+ ["Vector3"] = true;
25
+ ["NumberSequence"] = true;
26
+ ["ColorSequence"] = true;
27
+ ["NumberRange"] = true;
28
+ ["Rect"] = true;
29
+ }
30
+
31
+ --[=[
32
+ Returns whether the attribute is a valid type or not for an attribute.
33
+
34
+ ```lua
35
+ print(AttributeUtils.isValidAttributeType(typeof("hi"))) --> true
36
+ ```
37
+
38
+ @param valueType string
39
+ @return boolean
40
+ ]=]
41
+ function AttributeUtils.isValidAttributeType(valueType)
42
+ return VALID_ATTRIBUTE_TYPES[valueType] == true
43
+ end
44
+
14
45
  --[=[
15
46
  Whenever the attribute is true, the binder will be bound, and when the
16
47
  binder is bound, the attribute will be true.
@@ -5,8 +5,12 @@
5
5
 
6
6
  local require = require(script.Parent.loader).load(script)
7
7
 
8
- local Observable = require("Observable")
8
+ local Brio = require("Brio")
9
9
  local Maid = require("Maid")
10
+ local Observable = require("Observable")
11
+ local Symbol = require("Symbol")
12
+
13
+ local UNSET_VALUE = Symbol.named("unsetValue")
10
14
 
11
15
  local RxAttributeUtils = {}
12
16
 
@@ -24,17 +28,62 @@ function RxAttributeUtils.observeAttribute(instance, attributeName, defaultValue
24
28
  return Observable.new(function(sub)
25
29
  local maid = Maid.new()
26
30
 
27
- local function update()
28
- local value = instance:GetAttribute(attributeName)
29
- if value == nil then
31
+ local function handleAttributeChanged()
32
+ local attributeValue = instance:GetAttribute(attributeName)
33
+ if attributeValue == nil then
30
34
  sub:Fire(defaultValue)
31
35
  else
32
- sub:Fire(value)
36
+ sub:Fire(attributeValue)
37
+ end
38
+ end
39
+
40
+ maid:GiveTask(instance:GetAttributeChangedSignal(attributeName):Connect(handleAttributeChanged))
41
+ handleAttributeChanged()
42
+
43
+ return maid
44
+ end)
45
+ end
46
+
47
+ --[=[
48
+ Observes an attribute on an instance with a conditional statement.
49
+ @param instance Instance
50
+ @param attributeName string
51
+ @param condition function
52
+ @return Observable<Brio<any>>
53
+ ]=]
54
+ function RxAttributeUtils.observeAttributeBrio(instance, attributeName, condition)
55
+ assert(typeof(instance) == "Instance", "Bad instance")
56
+ assert(type(attributeName) == "string", "Bad attributeName")
57
+
58
+ return Observable.new(function(sub)
59
+ local maid = Maid.new()
60
+ local lastValue = UNSET_VALUE
61
+
62
+ local function handleAttributeChanged()
63
+ local attributeValue = instance:GetAttribute(attributeName)
64
+
65
+ -- Deferred events can cause multiple values to be queued at once
66
+ -- but we operate at this post-deferred layer, so lets only output
67
+ -- reflected values.
68
+ if lastValue ~= attributeValue then
69
+ lastValue = attributeValue
70
+
71
+ if not condition or condition(attributeValue) then
72
+ local brio = Brio.new(attributeValue)
73
+ maid._lastBrio = brio
74
+
75
+ -- The above line can cause us to be overwritten so make sure before firing.
76
+ if maid._lastBrio == brio then
77
+ sub:Fire(brio)
78
+ end
79
+ else
80
+ maid._lastBrio = nil
81
+ end
33
82
  end
34
83
  end
35
84
 
36
- maid:GiveTask(instance:GetAttributeChangedSignal(attributeName):Connect(update))
37
- update()
85
+ maid:GiveTask(instance:GetAttributeChangedSignal(attributeName):Connect(handleAttributeChanged))
86
+ handleAttributeChanged()
38
87
 
39
88
  return maid
40
89
  end)