centau / vide

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

add vide.mutate #35

Closed ewd3v closed 2 months ago

ewd3v commented 2 months ago

Utility function.

local tbl = source({ "Hi" })
effect(function()
    print(tbl[1])
end)

mutate(tbl, function(value)
    value[1] = "Changed message"
end)

is the same as:

local tbl = source({ "Hi" })
effect(function()
    print(tbl[1])
end)

local value = tbl()
value[1] = "Changed message"
tbl(value)

If the mutator function returns a new value the source will be updated to use that instead.

alicesaidhi commented 2 months ago

why?

ewd3v commented 2 months ago

why?

I mean true it can be argued this doesn't help much but I thought this could help with making code easier to read (since you're extracting relating code into their own code blocks). Sure using do statements achieve the same effect I guess, but extracting it into a function has the benefit of being able to add guard statements to that codeblock.

I also thought it wasn't a terrible idea when I thought of the ability to return a new value that the source should be updated to instead (though perhaps it should be renamed to "update" because of that).

So yeah, really just thought it made some code cleaner by removing some boilerplate code (just a utility function anyways).

alicesaidhi commented 2 months ago

I don't see why this should be included in vide; it doesn't make a pattern easier, nor does it need to do anything that requires it to be in vide

ewd3v commented 2 months ago

I don't see why this should be included in vide; it doesn't make a pattern easier, nor does it need to do anything that requires it to be in vide

Yeah it doesn't need to be in vide (just thought it might be interesting.