mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
Make root() return destructor automatically
This commit is contained in:
parent
8142acd1c1
commit
82eec61c45
8 changed files with 26 additions and 30 deletions
|
|
@ -14,18 +14,15 @@ Creates and runs a function in a new stable scope.
|
|||
- **Type**
|
||||
|
||||
```lua
|
||||
function root<T...>(fn: (destroy: () -> ()) -> T...): T...
|
||||
function root<T...>(fn: (() -> ()) -> T...): (() -> (), T...)
|
||||
```
|
||||
|
||||
- **Details**
|
||||
|
||||
Returns the result of the given function.
|
||||
Returns a function to destroy the root scope. Also passes this function as
|
||||
the first argument into its callback.
|
||||
|
||||
Creates a new stable scope, where creation of effects can be tracked and
|
||||
properly disposed of.
|
||||
|
||||
A function to destroy the root is passed into the callback, which will run
|
||||
any cleanups and allow derived sources created to garbage collect.
|
||||
All values returned by the callback are also returned following the destructor.
|
||||
|
||||
## source()
|
||||
|
||||
|
|
|
|||
|
|
@ -81,9 +81,8 @@ mount(function()
|
|||
|
||||
effect(function()
|
||||
if toggled() then
|
||||
local destroy = root(function(destroy)
|
||||
local destroy = root(function()
|
||||
Counter()
|
||||
return destroy
|
||||
end)
|
||||
cleanup(destroy)
|
||||
end
|
||||
|
|
|
|||
|
|
@ -13,15 +13,13 @@ local effect = vide.effect
|
|||
local count = source(0)
|
||||
|
||||
|
||||
local destroy = root(function(destroy)
|
||||
local destroy = root(function()
|
||||
effect(function()
|
||||
local x = count()
|
||||
cleanup(function() print(x) end)
|
||||
end)
|
||||
|
||||
cleanup(function() print "root destroyed" end)
|
||||
|
||||
return destroy
|
||||
end)
|
||||
|
||||
count(1) -- prints "0"
|
||||
|
|
|
|||
|
|
@ -43,21 +43,20 @@ local count = root(setup) -- ok since effect() was called within a stable scope
|
|||
count(1) -- prints "1"
|
||||
```
|
||||
|
||||
The scope created by `root()` can be destroyed by calling the function it passes
|
||||
into the given function.
|
||||
The scope created by `root()` can be destroyed.
|
||||
|
||||
```lua
|
||||
local function setup(destroy)
|
||||
local function setup()
|
||||
local count = source(0)
|
||||
|
||||
effect(function()
|
||||
print(count())
|
||||
end)
|
||||
|
||||
return count, destroy
|
||||
return count
|
||||
end
|
||||
|
||||
local count, destroy = root(setup)
|
||||
local destroy, count = root(setup)
|
||||
|
||||
count(1) -- prints "1"
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue