Decorators
A decorator adds logic to components: state, functions and mount. It is declared with dec and applied with @name(arguments) before comp. Like a component, it exists only at compile time: its code is inlined into every component it is applied to, and each of them gets its own state.
// A decorator adds logic to components: state, functions and mount. Its code is // inlined into every component it is applied to, so each one ticks on its own dec ticker(interval number = 1000) { public state ticks = 0 public state running = true public func toggle() { running = !running } mount { const timer = setInterval(() => { if running { ticks++ } }, interval) defer clearInterval(timer) } } // The component reads public state as @name.member and calls public functions as @name.f() @ticker comp Stopwatch(label string) { return <p class="card"> {label}: {@ticker.ticks} s <button onClick={@ticker.toggle()}>{@ticker.running ? "Pause" : "Go on"}</button> </p> } // The same logic in another component; the argument makes it tick faster const FRAMES = ["◐", "◓", "◑", "◒"] @ticker(150) comp Spinner() { return <p class="card">{FRAMES[@ticker.ticks % FRAMES.length]}</p> } document.body.append(<Stopwatch label="Tea" />, <Stopwatch label="Eggs" />, <Spinner />)
As the board grows, a decorator can add the same behavior to each task row, such as a shared counter or a wrapper that handles a component's failure. Keep task data in the board; the decorator contributes reusable component behavior.
- The component sees only what is public: it reads
public state,public derivedandpublic constas@ticker.ticksand calls apublic funcas@ticker.toggle(). In markup,@ticker.ticksupdates like the component's own state. A parameter withpublicbefore it,dec titled(public title string), is a member too,@titled.title. Other names of the decorator are its own and cannot clash with the component's. - Outside the decorator its members are read-only:
@ticker.ticks = 0is an error, changes go through its public functions. There is nopublic let. - The arguments are positional, as in a call, and the parameters may have default values:
@tickerticks every second. The arguments see the properties of the component,@ticker(label.length * 100), and are computed again when they change. - With several decorators,
@a @b comp X, their bodies run from top to bottom before the component's, andmountruns from the inside out: the component,b,a.@html-taggoes first.
A decorator can also wrap what the component returns: its body ends with return (content Element) => ..., which gets the component's markup and gives other markup instead. A decorator that needs another one sees its public members.
// A wrapper gets what the component returns and gives other markup instead. // This one puts it in a card with a title that folds it dec folded(title string) { state open = true return (content Element) => <section class="card"> <h3 onClick={open = !open}>{open ? "▾" : "▸"} {title}</h3> {open ? content : null} </section> } dec quantity() { public state count = 0 public func add() { count++ } } // A decorator that needs another one sees its public members dec quantityBadge() needs quantity { return (content Element) => <div> {content} <p>Taken: {@quantity.count}</p> </div> } // The wrappers apply from the bottom up: quantityBadge, then folded. // quantity goes above quantityBadge, which needs it @folded(name) @quantity @quantityBadge comp Product(name string, price number) { return <p>Price: {price} <button onClick={@quantity.add()}>Take one</button></p> } document.body.append(<Product name="Guava" price={3} />, <Product name="Papaya" price={2} />)
- The type of the wrapper's parameter is written: it limits where the decorator can be applied, so
(form HTMLFormElement) => ...fits only components that return a form.<Product />has the type of what the outermost wrapper returns. - The wrapper runs once, when the component is created. Its markup reads the decorator's state and updates like the component's own. The wrappers apply from the bottom decorator up.
dec quantityBadge() needs quantitymay use@quantity.counteven in its body, so@quantitymust be applied above it.export decexports a decorator, and another module imports it by its name:import { quantity } from "./quantity.rk". Its code is inlined there, so it cannot assign the variables of its own module, and the decorators it needs are exported too.
A decorator can include others: dec card(title string) includes framed(title), likes applies them with it, above its own body, and their arguments see its parameters. The component reads all their public members through it, @card.count. A public ref gives the component an element of the wrapper.
// A decorator can include others: they are applied with it, and the component // reads all their public members through it dec likes() { public state count = 0 public func like() { count++ } } // A public ref gives the component an element of the wrapper dec framed(title string) { public ref box return (content Element) => <section ref={box} class="card"> <b>{title}</b> {content} </section> } // The arguments of the included decorators see the parameters of card dec card(title string) includes framed(title), likes { public derived popular = @likes.count >= 3 } @card(name) comp Fruit(name string) { func like() { @card.like() if @card.popular { @card.box.style.borderColor = "#f5a623" } } return <p> <button onClick={like()}>♥ {@card.count}</button> {if @card.popular { <i> popular</i> }} </p> } document.body.append(<Fruit name="Mango" />, <Fruit name="Guava" />)
@cardcounts as@likesapplied: it satisfiesneeds likesof a decorator below it, and@likesnext to it is applied twice. Two members with the same name are an error.- A ref always points to its element:
@card.box = xis an error, but the element itself can be changed,@card.box.style.color = "red". - Not yet: an exported decorator that includes others.