centau / vide

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

Add features to normalize sources and values #6

Closed littensy closed 1 year ago

littensy commented 1 year ago

There are often cases where props may be either a source or a raw value. Being able to unwrap a T | Source<T> value into T would help allow developers to accept both static and reactive values in components and composables. The functionality would be similar to unref() from Vue.

local function composable(value: string | Source<string>)
    watch(function()
        print(unwrap(value)) -- Hello, world!
    end)
end

Vue also allows you to pass a ref to ref(), and it will be returned as-is. Being able to normalize a T | Source<T> value to Source<T> would go hand-in-hand with the above proposal.

local function composable(value: string | Source<string>)
    local normalized = source(value)

    watch(function()
        print(normalized()) -- Hello, world!
    end)
end