centau / vide

A reactive Luau library for creating UI.
https://centau.github.io/vide/
MIT License
95 stars 16 forks source link

Add function `update` #2

Closed sirjahames closed 11 months ago

sirjahames commented 1 year ago

Adding the function listed in the title would be beneficial for development processes with vide.

update(instance: Instance)

Updates an existing instance by applying the properties and states passed.

Type

type Properties = Map<string|number, any>

function update(instance: Instance): (Properties) -> nil

Details

Example

local frame = script.Parent
print(frame.Name) --> "Frame"

update(frame) {
    Name = "SomeFrame"
}

print(frame.Name) --> "SomeFrame"
centau commented 1 year ago

There are some issues and concerns I have that need to be addressed before adding this function:

  1. The name update() is a bit overloaded in the context of this library and could cause some confusion, a better name could be hydrate() or apply().

  2. What should the behavior be in the following case?

    local label = Instance.new("TextLabel")
    
    local countA = source(0)
    local countB = source(100)
    
    update(label) {
        Text = countA
    }
    
    update(label) {
        Text = countB
    }
  3. What are the use-cases for this function? Currently create() can accept an instance to clone and apply properties to which serves the purpose of allowing us to use pre-made instances in the same way as Vide-made instances and avoiding the issue in 2..`

centau commented 1 year ago

I don't think we should officially have this function as part of the API because it can easily lead to antipatterns. You're supposed to create instances and side-effects to update properties only once, while this function lets you break this.

If you want to create side-effects to update properties of instances created outside of vide, you can easily do so with an effect() call.