Skip to content

Commit

Permalink
fix: Ensure default value is returned in certain nil scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
Quenty committed Sep 17, 2023
1 parent fe2f05c commit f9032d0
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions src/datastore/src/Server/Modules/DataStoreStage.lua
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,16 @@ end
end)
```
@param defaultValue any
@return Promise<any>
]=]
function DataStoreStage:LoadAll()
function DataStoreStage:LoadAll(defaultValue)
return self:PromiseViewUpToDate():Then(function()
return self._viewSnapshot
if self._viewSnapshot == nil then
return defaultValue
else
return self._viewSnapshot
end
end)
end

Expand Down Expand Up @@ -250,10 +255,18 @@ function DataStoreStage:Observe(key, defaultValue)
:Then(function()
-- Only connect once loaded
maid:GiveTask(self.Changed:Connect(function(viewSnapshot)
sub:Fire(viewSnapshot)
if viewSnapshot == nil then
sub:Fire(defaultValue)
else
sub:Fire(viewSnapshot)
end
end))

sub:Fire(self._viewSnapshot)
if self._viewSnapshot == nil then
sub:Fire(defaultValue)
else
sub:Fire(self._viewSnapshot)
end
end, function(...)
sub:Fail(...)
end)
Expand All @@ -265,7 +278,13 @@ function DataStoreStage:Observe(key, defaultValue)
return Observable.new(function(sub)
local maid = Maid.new()

maid:GiveTask(self._keySubscriptions:Observe(key):Subscribe(sub:GetFireFailComplete()))
maid:GiveTask(self._keySubscriptions:Observe(key):Subscribe(function(value)
if value == nil then
sub:Fire(defaultValue)
else
sub:Fire(value)
end
end), sub:GetFailComplete())

-- Load initially
maid:GivePromise(self:Load(key, defaultValue))
Expand Down

0 comments on commit f9032d0

Please sign in to comment.