@quenty/valueobject 7.7.0 → 7.8.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/LICENSE.md +1 -1
- package/package.json +2 -2
- package/src/Shared/ValueObject.lua +56 -17
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
|
+
# [7.8.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/valueobject@7.7.0...@quenty/valueobject@7.8.0) (2023-03-31)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
### Features
|
|
10
|
+
|
|
11
|
+
* Support chaining additional arguments to ValueObject ([b53afa9](https://github.com/Quenty/NevermoreEngine/commit/b53afa993d710cad092975ad07d5ec92a563ddce))
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
|
|
6
17
|
# [7.7.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/valueobject@7.6.0...@quenty/valueobject@7.7.0) (2023-03-05)
|
|
7
18
|
|
|
8
19
|
**Note:** Version bump only for package @quenty/valueobject
|
package/LICENSE.md
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/valueobject",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.8.0",
|
|
4
4
|
"description": "To work like value objects in Roblox and track a single item with .Changed events",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -35,5 +35,5 @@
|
|
|
35
35
|
"publishConfig": {
|
|
36
36
|
"access": "public"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "cf537ed1fe265276971486129beb372cbaaca4a6"
|
|
39
39
|
}
|
|
@@ -27,10 +27,10 @@ function ValueObject.new(baseValue)
|
|
|
27
27
|
|
|
28
28
|
--[=[
|
|
29
29
|
Event fires when the value's object value change
|
|
30
|
-
@prop Changed Signal<T> -- fires with oldValue, newValue
|
|
30
|
+
@prop Changed Signal<T> -- fires with oldValue, newValue, ...
|
|
31
31
|
@within ValueObject
|
|
32
32
|
]=]
|
|
33
|
-
self.Changed = Signal.new() -- :Fire(newValue, oldValue, maid)
|
|
33
|
+
self.Changed = Signal.new() -- :Fire(newValue, oldValue, maid, ...)
|
|
34
34
|
self._maid:GiveTask(self.Changed)
|
|
35
35
|
|
|
36
36
|
return setmetatable(self, ValueObject)
|
|
@@ -44,8 +44,8 @@ end
|
|
|
44
44
|
function ValueObject.fromObservable(observable)
|
|
45
45
|
local result = ValueObject.new()
|
|
46
46
|
|
|
47
|
-
result._maid:GiveTask(observable:Subscribe(function(value)
|
|
48
|
-
result
|
|
47
|
+
result._maid:GiveTask(observable:Subscribe(function(value, ...)
|
|
48
|
+
result:SetValue(value, ...)
|
|
49
49
|
end))
|
|
50
50
|
|
|
51
51
|
return result
|
|
@@ -75,15 +75,53 @@ function ValueObject:Observe()
|
|
|
75
75
|
|
|
76
76
|
local maid = Maid.new()
|
|
77
77
|
|
|
78
|
-
maid:GiveTask(self.Changed:Connect(function()
|
|
79
|
-
sub:Fire(
|
|
78
|
+
maid:GiveTask(self.Changed:Connect(function(newValue, _, _, ...)
|
|
79
|
+
sub:Fire(newValue, ...)
|
|
80
80
|
end))
|
|
81
81
|
|
|
82
|
-
|
|
82
|
+
local args = rawget(self, "_lastEventContext")
|
|
83
|
+
if args then
|
|
84
|
+
sub:Fire(self.Value, table.unpack(args, 1, args.n))
|
|
85
|
+
else
|
|
86
|
+
sub:Fire(self.Value)
|
|
87
|
+
end
|
|
83
88
|
|
|
84
89
|
return maid
|
|
85
90
|
end)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
--[=[
|
|
94
|
+
Allows you to set a value, and provide additional event context for the actual change.
|
|
95
|
+
For example, you might do.
|
|
96
|
+
|
|
97
|
+
```lua
|
|
98
|
+
self.IsVisible:SetValue(isVisible, true)
|
|
99
|
+
|
|
100
|
+
print(self.IsVisible.Changed:Connect(function(isVisible, _, _, doNotAnimate)
|
|
101
|
+
print(doNotAnimate)
|
|
102
|
+
end))
|
|
103
|
+
```
|
|
104
|
+
|
|
105
|
+
@param value T
|
|
106
|
+
@param ... any -- Additional args. Can be used to pass event changing state args with value
|
|
107
|
+
]=]
|
|
108
|
+
function ValueObject:SetValue(value, ...)
|
|
109
|
+
local previous = rawget(self, "_value")
|
|
110
|
+
if previous ~= value then
|
|
111
|
+
if select("#", ...) > 0 then
|
|
112
|
+
rawset(self, "_lastEventContext", table.pack(...))
|
|
113
|
+
else
|
|
114
|
+
rawset(self, "_lastEventContext", nil)
|
|
115
|
+
end
|
|
86
116
|
|
|
117
|
+
rawset(self, "_value", value)
|
|
118
|
+
|
|
119
|
+
local maid = Maid.new()
|
|
120
|
+
|
|
121
|
+
self.Changed:Fire(value, previous, maid, ...)
|
|
122
|
+
|
|
123
|
+
self._maid._valueMaid = maid
|
|
124
|
+
end
|
|
87
125
|
end
|
|
88
126
|
|
|
89
127
|
--[=[
|
|
@@ -96,6 +134,13 @@ function ValueObject:__index(index)
|
|
|
96
134
|
return self._value
|
|
97
135
|
elseif ValueObject[index] then
|
|
98
136
|
return ValueObject[index]
|
|
137
|
+
elseif index == "LastEventContext" then
|
|
138
|
+
local args = rawget(self, "_lastEventContext")
|
|
139
|
+
if args then
|
|
140
|
+
return table.unpack(args, 1, args.n)
|
|
141
|
+
else
|
|
142
|
+
return
|
|
143
|
+
end
|
|
99
144
|
elseif index == "_value" then
|
|
100
145
|
return nil -- Edge case
|
|
101
146
|
else
|
|
@@ -105,16 +150,10 @@ end
|
|
|
105
150
|
|
|
106
151
|
function ValueObject:__newindex(index, value)
|
|
107
152
|
if index == "Value" then
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
local maid = Maid.new()
|
|
113
|
-
|
|
114
|
-
self.Changed:Fire(value, previous, maid)
|
|
115
|
-
|
|
116
|
-
self._maid._valueMaid = maid
|
|
117
|
-
end
|
|
153
|
+
-- Avoid deoptimization
|
|
154
|
+
ValueObject.SetValue(self, value)
|
|
155
|
+
elseif index == "LastEventContext" or ValueObject[index] then
|
|
156
|
+
error(("%q cannot be set in ValueObject"):format(tostring(index)))
|
|
118
157
|
else
|
|
119
158
|
error(("%q is not a member of ValueObject"):format(tostring(index)))
|
|
120
159
|
end
|