Fusion icon indicating copy to clipboard operation
Fusion copied to clipboard

Only State object

Open Judash399 opened this issue 3 weeks ago • 2 comments

Currently in fusion, its hard parent children only when a certain condition is met. This is still possible of course, you can use computeds to make this work. Although I suggest we implement a new state object to simplify this action that I've personally ran into several times.

This object would be called Only because it only is set to the value when a certain contition is met.

Only could be used like this:

local condition = scope:Value(true)

local value = scope:Only(condition, scope:Value("Test"))

print(peek(value)) --> "Test"

condition:set(false)

print(peek(value)) --> nil

As already mentioned, this would be helpful when parenting children to instances, especially for components.

local object = scope:New "Frame" {
    [Children] = {
        scope:Only(condition, scope:New "Frame" {
            Name = "Inner Frame"
        })
    }
}

Although this is possible to be done through computeds, I belive we should still implement this to make code clearer, and easier to write.

Judash399 avatar Dec 10 '25 05:12 Judash399

I realized that adding an entire new State Object for Only would be pretty complicaated and have an unnecisary amount of code. Thats why I suggest we instaid make it a function you call instaid. So it would look like this instaid:

local condition = scope:Value(true)

local value = scope:Computed(Only(condition, scope:Value("Test")))

print(peek(value)) --> "Test"

condition:set(false)

print(peek(value)) --> nil

Only would now just be a function that returns another function that Computed can use. I call these Computed templates, and im pretty sure this is the first instance of this.

This would be an even smaller amount of code (likely around 15 lines of code) meaning that this might not have much of a priority, but it still could be a nice helper function.

Judash399 avatar Dec 11 '25 00:12 Judash399

Something like this was already suggested in #377, and was closed as not planned because it was "too general of a design".

HappySunChild avatar Dec 12 '25 22:12 HappySunChild