Reload de Assets
En esta página
zolo dev también monitorea archivos de datos cuando se pasa la flag
--watch <dir> (recursiva). Los formatos reconocidos incluyen .json, .toml,
.yaml, .csv, .wgsl, .glsl, .frag, .vert y .hlsl. Por cada archivo
modificado, tras un debounce de 150 ms, el runtime llama a
pub fn __on_asset_reload(path: str, ext: str).
Sin esa definición, el evento se descarta silenciosamente.
Para ejecutar este demo es necesario pasar la flag explícitamente:
cd examples/features/21-hot-reload/05-asset-reload
zolo dev main.zolo --watch assets
El módulo app.zolo mantiene el config en memoria y expone show() y el hook
de asset. Al detectar la extensión "json", el hook relee el archivo con
fs::read_to_string y reemplaza el config vivo:
Edita assets/config.json (p. ej.: cambia "speed") y observa el hook dispararse.
// Feature: HMR of assets — reload config without restarting
// Syntax: global hook `pub fn __on_asset_reload(path: str, ext: str)`
// When to use: games (shaders, sprites), apps with hot config,
// pipelines where data changes more often than the code.
//
// The watcher recognizes: .json .toml .yaml .yml .csv
// .wgsl .glsl .frag .vert .hlsl
// For each such file under `--watch <dir>` (recursive) or the
// implicit directories of the entry/modules, the hook is called.
use std::package
use std::json
let config: any = #{name: "default", speed: 1, color: "gray"}
pub fn show() {
let live = package.loaded["app"]
print("current config: name={live.config.name}, speed={live.config.speed}, color={live.config.color}")
}
pub fn __on_asset_reload(path: str, ext: str) {
let live = package.loaded["app"]
print(">>> asset changed: {path} ({ext})")
if ext == "json" {
let raw = fs::read_to_string(path)
if raw != nil {
live.config = json.parse(raw)
print(">>> config reloaded")
}
}
}
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
El punto de entrada carga el JSON inicial y cede el control al bucle interactivo:
// Feature: hot-reload of data
// Syntax: run with `zolo dev main.zolo --watch assets`.
// When to use: edit config.json and see the app react without restart.
use std::package
use std::json
use app::{show, __on_asset_reload}
fn main() {
// Initial load.
let raw = fs::read_to_string("assets/config.json")
if raw != nil {
let live = package.loaded["app"]
live.config = json.parse(raw)
}
show()
print("ENTER reprints. Edit assets/config.json and save.")
print("Run with: zolo dev main.zolo --watch assets")
while true {
let line = io::read("*l")
if line is nil || line == "q" {
break
}
show()
}
}
Requiere la CLI o el host de Zolo; ábrelo en el playground o ejecútalo localmente.
Desafío
Agrega un campo "theme": "dark" a assets/config.json e imprímelo en show().
Guarda el JSON — el hook debe recargarlo sin cambios en el código Zolo.
Consulta también