Pular para o conteúdo

Leitura Sem Rastreio

Nesta página

Nem toda leitura deve criar uma dependência. O Zolo oferece duas formas de ler sem rastrear:

  • signal_untrack(|| { s.get() }) — qualquer .get() dentro do fechamento passado para signal_untrack é ignorado pelo sistema de rastreio. Use para leituras pontuais que não devem ser deps de um effect ou computed.
  • s.peek() — atalho direto para leitura não rastreada no próprio signal. Muito prático dentro de effects para auto-incremento sem criar ciclo.

O complemento de peek é s.modify(fn), que aplica uma função ao valor atual (s.set(fn(s.get()))) de forma concisa — ideal para contadores, acumuladores e atualizações de lista.

b é lido com signal_untrack dentro do effect; mudar b não o re-aciona.

04-untrack.zolo
Playground
// Feature: signal_untrack — read a signal without registering as a dep

// Syntax: `signal_untrack(|| { s.get() })` — the read inside

// the closure is NOT tracked.

// When to use: read-once initialization inside an effect, debug

// peeks, or to break a dependency intentionally.


use std::reactive::Signal

let a = signal(1)
let b = signal(100)

let e = effect(|| {
    let av = a.get()
    // `b` is read but NOT a dependency of this effect.

    let bv = signal_untrack(|| { b.get() })
    print("a={av} b={bv}")
})
// expected: a=1 b=100


// Changing `b` does NOT re-run the effect.

b.set(200)
b.set(300)

// Changing `a` re-runs the effect — and now sees the latest `b`.

a.set(2)
// expected: a=2 b=300

modify para incremento e peek para log interno sem criar dependência de logs.

07-peek-and-modify.zolo
Playground
// Feature: Signal.peek and Signal.modify — non-tracking read + functional update

// Syntax: `s.peek()` reads without subscribing; `s.modify(fn)`

// applies a function to the current value.

// When to use: peek for diagnostics or self-updates inside effects;

// modify for counter-style increments, accumulators, list pushes.


use std::reactive::Signal

let counter = signal(10)

// `s.modify(fn)` is shorthand for `s.set(fn(s.get()))`.

counter.modify(|v| { v + 5 })
print(counter.get())
// expected: 15


counter.modify(|v| { v * 2 })
print(counter.get())
// expected: 30


// `peek` reads without tracking — useful inside effects when you

// want to use the current value but not re-run when it changes.

let logs = signal(0)
let view = signal("home")

let e = effect(|| {
    let page = view.get()
    let n = logs.peek() + 1   // not a dep

    logs.set(n)
    print("nav to {page}, log #{n}")
})
// expected: nav to home, log #1


view.set("about")
// expected: nav to about, log #2

Buscar no Zolo

9 resultados

enespt-br