///|
test (t : @test.T) {
struct ComponentRepr {
text : String
html : Lazy[String]
}
struct Component(LazyRec[ComponentRepr])
fn new() -> Component {
LazyRec::new(fn(self : Lazy[ComponentRepr]) {
{
text: "",
html: Lazy::new(fn() {
let html = "\{self.force().text}
"
t.writeln("render: \{html}")
html
}),
}
})
}
fn modify_text(self : Component, f : (String) -> String) -> Unit {
self.inner().modify(fn(self) { { text: f(self.text), html: self.html } })
}
fn html(self : Component) -> String {
self.inner().get(fn(self) { self.html.force() })
}
let c = new()
modify_text(c, fn(_) { "hello world" })
inspect(html(c), content="
hello world
")
inspect(html(c), content="
hello world
")
inspect(html(c), content="
hello world
")
inspect(html(c), content="
hello world
")
modify_text(c, fn(x) { x + "!" })
inspect(html(c), content="
hello world!
")
inspect(html(c), content="
hello world!
")
inspect(html(c), content="
hello world!
")
inspect(html(c), content="
hello world!
")
t.snapshot(filename="component.txt")
}