Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Effects always run twice #43

Open
fewkz opened this issue Nov 19, 2024 · 1 comment
Open

Effects always run twice #43

fewkz opened this issue Nov 19, 2024 · 1 comment

Comments

@fewkz
Copy link

fewkz commented Nov 19, 2024

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
Copy link
Contributor

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())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants