mirror of
https://github.com/centau/vide.git
synced 2026-08-20 14:41:37 +00:00
This commit is contained in:
parent
8e31946dda
commit
13965868ce
13 changed files with 421 additions and 448 deletions
57
docs/tut/crash-course/5-derived-state.md
Normal file
57
docs/tut/crash-course/5-derived-state.md
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
# [Derived State](./index.md)
|
||||
|
||||
You can create new state from existing states. This is known as *deriving
|
||||
state*.
|
||||
|
||||
```lua
|
||||
local count = source(0)
|
||||
|
||||
local function text()
|
||||
return "count: " .. count()
|
||||
end
|
||||
|
||||
create "TextLabel" {
|
||||
Text = text
|
||||
}
|
||||
```
|
||||
|
||||
Assigning a non-event property a function will bind that property to that
|
||||
function, anytime a state being read from inside that function is changed, the
|
||||
function will be re-ran and the property value updated.
|
||||
|
||||
Sometimes when using expensive computations to derive state, you only want to
|
||||
recalculate it when a source state has changed.
|
||||
|
||||
```lua
|
||||
local derive = vide.derive
|
||||
```
|
||||
|
||||
```lua
|
||||
local count = source(0)
|
||||
|
||||
local factorial = derive(function()
|
||||
local n = 1
|
||||
for i = 2, count() do
|
||||
n *= i
|
||||
end
|
||||
return n
|
||||
end)
|
||||
```
|
||||
|
||||
`derive()` will cache and return the same value until a source state has
|
||||
changed, where it will recompute and cache a new value.
|
||||
|
||||
```lua
|
||||
create "TextLabel" {
|
||||
Text = function()
|
||||
return "factorial: " .. factorial()
|
||||
end
|
||||
}
|
||||
|
||||
count(3) -- displays "factorial: 6"
|
||||
count(4) -- displays "factorial: 24"
|
||||
```
|
||||
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
### [← State](./4-State.md) | [Table State →](./6-table-state.md)
|
||||
Loading…
Add table
Add a link
Reference in a new issue