vide/docs/tut/crash-course/5-effect.md
alicesaidhi f8e84f9f8d
Improve Documentation Site (#41)
* update css and snippets

* Add banner

* improve home page

* Update Banner

* cleanup

* Update font to JetBrains Mono

* Add a quick look to Home

* Simplify Home Page

* Removed banner, removed copyright notice, reduced home page
2024-11-03 17:32:19 +00:00

976 B

Effects

Effects are functions that are ran in response to source updates. They are A source and effect is analogous to a signal and connection.

Effects are created using effect().

local source = vide.source
local effect = vide.effect

local count = source(0)

effect(function()
    print("count: " .. count())
end)

-- "count: 0" printed
count(1)
-- "count: 1" printed

Any source read inside an effect is tracked and will rerun the effect when that source is updated.

Derived sources are also tracked, it doesn't matter how deeply nested inside a function a source is.

local source = vide.source
local effect = vide.effect

local count = source(1)

local doubled = function()
    return count() * 2
end

effect(function()
    print("doubled count: " .. doubled())
end)

-- "doubled count: 2" printed
count(2)
-- "doubled count: 4" printed

If a source is updated with the same value it already had, it will not rerun effects depending on it.