From f9032d0d465231f86f55a60c6465183731567b4c Mon Sep 17 00:00:00 2001 From: James Onnen Date: Sat, 16 Sep 2023 17:08:17 -0700 Subject: [PATCH] fix: Ensure default value is returned in certain nil scenarios --- .../src/Server/Modules/DataStoreStage.lua | 29 +++++++++++++++---- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/datastore/src/Server/Modules/DataStoreStage.lua b/src/datastore/src/Server/Modules/DataStoreStage.lua index 7c78e8788b..5a194154fd 100644 --- a/src/datastore/src/Server/Modules/DataStoreStage.lua +++ b/src/datastore/src/Server/Modules/DataStoreStage.lua @@ -141,11 +141,16 @@ end end) ``` + @param defaultValue any @return Promise ]=] -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 @@ -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) @@ -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))