@quenty/valueobject 7.13.0 → 7.14.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
+ # [7.14.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/valueobject@7.13.0...@quenty/valueobject@7.14.0) (2023-05-26)
7
+
8
+
9
+ ### Features
10
+
11
+ * Add type checking and mounting to ValueObject ([c3ee035](https://github.com/Quenty/NevermoreEngine/commit/c3ee0354df477b96e60e7aa14fd7a95be8ecd6ed))
12
+ * Initial refactor of guis to use ValueObject instead of ValueObject ([723aba0](https://github.com/Quenty/NevermoreEngine/commit/723aba0208cae7e06c9d8bf2d8f0092d042d70ea))
13
+
14
+
15
+
16
+
17
+
6
18
  # [7.13.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/valueobject@7.12.0...@quenty/valueobject@7.13.0) (2023-05-08)
7
19
 
8
20
  **Note:** Version bump only for package @quenty/valueobject
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/valueobject",
3
- "version": "7.13.0",
3
+ "version": "7.14.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",
@@ -26,14 +26,15 @@
26
26
  "Quenty"
27
27
  ],
28
28
  "dependencies": {
29
- "@quenty/brio": "^8.12.0",
29
+ "@quenty/brio": "^8.13.0",
30
30
  "@quenty/loader": "^6.2.1",
31
31
  "@quenty/maid": "^2.5.0",
32
- "@quenty/rx": "^7.10.0",
33
- "@quenty/signal": "^2.3.0"
32
+ "@quenty/rx": "^7.11.0",
33
+ "@quenty/signal": "^2.4.0",
34
+ "@quenty/valuebaseutils": "^7.14.0"
34
35
  },
35
36
  "publishConfig": {
36
37
  "access": "public"
37
38
  },
38
- "gitHead": "2ad8cea7dd3ad79a39afd7d7b785b489b90553fd"
39
+ "gitHead": "11058e90e51ea83d3dad6ae9abe59cc19c36b94b"
39
40
  }
@@ -6,9 +6,13 @@
6
6
 
7
7
  local require = require(script.Parent.loader).load(script)
8
8
 
9
- local Signal = require("Signal")
9
+ local GoodSignal = require("GoodSignal")
10
10
  local Maid = require("Maid")
11
11
  local Observable = require("Observable")
12
+ local ValueBaseUtils = require("ValueBaseUtils")
13
+ local RxValueBaseUtils = require("RxValueBaseUtils")
14
+
15
+ local EMPTY_FUNCTION = function() end
12
16
 
13
17
  local ValueObject = {}
14
18
  ValueObject.ClassName = "ValueObject"
@@ -16,21 +20,26 @@ ValueObject.ClassName = "ValueObject"
16
20
  --[=[
17
21
  Constructs a new value object
18
22
  @param baseValue T
23
+ @param checkType string | nil
19
24
  @return ValueObject
20
25
  ]=]
21
- function ValueObject.new(baseValue)
22
- local self = {}
23
-
24
- rawset(self, "_value", baseValue)
25
-
26
- self._maid = Maid.new()
26
+ function ValueObject.new(baseValue, checkType)
27
+ local self = {
28
+ _value = baseValue;
29
+ _checkType = checkType;
30
+ _maid = Maid.new();
31
+ }
32
+
33
+ if checkType and typeof(baseValue) ~= checkType then
34
+ error(string.format("Expected value of type %q, got %q instead", checkType, typeof(baseValue)))
35
+ end
27
36
 
28
37
  --[=[
29
38
  Event fires when the value's object value change
30
39
  @prop Changed Signal<T> -- fires with oldValue, newValue, ...
31
40
  @within ValueObject
32
41
  ]=]
33
- self.Changed = Signal.new() -- :Fire(newValue, oldValue, maid, ...)
42
+ self.Changed = GoodSignal.new() -- :Fire(newValue, oldValue, maid, ...)
34
43
  self._maid:GiveTask(self.Changed)
35
44
 
36
45
  return setmetatable(self, ValueObject)
@@ -44,9 +53,7 @@ end
44
53
  function ValueObject.fromObservable(observable)
45
54
  local result = ValueObject.new()
46
55
 
47
- result._maid:GiveTask(observable:Subscribe(function(value, ...)
48
- result:SetValue(value, ...)
49
- end))
56
+ result:Mount(observable)
50
57
 
51
58
  return result
52
59
  end
@@ -60,6 +67,63 @@ function ValueObject.isValueObject(value)
60
67
  return type(value) == "table" and getmetatable(value) == ValueObject
61
68
  end
62
69
 
70
+ function ValueObject:_toMountableObservable(value)
71
+ if Observable.isObservable(value) then
72
+ return value
73
+ elseif typeof(value) == "Instance" then
74
+ -- IntValue, ObjectValue, et cetera
75
+ if ValueBaseUtils.isValueBase(value) then
76
+ return RxValueBaseUtils.observeValue(value)
77
+ end
78
+ elseif type(value) == "table" then
79
+ if ValueObject.isValueObject(value) then
80
+ return value:Observe()
81
+ -- elseif Promise.isPromise(value) then
82
+ -- return Rx.fromPromise(value)
83
+ end
84
+ end
85
+
86
+ return nil
87
+ end
88
+ --[=[
89
+ Mounts the value to the observable. Overrides the last mount.
90
+
91
+ @param value Observable | T
92
+ @return MaidTask
93
+ ]=]
94
+ function ValueObject:Mount(value)
95
+ local observable = self:_toMountableObservable(value)
96
+ if observable then
97
+ self._maid._mount = nil
98
+
99
+ local maid = Maid.new()
100
+
101
+ maid:GiveTask(observable:Subscribe(function(...)
102
+ self:SetValue(...)
103
+ end))
104
+
105
+ maid:GiveTask(function()
106
+ if self._maid._mount == maid then
107
+ self._maid._mount = nil
108
+ end
109
+ end)
110
+
111
+ self._maid._mount = maid
112
+
113
+ return function()
114
+ if self._maid._mount == maid then
115
+ self._maid._mount = nil
116
+ end
117
+ end
118
+ else
119
+ self._maid._mount = nil
120
+
121
+ self:SetValue(value)
122
+
123
+ return EMPTY_FUNCTION
124
+ end
125
+ end
126
+
63
127
  --[=[
64
128
  Observes the current value of the ValueObject
65
129
  @return Observable<T>
@@ -107,6 +171,12 @@ end
107
171
  ]=]
108
172
  function ValueObject:SetValue(value, ...)
109
173
  local previous = rawget(self, "_value")
174
+ local checkType = rawget(self, "_checkType")
175
+
176
+ if checkType and typeof(value) ~= checkType then
177
+ error(string.format("Expected value of type %q, got %q instead", checkType, typeof(value)))
178
+ end
179
+
110
180
  if previous ~= value then
111
181
  if select("#", ...) > 0 then
112
182
  rawset(self, "_lastEventContext", table.pack(...))