@quenty/statestack 3.4.0 → 3.4.2-canary.4c04014.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,22 @@
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
+ ## [3.4.2-canary.4c04014.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/statestack@3.4.1...@quenty/statestack@3.4.2-canary.4c04014.0) (2022-01-17)
7
+
8
+ **Note:** Version bump only for package @quenty/statestack
9
+
10
+
11
+
12
+
13
+
14
+ ## [3.4.1](https://github.com/Quenty/NevermoreEngine/compare/@quenty/statestack@3.4.0...@quenty/statestack@3.4.1) (2022-01-16)
15
+
16
+ **Note:** Version bump only for package @quenty/statestack
17
+
18
+
19
+
20
+
21
+
6
22
  # [3.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/statestack@3.3.0...@quenty/statestack@3.4.0) (2022-01-07)
7
23
 
8
24
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quenty/statestack",
3
- "version": "3.4.0",
3
+ "version": "3.4.2-canary.4c04014.0",
4
4
  "description": "Stack of values that allows multiple systems to enable or disable a state",
5
5
  "keywords": [
6
6
  "Roblox",
@@ -25,12 +25,13 @@
25
25
  "Quenty"
26
26
  ],
27
27
  "dependencies": {
28
- "@quenty/baseobject": "^3.4.0",
29
- "@quenty/loader": "^3.3.0",
30
- "@quenty/valueobject": "^3.7.0"
28
+ "@quenty/baseobject": "3.4.1-canary.4c04014.0",
29
+ "@quenty/brio": "3.7.2-canary.4c04014.0",
30
+ "@quenty/loader": "3.3.1-canary.4c04014.0",
31
+ "@quenty/valueobject": "3.7.2-canary.4c04014.0"
31
32
  },
32
33
  "publishConfig": {
33
34
  "access": "public"
34
35
  },
35
- "gitHead": "5a3f3fb6c908fd3874f5ceacc70b404780275119"
36
+ "gitHead": "4c040145693283525ba3f4c793cead7abee28fb2"
36
37
  }
@@ -0,0 +1,50 @@
1
+ --[=[
2
+ @class RxStateStackUtils
3
+ ]=]
4
+
5
+ local require = require(script.Parent.loader).load(script)
6
+
7
+ local StateStack = require("StateStack")
8
+ local Observable = require("Observable")
9
+ local Maid = require("Maid")
10
+ local Brio = require("Brio")
11
+
12
+ local RxStateStackUtils = {}
13
+
14
+ --[=[
15
+ Converts the observable of Brios into a statestack.
16
+ @return (source: Observable<Brio<T>>) -> Observable<T?>
17
+ ]=]
18
+ function RxStateStackUtils.topOfStack()
19
+ return function(source)
20
+ return Observable.new(function(sub)
21
+ local maid = Maid.new()
22
+
23
+ local current = StateStack.new(nil)
24
+ maid:GiveTask(current)
25
+
26
+ maid:GiveTask(source:Subscribe(function(brio)
27
+ if not Brio.isBrio(brio) then
28
+ warn("Not a brio")
29
+ return
30
+ end
31
+
32
+ if not brio:IsDead() then
33
+ brio:ToMaid():GiveTask(current:PushState(brio:GetValue()))
34
+ end
35
+ end))
36
+
37
+ local function update()
38
+ sub:Fire(current:GetState())
39
+ end
40
+
41
+ maid:GiveTask(current.Changed:Connect(update))
42
+ update()
43
+
44
+ return maid
45
+ end)
46
+
47
+ end
48
+ end
49
+
50
+ return RxStateStackUtils
@@ -2,14 +2,14 @@
2
2
  Stack of values that allows multiple systems to enable or disable a state.
3
3
 
4
4
  ```lua
5
- local disabledStack = StateStack.new()
5
+ local disabledStack = StateStack.new(false)
6
6
  print(disabledStack:GetState()) --> false
7
7
 
8
8
  disabledStack.Changed:Connect(function()
9
9
  print("From changed event we have state: ", disabledStack:GetState())
10
10
  end)
11
11
 
12
- local cancel = disabledStack:PushState() --> From changed event we have state: true
12
+ local cancel = disabledStack:PushState(true) --> From changed event we have state: true
13
13
  print(disabledStack:GetState()) --> true
14
14
 
15
15
  cancel() --> From changed event we have state: true
@@ -32,12 +32,14 @@ StateStack.__index = StateStack
32
32
 
33
33
  --[=[
34
34
  Constructs a new StateStack.
35
+ @param defaultValue any -- The default value to use for the statestack.
35
36
  @return StateStack
36
37
  ]=]
37
- function StateStack.new()
38
+ function StateStack.new(defaultValue)
38
39
  local self = setmetatable(BaseObject.new(), StateStack)
39
40
 
40
- self._state = ValueObject.new(false)
41
+ self._defaultValue = defaultValue
42
+ self._state = ValueObject.new(defaultValue)
41
43
  self._maid:GiveTask(self._state)
42
44
 
43
45
  self._stateStack = {}
@@ -52,6 +54,14 @@ function StateStack.new()
52
54
  return self
53
55
  end
54
56
 
57
+ --[=[
58
+ Gets the count of the stack
59
+ @return number
60
+ ]=]
61
+ function StateStack:GetCount()
62
+ return #self._stateStack
63
+ end
64
+
55
65
  --[=[
56
66
  Gets the current state
57
67
  @return T?
@@ -74,13 +84,8 @@ end
74
84
  @return function -- Cleanup function to invoke
75
85
  ]=]
76
86
  function StateStack:PushState(state)
77
- if state == nil then
78
- state = true
79
- end
80
-
81
87
  local data = { state }
82
88
  table.insert(self._stateStack, data)
83
-
84
89
  self:_updateState()
85
90
 
86
91
  return function()
@@ -101,15 +106,11 @@ function StateStack:_popState(data)
101
106
  end
102
107
 
103
108
  function StateStack:_updateState()
104
- local _, data = next(self._stateStack)
105
- if data == nil then
106
- if type(self._state.Value) == "boolean" then
107
- self._state.Value = false
108
- else
109
- self._state.Value = nil
110
- end
109
+ local dataContainer = self._stateStack[#self._stateStack]
110
+ if dataContainer == nil then
111
+ self._state.Value = self._defaultValue
111
112
  else
112
- self._state.Value = data[1]
113
+ self._state.Value = dataContainer[1]
113
114
  end
114
115
  end
115
116