Saltar al contenido

Lectura Sin Rastreo

En esta página

No toda lectura debe crear una dependencia. Zolo ofrece dos formas de leer sin rastrear:

  • signal_untrack(|| { s.get() }) — cualquier .get() dentro del cierre pasado a signal_untrack es ignorado por el sistema de rastreo. Úsalo para lecturas puntuales que no deben ser deps de un effect o computed.
  • s.peek() — atajo directo para lectura no rastreada en el propio signal. Muy práctico dentro de effects para auto-incremento sin crear un ciclo.

El complemento de peek es s.modify(fn), que aplica una función al valor actual (s.set(fn(s.get()))) de forma concisa — ideal para contadores, acumuladores y actualizaciones de lista.

b se lee con signal_untrack dentro del effect; cambiar b no lo re-activa.

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 y peek para log interno sin crear dependencia 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 en Zolo

9 resultados

enespt-br