Files
el/examples/browser-counter.el
Will Anderson a54b2bebf9 add DOM bridge, async/await, window export, and native_js to JS target
- el_runtime.js: add 19 dom_* builtins (browser-only, throw in Node),
  window_set/window_get for exposing El functions to the browser global
  scope, and native_js/native_js_call escape hatches for third-party libs
- codegen-js.el: destructure all new builtins in generated preamble; add
  @async decorator support that emits async function + await at call sites
  for known-async HTTP builtins and user-declared @async functions; pre-
  registration pass ensures forward calls to @async functions get await
- spec/codegen-js.md: mark Phase 3 (DOM bridge) implemented, document
  @async approach and its limitations, update builtin table and status
- examples/browser-counter.el: canonical example showing dom_get_element,
  dom_set_text, dom_is_null, window_set, and state_set/get
2026-05-04 10:29:43 -05:00

42 lines
1.4 KiB
EmacsLisp

// browser-counter.el canonical browser DOM bridge example
//
// Compile with: elc --target=js examples/browser-counter.el > counter.js
//
// Then include in an HTML page that has a <span id="count-display"> element.
// The page can call window.increment() from any onclick handler, e.g.:
// <button onclick="increment()">+1</button>
//
// On load the display is initialised to "0". Each call to increment()
// adds 1 and updates the display text.
//
// Demonstrates:
// - dom_get_element to locate a DOM node by id
// - dom_set_text to update visible text content
// - dom_is_null to guard against missing elements
// - window_set to expose an El function for inline event handlers
// - state_set/get for in-memory counter state (survives calls, resets
// on page reload same semantics as the C state_* API)
fn init() -> Void {
state_set("counter", 0)
let display = dom_get_element("count-display")
if !dom_is_null(display) {
dom_set_text(display, "0")
}
}
fn increment() -> Void {
let current = str_to_int(state_get("counter"))
let next = current + 1
state_set("counter", next)
let display = dom_get_element("count-display")
if !dom_is_null(display) {
dom_set_text(display, int_to_str(next))
}
}
fn main() -> Void {
init()
window_set("increment", increment)
}