Skip to content

Commit

Permalink
feat: Add :SetValue() behavior to cleanup past mounted sub and return…
Browse files Browse the repository at this point in the history
… a function to unset the value
  • Loading branch information
Quenty committed Dec 3, 2024
1 parent 8544c2b commit 03fd05a
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions src/valueobject/src/Shared/ValueObject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ValueObject.ClassName = "ValueObject"
function ValueObject.new(baseValue, checkType)
local self = setmetatable({
_value = baseValue;
_default = baseValue;
_checkType = checkType;
}, ValueObject)

Expand Down Expand Up @@ -111,7 +112,7 @@ function ValueObject:Mount(value)
self:_cleanupLastMountedSub()

local sub = observable:Subscribe(function(...)
self:SetValue(...)
ValueObject._applyValue(self, ...)
end)

rawset(self, "_lastMountedSub", sub)
Expand All @@ -124,7 +125,7 @@ function ValueObject:Mount(value)
else
self:_cleanupLastMountedSub()

self:SetValue(value)
ValueObject._applyValue(self, value)

return EMPTY_FUNCTION
end
Expand Down Expand Up @@ -234,8 +235,21 @@ end
@param value T
@param ... any -- Additional args. Can be used to pass event changing state args with value
@return () -> () -- Cleanup
]=]
function ValueObject:SetValue(value, ...)
self:_cleanupLastMountedSub()

ValueObject._applyValue(self, value, ...)

return function()
if rawget(self, "_value") == value then
ValueObject._applyValue(self, rawget(self, "_default"))
end
end
end

function ValueObject:_applyValue(value, ...)
local previous = rawget(self, "_value")
local checkType = rawget(self, "_checkType")

Expand Down Expand Up @@ -298,7 +312,7 @@ end
function ValueObject:__newindex(index, value)
if index == "Value" then
-- Avoid deoptimization
ValueObject.SetValue(self, value)
ValueObject._applyValue(self, value)
elseif index == "LastEventContext" or ValueObject[index] then
error(string.format("%q cannot be set in ValueObject", tostring(index)))
else
Expand Down

0 comments on commit 03fd05a

Please sign in to comment.