centau / vide

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

Effects always run twice #43

Open fewkz opened 1 week ago

fewkz commented 1 week ago

Using vide 0.3.1, latest version on wally Effects seem to always run twice when a source changes image I assumed maybe reads also trigger effects, but that's not the cause: image

littensy commented 6 days ago

This is a feature of Vide's Strict Mode to encourage writing pure effects that clean up properly. It's automatically enabled in Roblox Studio and defaults to false in production.

If you need your code to run once in an effect (and you don't want to disable strict mode), one method is to defer the code and allow subsequent effect calls to cancel it:

Vide.effect(function()
    local connection = RunService.Heartbeat:Once(function()
        -- code here runs max once per frame
    end)
    cleanup(connection)
end)

You can also compare the current and previous state tracked by an effect and check whether that changed:

vide.effect(function(previous)
    local current = state()
    if current ~= previous then
        -- code here only runs when state() changed
    end
    return current
end, state())