@quenty/scoredactionservice 16.53.0 → 16.53.2

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.
@@ -0,0 +1,278 @@
1
+ --!strict
2
+ --[[
3
+ @class TouchButtonScoredActionPicker.spec.lua
4
+ ]]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local Jest = require("Jest")
9
+ local Maid = require("Maid")
10
+ local ScoredAction = require("ScoredAction")
11
+ local TouchButtonScoredActionPicker = require("TouchButtonScoredActionPicker")
12
+
13
+ local describe = Jest.Globals.describe
14
+ local expect = Jest.Globals.expect
15
+ local it = Jest.Globals.it
16
+
17
+ type Controller = {
18
+ newPicker: () -> TouchButtonScoredActionPicker.TouchButtonScoredActionPicker,
19
+ newLoosePicker: () -> TouchButtonScoredActionPicker.TouchButtonScoredActionPicker,
20
+ newAction: (number) -> ScoredAction.ScoredAction,
21
+ newLooseAction: (number) -> ScoredAction.ScoredAction,
22
+ destroy: () -> (),
23
+ }
24
+
25
+ local function setup(): Controller
26
+ local maid = Maid.new()
27
+
28
+ local function makeAction(score: number): ScoredAction.ScoredAction
29
+ local action: ScoredAction.ScoredAction = ScoredAction.new()
30
+ action:SetScore(score)
31
+ return action
32
+ end
33
+
34
+ local controller: Controller = {
35
+ newPicker = function()
36
+ return maid:Add(TouchButtonScoredActionPicker.new()) :: any
37
+ end,
38
+
39
+ newLoosePicker = function()
40
+ return TouchButtonScoredActionPicker.new()
41
+ end,
42
+
43
+ newAction = function(score: number)
44
+ return maid:Add(makeAction(score)) :: any
45
+ end,
46
+
47
+ newLooseAction = makeAction,
48
+
49
+ destroy = function()
50
+ maid:DoCleaning()
51
+ end,
52
+ }
53
+
54
+ return controller
55
+ end
56
+
57
+ describe("TouchButtonScoredActionPicker.Update", function()
58
+ it("prefers every action at once", function()
59
+ local controller = setup()
60
+ local picker = controller.newPicker()
61
+
62
+ local first = controller.newAction(1)
63
+ local second = controller.newAction(2)
64
+ picker:AddAction(first)
65
+ picker:AddAction(second)
66
+ picker:Update()
67
+
68
+ -- Unlike keys, there are as many touch buttons as we want, so a lower score is not a loss.
69
+ expect(first:IsPreferred()).toBe(true)
70
+ expect(second:IsPreferred()).toBe(true)
71
+
72
+ controller.destroy()
73
+ end)
74
+
75
+ it("never prefers an action scored at -math.huge", function()
76
+ local controller = setup()
77
+ local picker = controller.newPicker()
78
+
79
+ local action = controller.newAction(-math.huge)
80
+ picker:AddAction(action)
81
+ picker:Update()
82
+
83
+ expect(action:IsPreferred()).toBe(false)
84
+
85
+ controller.destroy()
86
+ end)
87
+
88
+ it("does not prefer an action that is already disabled", function()
89
+ local controller = setup()
90
+ local picker = controller.newPicker()
91
+
92
+ local action = controller.newAction(1)
93
+ action:SetIsEnabled(false)
94
+ picker:AddAction(action)
95
+
96
+ expect(action:IsPreferred()).toBe(false)
97
+
98
+ controller.destroy()
99
+ end)
100
+
101
+ it("drops the preference when the action is disabled", function()
102
+ local controller = setup()
103
+ local picker = controller.newPicker()
104
+
105
+ local action = controller.newAction(1)
106
+ picker:AddAction(action)
107
+ expect(action:IsPreferred()).toBe(true)
108
+
109
+ action:SetIsEnabled(false)
110
+ picker:Update()
111
+
112
+ expect(action:IsPreferred()).toBe(false)
113
+
114
+ controller.destroy()
115
+ end)
116
+
117
+ it("gives the preference back when the action is re-enabled", function()
118
+ local controller = setup()
119
+ local picker = controller.newPicker()
120
+
121
+ local action = controller.newAction(1)
122
+ picker:AddAction(action)
123
+
124
+ action:SetIsEnabled(false)
125
+ picker:Update()
126
+ expect(action:IsPreferred()).toBe(false)
127
+
128
+ action:SetIsEnabled(true)
129
+ picker:Update()
130
+ expect(action:IsPreferred()).toBe(true)
131
+
132
+ controller.destroy()
133
+ end)
134
+
135
+ it("leaves the other actions alone when one is disabled", function()
136
+ local controller = setup()
137
+ local picker = controller.newPicker()
138
+
139
+ local disabled = controller.newAction(1)
140
+ local enabled = controller.newAction(1)
141
+ picker:AddAction(disabled)
142
+ picker:AddAction(enabled)
143
+
144
+ disabled:SetIsEnabled(false)
145
+ picker:Update()
146
+
147
+ expect(disabled:IsPreferred()).toBe(false)
148
+ expect(enabled:IsPreferred()).toBe(true)
149
+
150
+ controller.destroy()
151
+ end)
152
+
153
+ it("drops the preference when the action is removed", function()
154
+ local controller = setup()
155
+ local picker = controller.newPicker()
156
+
157
+ local action = controller.newAction(1)
158
+ picker:AddAction(action)
159
+ expect(action:IsPreferred()).toBe(true)
160
+
161
+ picker:RemoveAction(action)
162
+
163
+ expect(action:IsPreferred()).toBe(false)
164
+ expect(picker:HasActions()).toBe(false)
165
+
166
+ controller.destroy()
167
+ end)
168
+
169
+ it("prefers an action that gained a score", function()
170
+ local controller = setup()
171
+ local picker = controller.newPicker()
172
+
173
+ local action = controller.newAction(-math.huge)
174
+ picker:AddAction(action)
175
+
176
+ action:SetScore(1)
177
+ picker:Update()
178
+
179
+ expect(action:IsPreferred()).toBe(true)
180
+
181
+ controller.destroy()
182
+ end)
183
+
184
+ it("holds a single preference across repeated updates", function()
185
+ local controller = setup()
186
+ local picker = controller.newPicker()
187
+
188
+ local action = controller.newAction(1)
189
+ picker:AddAction(action)
190
+
191
+ picker:Update()
192
+ picker:Update()
193
+ picker:RemoveAction(action)
194
+
195
+ expect(action:IsPreferred()).toBe(false)
196
+
197
+ controller.destroy()
198
+ end)
199
+
200
+ it("tolerates an action destroyed without being removed", function()
201
+ local controller = setup()
202
+ local picker = controller.newPicker()
203
+
204
+ local action = controller.newLooseAction(1)
205
+ picker:AddAction(action)
206
+ action:Destroy()
207
+
208
+ expect(function()
209
+ picker:Update()
210
+ end).never.toThrow()
211
+
212
+ controller.destroy()
213
+ end)
214
+ end)
215
+
216
+ describe("TouchButtonScoredActionPicker.AddAction", function()
217
+ it("ignores a repeated add of the same action", function()
218
+ local controller = setup()
219
+ local picker = controller.newPicker()
220
+
221
+ local action = controller.newAction(1)
222
+ picker:AddAction(action)
223
+ picker:AddAction(action)
224
+ picker:RemoveAction(action)
225
+
226
+ expect(action:IsPreferred()).toBe(false)
227
+ expect(picker:HasActions()).toBe(false)
228
+
229
+ controller.destroy()
230
+ end)
231
+ end)
232
+
233
+ describe("TouchButtonScoredActionPicker.RemoveAction", function()
234
+ it("leaves the other actions preferred", function()
235
+ local controller = setup()
236
+ local picker = controller.newPicker()
237
+
238
+ local removed = controller.newAction(1)
239
+ local remaining = controller.newAction(1)
240
+ picker:AddAction(removed)
241
+ picker:AddAction(remaining)
242
+
243
+ picker:RemoveAction(removed)
244
+
245
+ expect(removed:IsPreferred()).toBe(false)
246
+ expect(remaining:IsPreferred()).toBe(true)
247
+
248
+ controller.destroy()
249
+ end)
250
+ end)
251
+
252
+ describe("TouchButtonScoredActionPicker.HasActions", function()
253
+ it("is false for a new picker", function()
254
+ local controller = setup()
255
+ local picker = controller.newPicker()
256
+
257
+ expect(picker:HasActions()).toBe(false)
258
+
259
+ controller.destroy()
260
+ end)
261
+ end)
262
+
263
+ describe("TouchButtonScoredActionPicker.Destroy", function()
264
+ it("drops the preferences it was holding", function()
265
+ local controller = setup()
266
+ local picker = controller.newLoosePicker()
267
+
268
+ local action = controller.newAction(1)
269
+ picker:AddAction(action)
270
+ expect(action:IsPreferred()).toBe(true)
271
+
272
+ picker:Destroy()
273
+
274
+ expect(action:IsPreferred()).toBe(false)
275
+
276
+ controller.destroy()
277
+ end)
278
+ end)
@@ -0,0 +1,262 @@
1
+ --!strict
2
+ --[[
3
+ @class ScoredAction.spec.lua
4
+ ]]
5
+
6
+ local require = require(script.Parent.loader).load(script)
7
+
8
+ local Jest = require("Jest")
9
+ local Maid = require("Maid")
10
+ local ScoredAction = require("ScoredAction")
11
+ local ValueObject = require("ValueObject")
12
+
13
+ local describe = Jest.Globals.describe
14
+ local expect = Jest.Globals.expect
15
+ local it = Jest.Globals.it
16
+
17
+ type Controller = {
18
+ newAction: () -> ScoredAction.ScoredAction,
19
+ newLooseAction: () -> ScoredAction.ScoredAction,
20
+ newValue: (boolean) -> ValueObject.ValueObject<boolean>,
21
+ collect: (any) -> { any },
22
+ destroy: () -> (),
23
+ }
24
+
25
+ local function setup(): Controller
26
+ local maid = Maid.new()
27
+
28
+ local controller: Controller = {
29
+ newAction = function()
30
+ return maid:Add(ScoredAction.new()) :: any
31
+ end,
32
+
33
+ newLooseAction = function()
34
+ return ScoredAction.new()
35
+ end,
36
+
37
+ newValue = function(value: boolean)
38
+ return maid:Add(ValueObject.new(value, "boolean")) :: any
39
+ end,
40
+
41
+ collect = function(observable)
42
+ local results = {}
43
+ maid:GiveTask(observable:Subscribe(function(value)
44
+ table.insert(results, value)
45
+ end))
46
+ return results
47
+ end,
48
+
49
+ destroy = function()
50
+ maid:DoCleaning()
51
+ end,
52
+ }
53
+
54
+ return controller
55
+ end
56
+
57
+ describe("ScoredAction.new", function()
58
+ it("starts unscored, enabled, and unpreferred", function()
59
+ local controller = setup()
60
+ local action = controller.newAction()
61
+
62
+ expect(action:GetScore()).toBe(-math.huge)
63
+ expect(action:IsEnabled()).toBe(true)
64
+ expect(action:IsPreferred()).toBe(false)
65
+
66
+ controller.destroy()
67
+ end)
68
+ end)
69
+
70
+ describe("ScoredAction.SetScore", function()
71
+ it("round trips the score", function()
72
+ local controller = setup()
73
+ local action = controller.newAction()
74
+
75
+ action:SetScore(5)
76
+
77
+ expect(action:GetScore()).toBe(5)
78
+
79
+ controller.destroy()
80
+ end)
81
+
82
+ it("throws on a non-number score", function()
83
+ local controller = setup()
84
+ local action = controller.newAction()
85
+
86
+ expect(function()
87
+ (action :: any):SetScore("5")
88
+ end).toThrow()
89
+
90
+ controller.destroy()
91
+ end)
92
+ end)
93
+
94
+ describe("ScoredAction.SetIsEnabled", function()
95
+ it("sets the enabled state from a boolean", function()
96
+ local controller = setup()
97
+ local action = controller.newAction()
98
+
99
+ action:SetIsEnabled(false)
100
+
101
+ expect(action:IsEnabled()).toBe(false)
102
+
103
+ controller.destroy()
104
+ end)
105
+
106
+ it("tracks an observable source", function()
107
+ local controller = setup()
108
+ local action = controller.newAction()
109
+ local isEnabled = controller.newValue(true)
110
+
111
+ action:SetIsEnabled(isEnabled:Observe())
112
+ isEnabled.Value = false
113
+
114
+ expect(action:IsEnabled()).toBe(false)
115
+
116
+ controller.destroy()
117
+ end)
118
+
119
+ it("stops tracking the observable source once unmounted", function()
120
+ local controller = setup()
121
+ local action = controller.newAction()
122
+ local isEnabled = controller.newValue(true)
123
+
124
+ local unmount = action:SetIsEnabled(isEnabled:Observe())
125
+ unmount()
126
+ isEnabled.Value = false
127
+
128
+ expect(action:IsEnabled()).toBe(true)
129
+
130
+ controller.destroy()
131
+ end)
132
+
133
+ it("fires EnabledChanged with the new state", function()
134
+ local controller = setup()
135
+ local action = controller.newAction()
136
+
137
+ local states = controller.collect(action:ObserveIsEnabled())
138
+ action:SetIsEnabled(false)
139
+ action:SetIsEnabled(true)
140
+
141
+ expect(states).toEqual({ true, false, true })
142
+
143
+ controller.destroy()
144
+ end)
145
+ end)
146
+
147
+ describe("ScoredAction.ObserveIsEnabled", function()
148
+ it("emits the current state then each change", function()
149
+ local controller = setup()
150
+ local action = controller.newAction()
151
+
152
+ local states = controller.collect(action:ObserveIsEnabled())
153
+ action:SetIsEnabled(false)
154
+
155
+ expect(states).toEqual({ true, false })
156
+
157
+ controller.destroy()
158
+ end)
159
+ end)
160
+
161
+ describe("ScoredAction.PushPreferred", function()
162
+ it("is preferred while pushed", function()
163
+ local controller = setup()
164
+ local action = controller.newAction()
165
+
166
+ local pop = action:PushPreferred()
167
+ expect(action:IsPreferred()).toBe(true)
168
+
169
+ pop()
170
+ expect(action:IsPreferred()).toBe(false)
171
+
172
+ controller.destroy()
173
+ end)
174
+
175
+ it("stays preferred until every push is popped", function()
176
+ local controller = setup()
177
+ local action = controller.newAction()
178
+
179
+ local popFirst = action:PushPreferred()
180
+ local popSecond = action:PushPreferred()
181
+
182
+ popFirst()
183
+ expect(action:IsPreferred()).toBe(true)
184
+
185
+ popSecond()
186
+ expect(action:IsPreferred()).toBe(false)
187
+
188
+ controller.destroy()
189
+ end)
190
+
191
+ it("fires PreferredChanged with the new state", function()
192
+ local controller = setup()
193
+ local action = controller.newAction()
194
+
195
+ local states = {}
196
+ action.PreferredChanged:Connect(function(isPreferred)
197
+ table.insert(states, isPreferred)
198
+ end)
199
+
200
+ action:PushPreferred()()
201
+
202
+ expect(states).toEqual({ true, false })
203
+
204
+ controller.destroy()
205
+ end)
206
+
207
+ it("does not require a score", function()
208
+ local controller = setup()
209
+ local action = controller.newAction()
210
+
211
+ action:PushPreferred()
212
+
213
+ expect(action:GetScore()).toBe(-math.huge)
214
+ expect(action:IsPreferred()).toBe(true)
215
+
216
+ controller.destroy()
217
+ end)
218
+ end)
219
+
220
+ describe("ScoredAction.ObservePreferred", function()
221
+ it("emits the current state then each change", function()
222
+ local controller = setup()
223
+ local action = controller.newAction()
224
+
225
+ local states = controller.collect(action:ObservePreferred())
226
+ action:PushPreferred()()
227
+
228
+ expect(states).toEqual({ false, true, false })
229
+
230
+ controller.destroy()
231
+ end)
232
+ end)
233
+
234
+ describe("ScoredAction.Destroy", function()
235
+ it("fires Removing", function()
236
+ local controller = setup()
237
+ local action = controller.newLooseAction()
238
+
239
+ local fired = false
240
+ action.Removing:Connect(function()
241
+ fired = true
242
+ end)
243
+
244
+ action:Destroy()
245
+
246
+ expect(fired).toBe(true)
247
+
248
+ controller.destroy()
249
+ end)
250
+
251
+ it("clears the metatable so pickers can detect a stale action", function()
252
+ local controller = setup()
253
+ local action = controller.newLooseAction()
254
+ action:PushPreferred()
255
+
256
+ action:Destroy()
257
+
258
+ expect((action :: any).Destroy).toBeNil()
259
+
260
+ controller.destroy()
261
+ end)
262
+ end)
@@ -8,8 +8,6 @@
8
8
 
9
9
  local require = require(script.Parent.loader).load(script)
10
10
 
11
- local RunService = game:GetService("RunService")
12
-
13
11
  local InputKeyMapList = require("InputKeyMapList")
14
12
  local InputListScoreHelper = require("InputListScoreHelper")
15
13
  local Maid = require("Maid")
@@ -17,6 +15,7 @@ local Observable = require("Observable")
17
15
  local ScoredAction = require("ScoredAction")
18
16
  local ScoredActionPickerProvider = require("ScoredActionPickerProvider")
19
17
  local ServiceBag = require("ServiceBag")
18
+ local StepUtils = require("StepUtils")
20
19
  local ValueObject = require("ValueObject")
21
20
 
22
21
  local ScoredActionServiceClient = {}
@@ -51,7 +50,7 @@ end
51
50
  Starts the scored action service. Should be done via [ServiceBag].
52
51
  ]=]
53
52
  function ScoredActionServiceClient.Start(self: ScoredActionServiceClient)
54
- self._maid:GiveTask(RunService.Stepped:Connect(function()
53
+ self._maid:GiveTask(StepUtils.getSteppedSignal():Connect(function()
55
54
  -- TODO: Push to end of frame so we don't delay input by a frame?
56
55
  self._provider:Update()
57
56
  end))
@@ -107,6 +106,13 @@ end
107
106
  This MUTATES state of the scored action service whenever an object is emitted.
108
107
  :::
109
108
 
109
+ :::warning
110
+ Each emitted [ScoredAction] is owned by the subscription, and is destroyed when the next one is
111
+ emitted or when the subscription ends. A source that completes as soon as it emits (such as
112
+ [Rx.of]) therefore hands back an already-destroyed action. Keep the source open for as long as
113
+ the action should live.
114
+ :::
115
+
110
116
  @param scoreValue ValueObject<number>
111
117
  @return (source: Observable<InputKeyMapList>) -> Observable<ScoredAction>
112
118
  ]=]