Open MattSchrubb opened 4 years ago
I actually did it myself. Only updating the :Set() functions and the :_Update() functions.
function DataStore:_Update(dontCallOnUpdate, prevVal)
if not dontCallOnUpdate then
for _,callback in pairs(self.callbacks) do
callback(self.value, prevVal, self)
end
end
self.haveValue = true
self.valueUpdated = true
end
function DataStore:Set(value, _dontCallOnUpdate)
local prevValue = self.value
self.value = clone(value)
self:_Update(_dontCallOnUpdate, prevValue)
end
function DataStore:Update(updateFunc)
local prevVal = self.value
self.value = updateFunc(self.value)
self:_Update(nil, prevVal)
end
function CombinedDataStore:Set(value, dontCallOnUpdate)
local tableResult = self.combinedStore:GetTable({})
local prevVal = tableResult[self.combinedName]
tableResult[self.combinedName] = value
self.combinedStore:Set(tableResult, dontCallOnUpdate)
self:_Update(dontCallOnUpdate, prevVal)
end
function CombinedDataStore:Update(updateFunc)
local prevVal = self:Get()
self:Set(updateFunc(self:Get()))
self:_Update(nil, prevVal)
end
function CombinedDataStore:_Update(dontCallOnUpdate, prevVal)
if not dontCallOnUpdate then
for _, callback in pairs(self.onUpdateCallbacks or {}) do
callback(self:Get(), prevVal, self)
end
end
self.combinedStore:_Update(true)
end
Putting the previous value as the second argument is the most ergonomic, but it's a breaking change--we pass the data store as the second parameter. That seems like a mistake in hindsight, but I wonder if anyone relies on that behavior?
I haven't found a need to use the data store passed, but indeed it would be a breaking change for any game that uses that functionality.
Putting the previous value as the second argument is the most ergonomic, but it's a breaking change--we pass the data store as the second parameter. That seems like a mistake in hindsight, but I wonder if anyone relies on that behavior?
Can you add a function that would be able to do this? Or add a way to get the previous value?
I don't think it's that much of an issue to just have it as a last parameter, and if you don't need to use the data store argument to just _
it out.
dataStore:OnUpdate(function(newValue, _, oldValue)
Is this something that is planned on being added or are we supposed to just put it in ourselves? And if we have to put it in ourselves, is there a way that we can override the functions instead of needing to edit the code for every update?
You can submit a pull request :)
Using :OnUpdate() is great to check when a "key"'s value is changed, but what if we want to know what the previous value was? ie. To check how much the value changed when using Increment.
I'm not sure if there is a way to do this, but if not, then adding a way to access previously stored data easily would be the next step.