@quenty/saveslot 2.3.0 → 2.4.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,12 @@
|
|
|
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
|
+
# [2.4.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.3.0...@quenty/saveslot@2.4.0) (2026-07-27)
|
|
7
|
+
|
|
8
|
+
### Features
|
|
9
|
+
|
|
10
|
+
- **saveslot:** run consumer callbacks before a slot is selected ([#758](https://github.com/Quenty/NevermoreEngine/issues/758)) ([f2d8c1b](https://github.com/Quenty/NevermoreEngine/commit/f2d8c1b5e5108ff85df98749a35a54706e77ae9a))
|
|
11
|
+
|
|
6
12
|
# [2.3.0](https://github.com/Quenty/NevermoreEngine/compare/@quenty/saveslot@2.2.3...@quenty/saveslot@2.3.0) (2026-07-27)
|
|
7
13
|
|
|
8
14
|
### Features
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@quenty/saveslot",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.4.0",
|
|
4
4
|
"description": "PlayerDataStoreService wrapper for save slots",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"Roblox",
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"publishConfig": {
|
|
58
58
|
"access": "public"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "69f0c5a4f30d047c838a2a7f568ce02450f5e813"
|
|
61
61
|
}
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
--!strict
|
|
2
|
+
--[[
|
|
3
|
+
Where a selection asks SaveSlotService whether it may proceed, and what it does with the answer. The
|
|
4
|
+
fan-out behind that answer is covered in SaveSlotService.spec.
|
|
5
|
+
|
|
6
|
+
@class HasSaveSlots.PreSelect.spec.lua
|
|
7
|
+
]]
|
|
8
|
+
local require = require(script.Parent.loader).load(script)
|
|
9
|
+
|
|
10
|
+
local DataStoreMock = require("DataStoreMock")
|
|
11
|
+
local Jest = require("Jest")
|
|
12
|
+
local PlayerDataStoreService = require("PlayerDataStoreService")
|
|
13
|
+
local PlayerMock = require("PlayerMock")
|
|
14
|
+
local Promise = require("Promise")
|
|
15
|
+
local PromiseTestUtils = require("PromiseTestUtils")
|
|
16
|
+
local ServiceBag = require("ServiceBag")
|
|
17
|
+
|
|
18
|
+
local Workspace = game:GetService("Workspace")
|
|
19
|
+
|
|
20
|
+
local afterEach = Jest.Globals.afterEach
|
|
21
|
+
local describe = Jest.Globals.describe
|
|
22
|
+
local expect = Jest.Globals.expect
|
|
23
|
+
local it = Jest.Globals.it
|
|
24
|
+
|
|
25
|
+
local FAKE_USER_ID = 424243
|
|
26
|
+
|
|
27
|
+
local activeContext: any = nil
|
|
28
|
+
|
|
29
|
+
afterEach(function()
|
|
30
|
+
if activeContext then
|
|
31
|
+
local context = activeContext
|
|
32
|
+
activeContext = nil
|
|
33
|
+
context.destroy()
|
|
34
|
+
end
|
|
35
|
+
end)
|
|
36
|
+
|
|
37
|
+
type Ask = { slotId: string, previousSlotId: string? }
|
|
38
|
+
|
|
39
|
+
local function setup()
|
|
40
|
+
local serviceBag = ServiceBag.new()
|
|
41
|
+
serviceBag:GetService(require("TeleportDataService"))
|
|
42
|
+
serviceBag:GetService(require("SaveSlotSharedDataStoreService"))
|
|
43
|
+
local playerDataStoreService: PlayerDataStoreService.PlayerDataStoreService =
|
|
44
|
+
serviceBag:GetService(PlayerDataStoreService) :: any
|
|
45
|
+
local binder = serviceBag:GetService(require("HasSaveSlots"))
|
|
46
|
+
serviceBag:Init()
|
|
47
|
+
playerDataStoreService:SetRobloxDataStore(DataStoreMock.new())
|
|
48
|
+
serviceBag:Start()
|
|
49
|
+
|
|
50
|
+
local fakePlayer = PlayerMock.new({ UserId = FAKE_USER_ID })
|
|
51
|
+
fakePlayer.Parent = Workspace
|
|
52
|
+
|
|
53
|
+
local hasSaveSlots = assert(binder:Bind(fakePlayer), "Failed to bind HasSaveSlots")
|
|
54
|
+
hasSaveSlots.MaxSlotCount.Value = 5
|
|
55
|
+
|
|
56
|
+
-- Booting SaveSlotService here would drag a second PlayerMockService into a DataModel another suite is
|
|
57
|
+
-- already using one in, so the question put to it is answered by the test.
|
|
58
|
+
local asks: { Ask } = {}
|
|
59
|
+
local verdict: any = nil
|
|
60
|
+
hasSaveSlots._promisePreSelectFromSaveSlotService = function(_self: any, slotId: string): any
|
|
61
|
+
table.insert(asks, { slotId = slotId, previousSlotId = hasSaveSlots.ActiveSlotId.Value })
|
|
62
|
+
|
|
63
|
+
return verdict or Promise.resolved(true)
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
local destroyed = false
|
|
67
|
+
local function destroy()
|
|
68
|
+
if destroyed then
|
|
69
|
+
return
|
|
70
|
+
end
|
|
71
|
+
destroyed = true
|
|
72
|
+
if activeContext and activeContext.destroy == destroy then
|
|
73
|
+
activeContext = nil
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
fakePlayer:Destroy()
|
|
77
|
+
serviceBag:Destroy()
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
local context = {
|
|
81
|
+
serviceBag = serviceBag,
|
|
82
|
+
fakePlayer = fakePlayer,
|
|
83
|
+
hasSaveSlots = hasSaveSlots,
|
|
84
|
+
asks = asks,
|
|
85
|
+
answerWith = function(promise: any)
|
|
86
|
+
verdict = promise
|
|
87
|
+
end,
|
|
88
|
+
destroy = destroy,
|
|
89
|
+
}
|
|
90
|
+
activeContext = context
|
|
91
|
+
|
|
92
|
+
return context
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
local function await(promise: any): any
|
|
96
|
+
assert(PromiseTestUtils.awaitSettled(promise, 10), "promise never settled")
|
|
97
|
+
local ok, value = promise:Yield()
|
|
98
|
+
assert(ok, `promise rejected: {tostring(value)}`)
|
|
99
|
+
return value
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
describe("HasSaveSlots pre-select", function()
|
|
103
|
+
it("asks before the slot becomes active", function()
|
|
104
|
+
local context = setup()
|
|
105
|
+
|
|
106
|
+
local slotId = await(context.hasSaveSlots:PromiseCreateSlot(1))
|
|
107
|
+
await(context.hasSaveSlots:PromiseSelectSlot(slotId))
|
|
108
|
+
|
|
109
|
+
expect(#context.asks).toEqual(1)
|
|
110
|
+
expect(context.asks[1].slotId).toEqual(slotId)
|
|
111
|
+
expect(context.asks[1].previousSlotId).toBeNil()
|
|
112
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(slotId)
|
|
113
|
+
|
|
114
|
+
context.destroy()
|
|
115
|
+
end)
|
|
116
|
+
|
|
117
|
+
it("reports the selection being replaced when switching slots", function()
|
|
118
|
+
local context = setup()
|
|
119
|
+
|
|
120
|
+
local firstId = await(context.hasSaveSlots:PromiseCreateSlot(1))
|
|
121
|
+
local secondId = await(context.hasSaveSlots:PromiseCreateSlot(2))
|
|
122
|
+
await(context.hasSaveSlots:PromiseSelectSlot(firstId))
|
|
123
|
+
await(context.hasSaveSlots:PromiseSelectSlot(secondId))
|
|
124
|
+
|
|
125
|
+
expect(#context.asks).toEqual(2)
|
|
126
|
+
expect(context.asks[2].slotId).toEqual(secondId)
|
|
127
|
+
expect(context.asks[2].previousSlotId).toEqual(firstId)
|
|
128
|
+
|
|
129
|
+
context.destroy()
|
|
130
|
+
end)
|
|
131
|
+
|
|
132
|
+
it("asks for a new slot and for an ephemeral slot alike", function()
|
|
133
|
+
local context = setup()
|
|
134
|
+
|
|
135
|
+
local newId = await(context.hasSaveSlots:PromiseSelectNewSaveSlot())
|
|
136
|
+
local ephemeralId = await(context.hasSaveSlots:PromiseSelectEphemeralSlot({ SlotName = "Throwaway" }))
|
|
137
|
+
|
|
138
|
+
expect(#context.asks).toEqual(2)
|
|
139
|
+
expect(context.asks[1].slotId).toEqual(newId)
|
|
140
|
+
expect(context.asks[2].slotId).toEqual(ephemeralId)
|
|
141
|
+
|
|
142
|
+
context.destroy()
|
|
143
|
+
end)
|
|
144
|
+
|
|
145
|
+
it("does not ask when the slot is already active", function()
|
|
146
|
+
local context = setup()
|
|
147
|
+
|
|
148
|
+
local slotId = await(context.hasSaveSlots:PromiseCreateSlot(1))
|
|
149
|
+
await(context.hasSaveSlots:PromiseSelectSlot(slotId))
|
|
150
|
+
await(context.hasSaveSlots:PromiseSelectSlot(slotId))
|
|
151
|
+
|
|
152
|
+
expect(#context.asks).toEqual(1)
|
|
153
|
+
|
|
154
|
+
context.destroy()
|
|
155
|
+
end)
|
|
156
|
+
|
|
157
|
+
it("holds the selection until the answer settles", function()
|
|
158
|
+
local context = setup()
|
|
159
|
+
|
|
160
|
+
local gate = Promise.new()
|
|
161
|
+
context.answerWith(gate)
|
|
162
|
+
|
|
163
|
+
local slotId = await(context.hasSaveSlots:PromiseCreateSlot(1))
|
|
164
|
+
local selection = context.hasSaveSlots:PromiseSelectSlot(slotId)
|
|
165
|
+
|
|
166
|
+
expect(PromiseTestUtils.awaitSettled(selection, 0.5)).toEqual(false)
|
|
167
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
|
|
168
|
+
|
|
169
|
+
gate:Resolve(true)
|
|
170
|
+
|
|
171
|
+
await(selection)
|
|
172
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(slotId)
|
|
173
|
+
|
|
174
|
+
context.destroy()
|
|
175
|
+
end)
|
|
176
|
+
|
|
177
|
+
it("rejects the selection and leaves the active slot alone when refused", function()
|
|
178
|
+
local context = setup()
|
|
179
|
+
|
|
180
|
+
context.answerWith(Promise.resolved(false))
|
|
181
|
+
|
|
182
|
+
local slotId = await(context.hasSaveSlots:PromiseCreateSlot(1))
|
|
183
|
+
local selection = context.hasSaveSlots:PromiseSelectSlot(slotId)
|
|
184
|
+
|
|
185
|
+
assert(PromiseTestUtils.awaitSettled(selection, 10), "selection never settled")
|
|
186
|
+
expect(selection:IsRejected()).toEqual(true)
|
|
187
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toBeNil()
|
|
188
|
+
|
|
189
|
+
context.destroy()
|
|
190
|
+
end)
|
|
191
|
+
|
|
192
|
+
it("leaves the previous selection standing when a switch is refused", function()
|
|
193
|
+
local context = setup()
|
|
194
|
+
|
|
195
|
+
local firstId = await(context.hasSaveSlots:PromiseCreateSlot(1))
|
|
196
|
+
local secondId = await(context.hasSaveSlots:PromiseCreateSlot(2))
|
|
197
|
+
await(context.hasSaveSlots:PromiseSelectSlot(firstId))
|
|
198
|
+
|
|
199
|
+
context.answerWith(Promise.resolved(false))
|
|
200
|
+
local selection = context.hasSaveSlots:PromiseSelectSlot(secondId)
|
|
201
|
+
|
|
202
|
+
assert(PromiseTestUtils.awaitSettled(selection, 10), "selection never settled")
|
|
203
|
+
expect(selection:IsRejected()).toEqual(true)
|
|
204
|
+
expect(context.hasSaveSlots.ActiveSlotId.Value).toEqual(firstId)
|
|
205
|
+
|
|
206
|
+
context.destroy()
|
|
207
|
+
end)
|
|
208
|
+
end)
|
|
@@ -173,6 +173,22 @@ function HasSaveSlots.PromiseHasSlot(self: HasSaveSlots, slotId: SaveSlotData.Sl
|
|
|
173
173
|
end)
|
|
174
174
|
end
|
|
175
175
|
|
|
176
|
+
-- SaveSlotService owns the callbacks and runs them. Reached through the module instance rather than by
|
|
177
|
+
-- name, because `require("SaveSlotService")` from here -- at any depth, even inside this function -- is a
|
|
178
|
+
-- cyclic module dependency: SaveSlotService requires this module. A binder bound without the service in
|
|
179
|
+
-- its bag has nothing to ask, and allows the selection.
|
|
180
|
+
function HasSaveSlots._promisePreSelectFromSaveSlotService(
|
|
181
|
+
self: HasSaveSlots,
|
|
182
|
+
slotId: SaveSlotData.SlotId
|
|
183
|
+
): Promise.Promise<boolean>
|
|
184
|
+
local serviceModule = script.Parent.Parent:FindFirstChild("SaveSlotService")
|
|
185
|
+
if not serviceModule or not self._serviceBag:HasService(serviceModule) then
|
|
186
|
+
return (Promise :: any).resolved(true)
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
return self._serviceBag:GetService(serviceModule):PromisePreSelect(self._obj, slotId, self.ActiveSlotId.Value)
|
|
190
|
+
end
|
|
191
|
+
|
|
176
192
|
--[=[
|
|
177
193
|
Selects the slot with the given ID
|
|
178
194
|
]=]
|
|
@@ -192,20 +208,28 @@ function HasSaveSlots.PromiseSelectSlot(self: HasSaveSlots, slotId: SaveSlotData
|
|
|
192
208
|
SaveSlotData.LastPlayedTime:Set(slot, os.time())
|
|
193
209
|
end
|
|
194
210
|
|
|
211
|
+
local function promiseSetSlot()
|
|
212
|
+
return self:_promisePreSelectFromSaveSlotService(slotId):Then(function(allowed: boolean)
|
|
213
|
+
if not allowed then
|
|
214
|
+
return (Promise :: any).rejected(`Slot \{{slotId}\} refused by a pre-select callback`)
|
|
215
|
+
end
|
|
216
|
+
|
|
217
|
+
return setSlot()
|
|
218
|
+
end)
|
|
219
|
+
end
|
|
220
|
+
|
|
195
221
|
-- Initialize or save and switch
|
|
196
222
|
if self.ActiveSlotId.Value == nil then
|
|
197
|
-
|
|
198
|
-
return
|
|
223
|
+
return promiseSetSlot()
|
|
199
224
|
end
|
|
200
225
|
|
|
201
226
|
-- Leaving an ephemeral slot has nothing to persist, so switch without a datastore flush (the flush
|
|
202
227
|
-- exists to save the outgoing slot's progress, and an ephemeral slot has none).
|
|
203
228
|
if self:_isEphemeral(self.ActiveSlotId.Value) then
|
|
204
|
-
|
|
205
|
-
return
|
|
229
|
+
return promiseSetSlot()
|
|
206
230
|
end
|
|
207
231
|
|
|
208
|
-
return self._dataStore:Save():Then(
|
|
232
|
+
return self._dataStore:Save():Then(promiseSetSlot)
|
|
209
233
|
end)
|
|
210
234
|
end
|
|
211
235
|
|
|
@@ -14,7 +14,9 @@ local HasSaveSlotsData = require("HasSaveSlotsData")
|
|
|
14
14
|
local Maid = require("Maid")
|
|
15
15
|
local Observable = require("Observable")
|
|
16
16
|
local ObservableMap = require("ObservableMap")
|
|
17
|
+
local ObservableSet = require("ObservableSet")
|
|
17
18
|
local Promise = require("Promise")
|
|
19
|
+
local PromiseUtils = require("PromiseUtils")
|
|
18
20
|
local Remoting = require("Remoting")
|
|
19
21
|
local RxBrioUtils = require("RxBrioUtils")
|
|
20
22
|
local SaveSlotCodeUtils = require("SaveSlotCodeUtils")
|
|
@@ -25,6 +27,14 @@ local SaveSlotSharedDataStoreService = require("SaveSlotSharedDataStoreService")
|
|
|
25
27
|
local ServiceBag = require("ServiceBag")
|
|
26
28
|
local TeleportDataService = require("TeleportDataService")
|
|
27
29
|
|
|
30
|
+
-- Called with (player, slotId, previousSlotId) before slotId becomes the active slot, while the previous
|
|
31
|
+
-- selection is still in place. See RegisterPreSelectCallback.
|
|
32
|
+
export type PreSelectCallback = (
|
|
33
|
+
Player,
|
|
34
|
+
SaveSlotData.SlotId,
|
|
35
|
+
SaveSlotData.SlotId?
|
|
36
|
+
) -> (Promise.Promise<boolean?> | boolean)?
|
|
37
|
+
|
|
28
38
|
local SaveSlotService = {}
|
|
29
39
|
SaveSlotService.ServiceName = "SaveSlotService"
|
|
30
40
|
|
|
@@ -36,6 +46,9 @@ export type SaveSlotService = typeof(setmetatable(
|
|
|
36
46
|
_selectionRequired: boolean,
|
|
37
47
|
_maxSlotCount: number,
|
|
38
48
|
_defaultSummaryProviders: ObservableMap.ObservableMap<string, HasSaveSlots.SummaryProvider>,
|
|
49
|
+
-- ObservableSet<PreSelectCallback>; erased because the generic parameterized by a
|
|
50
|
+
-- function type blows up inference.
|
|
51
|
+
_preSelectCallbacks: any,
|
|
39
52
|
_remoting: any,
|
|
40
53
|
_teleportDataService: any,
|
|
41
54
|
_codeGenerator: SaveSlotCodeUtils.CodeGenerator?,
|
|
@@ -66,6 +79,7 @@ function SaveSlotService.Init(self: SaveSlotService, serviceBag: ServiceBag.Serv
|
|
|
66
79
|
self._selectionRequired = false
|
|
67
80
|
self._maxSlotCount = 1
|
|
68
81
|
self._defaultSummaryProviders = self._maid:Add(ObservableMap.new())
|
|
82
|
+
self._preSelectCallbacks = self._maid:Add(ObservableSet.new())
|
|
69
83
|
|
|
70
84
|
self._remoting = self._maid:Add(Remoting.Server.new(ReplicatedStorage, "SaveSlotService"))
|
|
71
85
|
|
|
@@ -290,6 +304,74 @@ function SaveSlotService.RegisterDefaultSummaryProvider(
|
|
|
290
304
|
return self._defaultSummaryProviders:Set(name, provider :: any)
|
|
291
305
|
end
|
|
292
306
|
|
|
307
|
+
--[=[
|
|
308
|
+
Registers a callback to run for every player immediately before a slot becomes active, whatever
|
|
309
|
+
selected it. It runs with the previous selection still in place, and what it returns decides what
|
|
310
|
+
happens next:
|
|
311
|
+
|
|
312
|
+
* nothing (or `true`) -- allow the selection
|
|
313
|
+
* `false` -- refuse it; the selection rejects and the active slot is left alone
|
|
314
|
+
* a promise -- hold the selection open until it settles, refusing if it resolves `false`
|
|
315
|
+
|
|
316
|
+
An error or a rejection is warned about and allows the selection: only a stated `false` refuses. There
|
|
317
|
+
is no timeout, so keep the work bounded.
|
|
318
|
+
|
|
319
|
+
@param callback PreSelectCallback
|
|
320
|
+
@return () -> () -- Removes the callback
|
|
321
|
+
]=]
|
|
322
|
+
function SaveSlotService.RegisterPreSelectCallback(self: SaveSlotService, callback: PreSelectCallback): () -> ()
|
|
323
|
+
assert(type(callback) == "function", "Bad callback")
|
|
324
|
+
|
|
325
|
+
return self._preSelectCallbacks:Add(callback)
|
|
326
|
+
end
|
|
327
|
+
|
|
328
|
+
--[=[
|
|
329
|
+
Runs every registered pre-select callback for a slot about to become active, resolving with whether the
|
|
330
|
+
selection may proceed once the ones that returned a promise have settled. Called by [HasSaveSlots] as
|
|
331
|
+
each selection commits.
|
|
332
|
+
|
|
333
|
+
Every callback runs, even once one has refused: they are independent, and one that only wanted to
|
|
334
|
+
settle state must not be skipped because an unrelated one said no.
|
|
335
|
+
|
|
336
|
+
@param player Player
|
|
337
|
+
@param slotId SlotId
|
|
338
|
+
@param previousSlotId SlotId?
|
|
339
|
+
@return Promise<boolean> -- False when any callback refused
|
|
340
|
+
]=]
|
|
341
|
+
function SaveSlotService.PromisePreSelect(
|
|
342
|
+
self: SaveSlotService,
|
|
343
|
+
player: Player,
|
|
344
|
+
slotId: SaveSlotData.SlotId,
|
|
345
|
+
previousSlotId: SaveSlotData.SlotId?
|
|
346
|
+
): Promise.Promise<boolean>
|
|
347
|
+
local promises = {}
|
|
348
|
+
local refused = false
|
|
349
|
+
|
|
350
|
+
for _, callback in self._preSelectCallbacks:GetList() do
|
|
351
|
+
local ok, result = pcall(callback, player, slotId, previousSlotId)
|
|
352
|
+
if not ok then
|
|
353
|
+
warn(`[SaveSlotService] - Pre-select callback errored: {tostring(result)}`)
|
|
354
|
+
elseif Promise.isPromise(result) then
|
|
355
|
+
table.insert(
|
|
356
|
+
promises,
|
|
357
|
+
(result :: any):Then(function(allowed: boolean?)
|
|
358
|
+
if allowed == false then
|
|
359
|
+
refused = true
|
|
360
|
+
end
|
|
361
|
+
end, function(err: any)
|
|
362
|
+
warn(`[SaveSlotService] - Pre-select callback rejected: {tostring(err)}`)
|
|
363
|
+
end)
|
|
364
|
+
)
|
|
365
|
+
elseif result == false then
|
|
366
|
+
refused = true
|
|
367
|
+
end
|
|
368
|
+
end
|
|
369
|
+
|
|
370
|
+
return (PromiseUtils.all(promises) :: any):Then(function()
|
|
371
|
+
return not refused
|
|
372
|
+
end)
|
|
373
|
+
end
|
|
374
|
+
|
|
293
375
|
--[=[
|
|
294
376
|
Observes the [DataStoreStage] for the player's active slot as a [Brio]
|
|
295
377
|
]=]
|
|
@@ -14,6 +14,7 @@ local Jest = require("Jest")
|
|
|
14
14
|
local Maid = require("Maid")
|
|
15
15
|
local PlayerDataStoreService = require("PlayerDataStoreService")
|
|
16
16
|
local PlayerMock = require("PlayerMock")
|
|
17
|
+
local Promise = require("Promise")
|
|
17
18
|
local PromiseTestUtils = require("PromiseTestUtils")
|
|
18
19
|
local SaveSlotConstants = require("SaveSlotConstants")
|
|
19
20
|
local SaveSlotService = require("SaveSlotService")
|
|
@@ -87,6 +88,192 @@ describe("SaveSlotService.GetExplicitSelectionRequired", function()
|
|
|
87
88
|
end)
|
|
88
89
|
end)
|
|
89
90
|
|
|
91
|
+
describe("SaveSlotService:RegisterPreSelectCallback", function()
|
|
92
|
+
it("rejects a non-function callback", function()
|
|
93
|
+
local controller = setup()
|
|
94
|
+
|
|
95
|
+
expect(function()
|
|
96
|
+
controller.saveSlotService:RegisterPreSelectCallback(nil :: any)
|
|
97
|
+
end).toThrow("Bad callback")
|
|
98
|
+
|
|
99
|
+
controller:destroy()
|
|
100
|
+
end)
|
|
101
|
+
|
|
102
|
+
it("accepts registrations after Start", function()
|
|
103
|
+
local controller = setup()
|
|
104
|
+
controller.serviceBag:Start()
|
|
105
|
+
local player = controller.fakePlayer()
|
|
106
|
+
|
|
107
|
+
local ran = 0
|
|
108
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
109
|
+
ran += 1
|
|
110
|
+
return nil
|
|
111
|
+
end)
|
|
112
|
+
|
|
113
|
+
controller.awaitBool(controller.saveSlotService:PromisePreSelect(player, "slot-1"))
|
|
114
|
+
expect(ran).toEqual(1)
|
|
115
|
+
|
|
116
|
+
controller:destroy()
|
|
117
|
+
end)
|
|
118
|
+
|
|
119
|
+
it("stops running a callback once removed", function()
|
|
120
|
+
local controller = setup()
|
|
121
|
+
local player = controller.fakePlayer()
|
|
122
|
+
|
|
123
|
+
local ran = 0
|
|
124
|
+
local remove = controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
125
|
+
ran += 1
|
|
126
|
+
return nil
|
|
127
|
+
end)
|
|
128
|
+
|
|
129
|
+
controller.awaitBool(controller.saveSlotService:PromisePreSelect(player, "slot-1"))
|
|
130
|
+
expect(ran).toEqual(1)
|
|
131
|
+
|
|
132
|
+
remove()
|
|
133
|
+
|
|
134
|
+
controller.awaitBool(controller.saveSlotService:PromisePreSelect(player, "slot-1"))
|
|
135
|
+
expect(ran).toEqual(1)
|
|
136
|
+
|
|
137
|
+
controller:destroy()
|
|
138
|
+
end)
|
|
139
|
+
end)
|
|
140
|
+
|
|
141
|
+
describe("SaveSlotService:PromisePreSelect", function()
|
|
142
|
+
it("allows the selection when nothing is registered", function()
|
|
143
|
+
local controller = setup()
|
|
144
|
+
|
|
145
|
+
expect(controller.awaitBool(controller.saveSlotService:PromisePreSelect(controller.fakePlayer(), "slot-1"))).toEqual(
|
|
146
|
+
true
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
controller:destroy()
|
|
150
|
+
end)
|
|
151
|
+
|
|
152
|
+
it("hands each callback the player and the selection it is replacing", function()
|
|
153
|
+
local controller = setup()
|
|
154
|
+
local player = controller.fakePlayer()
|
|
155
|
+
|
|
156
|
+
local calls: { any } = {}
|
|
157
|
+
controller.saveSlotService:RegisterPreSelectCallback(function(forPlayer, slotId, previousSlotId)
|
|
158
|
+
table.insert(calls, { player = forPlayer, slotId = slotId, previousSlotId = previousSlotId })
|
|
159
|
+
return nil
|
|
160
|
+
end)
|
|
161
|
+
|
|
162
|
+
controller.awaitBool(controller.saveSlotService:PromisePreSelect(player, "slot-2", "slot-1"))
|
|
163
|
+
|
|
164
|
+
expect(#calls).toEqual(1)
|
|
165
|
+
expect(calls[1].player).toEqual(player)
|
|
166
|
+
expect(calls[1].slotId).toEqual("slot-2")
|
|
167
|
+
expect(calls[1].previousSlotId).toEqual("slot-1")
|
|
168
|
+
|
|
169
|
+
controller:destroy()
|
|
170
|
+
end)
|
|
171
|
+
|
|
172
|
+
it("refuses when a callback returns false", function()
|
|
173
|
+
local controller = setup()
|
|
174
|
+
|
|
175
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
176
|
+
return false
|
|
177
|
+
end)
|
|
178
|
+
|
|
179
|
+
expect(controller.awaitBool(controller.saveSlotService:PromisePreSelect(controller.fakePlayer(), "slot-1"))).toEqual(
|
|
180
|
+
false
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
controller:destroy()
|
|
184
|
+
end)
|
|
185
|
+
|
|
186
|
+
it("refuses when a returned promise resolves false", function()
|
|
187
|
+
local controller = setup()
|
|
188
|
+
|
|
189
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
190
|
+
return Promise.resolved(false)
|
|
191
|
+
end)
|
|
192
|
+
|
|
193
|
+
expect(controller.awaitBool(controller.saveSlotService:PromisePreSelect(controller.fakePlayer(), "slot-1"))).toEqual(
|
|
194
|
+
false
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
controller:destroy()
|
|
198
|
+
end)
|
|
199
|
+
|
|
200
|
+
it("waits on every returned promise before answering", function()
|
|
201
|
+
local controller = setup()
|
|
202
|
+
|
|
203
|
+
local first = Promise.new()
|
|
204
|
+
local second = Promise.new()
|
|
205
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
206
|
+
return first
|
|
207
|
+
end)
|
|
208
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
209
|
+
return second
|
|
210
|
+
end)
|
|
211
|
+
|
|
212
|
+
local verdict = controller.saveSlotService:PromisePreSelect(controller.fakePlayer(), "slot-1")
|
|
213
|
+
|
|
214
|
+
first:Resolve()
|
|
215
|
+
expect(PromiseTestUtils.awaitSettled(verdict, 0.5)).toEqual(false)
|
|
216
|
+
|
|
217
|
+
second:Resolve()
|
|
218
|
+
expect(controller.awaitBool(verdict)).toEqual(true)
|
|
219
|
+
|
|
220
|
+
controller:destroy()
|
|
221
|
+
end)
|
|
222
|
+
|
|
223
|
+
it("runs every callback even once one has refused", function()
|
|
224
|
+
local controller = setup()
|
|
225
|
+
|
|
226
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
227
|
+
return false
|
|
228
|
+
end)
|
|
229
|
+
local ranAfter = 0
|
|
230
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
231
|
+
ranAfter += 1
|
|
232
|
+
return nil
|
|
233
|
+
end)
|
|
234
|
+
|
|
235
|
+
controller.awaitBool(controller.saveSlotService:PromisePreSelect(controller.fakePlayer(), "slot-1"))
|
|
236
|
+
|
|
237
|
+
expect(ranAfter).toEqual(1)
|
|
238
|
+
|
|
239
|
+
controller:destroy()
|
|
240
|
+
end)
|
|
241
|
+
|
|
242
|
+
it("allows the selection when a callback errors rather than treating it as a refusal", function()
|
|
243
|
+
local controller = setup()
|
|
244
|
+
|
|
245
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
246
|
+
error("callback blew up")
|
|
247
|
+
end)
|
|
248
|
+
local ranAfter = 0
|
|
249
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
250
|
+
ranAfter += 1
|
|
251
|
+
return nil
|
|
252
|
+
end)
|
|
253
|
+
|
|
254
|
+
expect(controller.awaitBool(controller.saveSlotService:PromisePreSelect(controller.fakePlayer(), "slot-1"))).toEqual(
|
|
255
|
+
true
|
|
256
|
+
)
|
|
257
|
+
expect(ranAfter).toEqual(1)
|
|
258
|
+
|
|
259
|
+
controller:destroy()
|
|
260
|
+
end)
|
|
261
|
+
|
|
262
|
+
it("allows the selection when a returned promise rejects", function()
|
|
263
|
+
local controller = setup()
|
|
264
|
+
|
|
265
|
+
controller.saveSlotService:RegisterPreSelectCallback(function()
|
|
266
|
+
return Promise.rejected("work blew up")
|
|
267
|
+
end)
|
|
268
|
+
|
|
269
|
+
expect(controller.awaitBool(controller.saveSlotService:PromisePreSelect(controller.fakePlayer(), "slot-1"))).toEqual(
|
|
270
|
+
true
|
|
271
|
+
)
|
|
272
|
+
|
|
273
|
+
controller:destroy()
|
|
274
|
+
end)
|
|
275
|
+
end)
|
|
276
|
+
|
|
90
277
|
describe("SaveSlotService configuration guards", function()
|
|
91
278
|
it("should reject RequireExplicitSelection after Start", function()
|
|
92
279
|
local controller = setup()
|