diff --git a/src/attributeutils/src/Shared/AttributeValue.lua b/src/attributeutils/src/Shared/AttributeValue.lua index fa32c7ad1d..530375be9e 100644 --- a/src/attributeutils/src/Shared/AttributeValue.lua +++ b/src/attributeutils/src/Shared/AttributeValue.lua @@ -1,21 +1,21 @@ --!nonstrict --[=[ - Allows access to an attribute like a ValueObject. + Allows access to an attribute like a ValueObject. - ```lua - local attributeValue = AttributeValue.new(workspace, "Version", "1.0.0") - print(attributeValue.Value) --> 1.0.0 - print(workspace:GetAttribute("version")) --> 1.0.0 + ```lua + local attributeValue = AttributeValue.new(workspace, "Version", "1.0.0") + print(attributeValue.Value) --> 1.0.0 + print(workspace:GetAttribute("version")) --> 1.0.0 - attributeValue.Changed:Connect(function() - print(attributeValue.Value) - end) + attributeValue.Changed:Connect(function() + print(attributeValue.Value) + end) - workspace:SetAttribute("1.1.0") --> 1.1.0 - attributeValue.Value = "1.2.0" --> 1.2.0 - ``` + workspace:SetAttribute("1.1.0") --> 1.1.0 + attributeValue.Value = "1.2.0" --> 1.2.0 + ``` - @class AttributeValue + @class AttributeValue ]=] local require = require(script.Parent.loader).load(script) @@ -44,13 +44,13 @@ export type AttributeValue = typeof(setmetatable( )) --[=[ - Constructs a new AttributeValue. If a defaultValue that is not nil - is defined, then this value will be set on the Roblox object. + Constructs a new AttributeValue. If a defaultValue that is not nil + is defined, then this value will be set on the Roblox object. - @param object Instance - @param attributeName string - @param defaultValue T - @return AttributeValue + @param object Instance + @param attributeName string + @param defaultValue T + @return AttributeValue ]=] function AttributeValue.new(object: Instance, attributeName: string, defaultValue: T): AttributeValue assert(typeof(object) == "Instance", "Bad object") @@ -70,10 +70,29 @@ function AttributeValue.new(object: Instance, attributeName: string, defaultV end --[=[ - Handles observing the value conditionalli + Allows you to set an attribute, and provides a cleanup - @param condition function | nil - @return Observable> + @param value T + @return () -> () -- Cleanup +]=] +function AttributeValue.SetValue(self: AttributeValue, value: T) + self._object:SetAttribute(rawget(self :: any, "_attributeName") :: string, value) + + return function() + if self._object:GetAttribute(rawget(self :: any, "_attributeName") :: string) == value then + self._object:SetAttribute( + rawget(self :: any, "_attributeName") :: string, + rawget(self :: any, "_default") :: any + ) + end + end +end + +--[=[ + Handles observing the value conditionalli + + @param condition function | nil + @return Observable> ]=] function AttributeValue.ObserveBrio( self: AttributeValue, @@ -83,25 +102,25 @@ function AttributeValue.ObserveBrio( end --[=[ - Observes an attribute on an instance. - @return Observable + Observes an attribute on an instance. + @return Observable ]=] function AttributeValue.Observe(self: AttributeValue): Observable.Observable return RxAttributeUtils.observeAttribute(self._object, self._attributeName, rawget(self :: any, "_defaultValue")) end --[=[ - The current property of the Attribute. Can be assigned to to write - the attribute. - @prop Value T - @within AttributeValue + The current property of the Attribute. Can be assigned to to write + the attribute. + @prop Value T + @within AttributeValue ]=] --[=[ - Signal that fires when the attribute changes - @readonly - @prop Changed Signal<()> - @within AttributeValue + Signal that fires when the attribute changes + @readonly + @prop Changed Signal<()> + @within AttributeValue ]=] function AttributeValue.__index(self: AttributeValue, index) if AttributeValue[index] then