Update docs

This commit is contained in:
aaron 2024-06-20 18:40:01 +01:00
parent f2de9b0e63
commit 62f1c3a20a
16 changed files with 86 additions and 347 deletions

View file

@ -2,48 +2,33 @@
Sometimes you may need to do some cleanup when destroying a component or after
a side-effect from a source update. Vide provides a function `cleanup()` which
is used to queue a cleanup callback for the next time a reactive scope is rerun
or destroyed, or when a stable scope is destroyed.
is used to queue a callback for the next time a reactive scope is rerun or
destroyed, or when a stable scope is destroyed.
```lua
local mount = vide.mount
local root = vide.root
local source = vide.source
local cleanup = vide.cleanup
local effect = vide.effect
local function Timer()
local count = source(0)
local count = source(0)
local con = game:GetService("RunService").Heartbeat:Connect(function(dt)
count(count() + dt)
local destroy = root(function(destroy)
effect(function()
local x = count()
cleanup(function() print(x) end)
end)
cleanup(function()
con:Disconnect()
end)
cleanup(function() print "root destroyed" end)
return create "TextButton" {
Position = UDim2.fromOffset(300, 300),
Size = UDim2.fromOffset(200, 50),
Text = function()
return "seconds: " .. math.floor(count())
end,
}
end
local instance, destroy = root(function(destroy)
local instance = Timer()
return instance, destroy
return destroy
end)
wait(5)
destroy() -- all queued cleanups are ran, heartbeat connection disconnected
count(1) -- prints "0"
count(2) -- prints "1"
destroy() -- prints "2" and "root destroyed"
```
In the above example, this allows us to disconnect the heartbeat connection
when the scope responsible for creating the timer component is destroyed.
::: tip
Roblox instances do not need to be explicitly destroyed for their
memory to be freed, they only need to be parented to `nil`. So there is no