Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure default value is returned in certain nil scenarios #410

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading