Skip to content

⭐ Implement State.fromValue #20

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

Open
wants to merge 1 commit into
base: Master
Choose a base branch
from
Open
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
32 changes: 32 additions & 0 deletions Modules/State/Source/init.luau
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,38 @@ function State.Interface.fromAttribute(object: Instance, attribute: string): Sta
return stateObject
end

--[=[
@function fromValue
@within State

@param object Instance & { Value: any? }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
@param object Instance & { Value: any? }
@param object ValueBase & { Value: any? }


@return State

Wrapper for `State.new` however wraps around a Roblox Instance, the State object will always have the latest value.

```lua
local object = State.fromValue(workspace.object)

...
```
]=]
function State.Interface.fromValue(object: Instance & { Value: any? }): State
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function State.Interface.fromValue(object: Instance & { Value: any? }): State
function State.Interface.fromValue(object: ValueBase & { Value: any? }): State

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it as ValueBase at first but opted for Instance instead as it adds nothing; none of the Value classes such as StringValue even inherit from the ValueBase class. It doesn't contain the one thing all of the Value classes share: a Value property - I don't even know why it exists. Moreover, everyone is familiar with Instance so I want to argue it's easier comprehension, even if nothing changes Intellisense-wise.

I have no problem conforming it but I thought I'd give my thoughts 🍀

assert(object.Value, "instance must be a Value object!")

local stateObject = State.Interface.new(object.Value)

local valueConnection = object:GetPropertyChangedSignal("Value"):Connect(function()
stateObject:Set(object.Value)
end)

stateObject.Destroyed:Once(function()
valueConnection:Disconnect()
end)

return stateObject
end

--[=[
@function is
@within State
Expand Down