← Back to Official Package Registry
Tier 6: Local-First, IoT, Web & Media • Server-Driven Live Viewsv1.0.0 • 23 KB • Ed25519 Verified

web_live@1.0.0

Homoiconic HTML virtual-DOM differ, keyed patch ops, Server-Sent Events (SSE) morphing frames, and typed HTMX request/response helpers.

use [web_live]
veko pkg add web_live@1.0.0
Run in Browser PlaygroundDownload 1.0.0.vkpRaw demo.vk

web_live

Server-driven LiveView virtual DOM tree differ, minimal DOM patch morpher, SSE patch streamer, < 2 KB browser runtime, and HTMX hypermedia helpers built over lib/html and lib/web.

Installation

powershell
veko pkg add web_live@1.0.0

Import in any Veko module:

rebol
use [live = web_live]

Architecture Notes

  • Homoiconic Hiccup Compatibility: Operates directly on lib/html Hiccup blocks ([.div { id = 'app' } [ [.h1 {} ['Hello']] ]]) with zero intermediate JSX compilation step.
  • Minimal Tree-Path Patch Operations: live/diff-trees walks old_tree and new_tree in lockstep, emitting .replace, .set_text, .set_attr, .del_attr, .insert_child, and back-to-front .remove_child operations addressed by integer childNodes paths (such as [0 2 1]).
  • Exact Structural Roundtrip: live/apply-patches applies patch sequences in pure Veko so (live/apply-patches old_tree patches) == new_tree holds deterministically in unit tests without launching a browser.
  • Compact < 2 KB Client Morph Runtime: live/client-runtime-js embeds an EventSource listener (veko-patch) and [vk-click] event delegation script in under 1.2 KB of vanilla JavaScript.
  • HTMX Hypermedia Engine: First-class helpers for HX-Request, HX-Boosted, HX-Target, HX-Trigger, HX-Redirect, HX-Reswap, HX-Retarget, hx-swap-oob, and automatic full-page vs fragment rendering (live/render-page-or-partial).

API Reference

Types

  • PatchOp — { op enum, path block, key str, val any, index i64 }
  • LiveSession — { id str, state any, tree any, version i64, reducer any, view any }
  • LiveStep — { version i64, patches block[PatchOp], sse str, tree any }

Virtual DOM & LiveView Functions

  • live/hiccup-el? node -> bool
  • live/diff-trees old_tree new_tree -> block[PatchOp]
  • live/apply-patches tree patches -> any
  • live/encode-sse-patch patches [/id event_id] [/event ev_name] -> str
  • live/client-runtime-js endpoint_prefix -> str
  • live/new-session id initial_state reducer_fn view_fn -> LiveSession
  • live/dispatch-event mut sess event_name payload -> LiveStep

HTMX Hypermedia Functions

  • live/htmx-request? req -> bool
  • live/htmx-boosted? req -> bool
  • live/htmx-target req -> str
  • live/htmx-trigger-name req -> str
  • live/hx-trigger resp event_spec -> web/Response
  • live/hx-redirect location -> web/Response
  • live/hx-reswap resp strategy -> web/Response
  • live/hx-retarget resp selector -> web/Response
  • live/hx-swap-oob node swap_spec -> block
  • live/render-page-or-partial req title head_extra fragment_node -> web/Response

Idiomatic Veko Example

rebol
use [live = web_live, html]

view = fn [count i64] block [
    [.div { id = 'counter' } [
        [.span { class = 'val' } [`Count: {mold count}`]]
        [.button { 'vk-click' = 'inc' } ['+1']]
    ]]
]

t0 = view 0
t1 = view 1
patches = live/diff-trees t0 t1
sse = live/encode-sse-patch/id patches '1'
println sse
packages/web_live/examples/demo.vkSelf-contained runnable example & assertion suite
Raw .vk
// packages/web_live/examples/demo.vk
// Verification suite for `web_live`: Virtual DOM `diff-trees`, `apply-patches`
// roundtrip invariants, SSE patch framing, <2 KB JS morph runtime, LiveSession
// reducer transitions, and HTMX hypermedia helpers.

use [live = web_live, html, web]

assert-eq = fn [label str, got any, want any] [
	if got != want [
		println `[FAIL] {label}: got {mold got}, expected {mold want}`
		return (err `assertion failed: {label}`)
	]
	println `[PASS] {label}`
	.ok
]

assert-true = fn [label str, cond bool] [
	if not cond [
		println `[FAIL] {label}`
		return (err `assertion failed: {label}`)
	]
	println `[PASS] {label}`
	.ok
]

render-dashboard = fn [st map] block [
	count = as-i64 (map/get st 'count')
	rows = map/get st 'rows'
	alert = str/append '' (map/get/or st 'alert' [ '' ])

	mut panel_attrs = {
		id          = 'live-panel'
		class       = if count >= 2 [ 'panel panel-hot' ] else [ 'panel' ]
		'data-step' = (mold count)
	}
	if alert != '' [
		map/set mut panel_attrs 'data-alert' alert
	]

	mut tr_nodes = []
	mut i = 0
	while i < (len rows) [
		r = rows[i]
		push mut tr_nodes [.tr { 'data-key' = (str/append '' (map/get r 'id')) } [
			[.td {} [(str/append '' (map/get r 'name'))]]
			[.td {} [(mold (map/get r 'ms'))]]
		]]
		i = i + 1
	]

	[.section panel_attrs [
		[.h2 {} [`Requests: {mold count}`]]
		[.table { class = 'metrics' } [
			[.tbody {} tr_nodes]
		]]
		(if count == 0 [
			[.p { class = 'hint' } ['Idle']]
		] else [
			[.span { class = 'active-badge' } ['Running']]
		])
	]]
]

dashboard-reducer = fn [st map, ev str, payload any] map [
	mut next = st
	if ev == 'inc' [
		c = as-i64 (map/get next 'count')
		map/set mut next 'count' (c + 1)
	]
	if ev == 'add_row' [
		mut rs = map/get next 'rows'
		push mut rs payload
		map/set mut next 'rows' rs
	]
	if ev == 'pop_row' [
		old_rs = map/get next 'rows'
		mut trimmed = []
		mut i = 0
		while i < ((len old_rs) - 1) [
			push mut trimmed old_rs[i]
			i = i + 1
		]
		map/set mut next 'rows' trimmed
	]
	if ev == 'clear_alert' [
		map/delete mut next 'alert'
	]
	next
]

main = fn [] any [
	s0 = {
		count = 0
		alert = 'warmup'
		rows  = [
			{ id = 'r1', name = 'GET /health', ms = 2 }
			{ id = 'r2', name = 'POST /mcp', ms = 9 }
		]
	}

	s1 = {
		count = 2
		rows  = [
			{ id = 'r1', name = 'GET /health', ms = 3 }
			{ id = 'r2', name = 'POST /mcp', ms = 9 }
			{ id = 'r3', name = 'POST /chat', ms = 41 }
		]
	}

	tree0 = render-dashboard s0
	tree1 = render-dashboard s1

	// 1. Compute minimal `diff-trees` patches covering `.del_attr`, `.set_attr`, `.set_text`, `.insert_child`, and `.replace`
	patches_0_1 = live/diff-trees tree0 tree1
	_ = assert-true/or 'diff-trees produces minimal non-empty patches' ((len patches_0_1) >= 5 and (len patches_0_1) <= 8) [e -> return e]

	patched1 = live/apply-patches/or tree0 patches_0_1 [e -> return e]
	_ = assert-eq/or 'apply-patches tree0 patches == tree1 (exact structural equality)' patched1 tree1 [e -> return e]
	_ = assert-eq/or 'html/render patched1 == html/render tree1' (html/render patched1) (html/render tree1) [e -> return e]

	// 2. Test `.remove_child` when rows shrink back from 3 rows to 1 row
	s2 = {
		count = 2
		rows  = [
			{ id = 'r1', name = 'GET /health', ms = 3 }
		]
	}
	tree2 = render-dashboard s2
	patches_1_2 = live/diff-trees tree1 tree2
	_ = assert-eq/or 'shrinking 2 rows emits exactly 2 remove_child patches' (len patches_1_2) 2 [e -> return e]
	_ = assert-eq/or 'remove_child emitted back-to-front (index 2 then 1)' patches_1_2[0].index 2 [e -> return e]
	_ = assert-eq/or 'remove_child second index 1' patches_1_2[1].index 1 [e -> return e]

	patched2 = live/apply-patches/or tree1 patches_1_2 [e -> return e]
	_ = assert-eq/or 'apply-patches tree1 patches == tree2' patched2 tree2 [e -> return e]

	// 3. Test SSE patch framing & <2 KB client runtime JS
	sse_wire = live/encode-sse-patch/id patches_0_1 '42'
	_ = assert-true/or 'SSE patch carries id: 42' (str/contains? sse_wire 'id: 42\n') [e -> return e]
	_ = assert-true/or 'SSE patch carries event: veko-patch' (str/contains? sse_wire 'event: veko-patch\n') [e -> return e]
	_ = assert-true/or 'SSE patch carries data: JSON ops' (str/contains? sse_wire '"op":"set_text"') [e -> return e]

	js_code = live/client-runtime-js '/live'
	js_len = len (bytes/from-str js_code)
	_ = assert-true/or 'client-runtime-js is compact (< 2048 bytes)' (js_len > 200 and js_len < 2048) [e -> return e]
	_ = assert-true/or 'client-runtime-js wires EventSource veko-patch' (str/contains? js_code 'EventSource') [e -> return e]

	// 4. Test stateful `LiveSession` reducer manager
	mut sess = live/new-session 'sess-1' s0 fn [st map, ev str, p any] map [
		dashboard-reducer st ev p
	] fn [st map] block [
		render-dashboard st
	]
	step1 = live/dispatch-event/or mut sess 'inc' .none [e -> return e]
	_ = assert-eq/or 'LiveSession version incremented to 2' step1.version 2 [e -> return e]
	step2 = live/dispatch-event/or mut sess 'add_row' { id = 'r3', name = 'GET /live', ms = 5 } [e -> return e]
	_ = assert-eq/or 'LiveSession version incremented to 3' step2.version 3 [e -> return e]
	_ = assert-true/or 'LiveSession SSE contains insert_child' (str/contains? step2.sse 'insert_child') [e -> return e]

	// 5. Test HTMX hypermedia request detection, headers & partial vs full-page wrapper
	full_req = web/Request {
		method = 'GET', path = '/dashboard', target = '/dashboard',
		query = {}, params = {}, headers = {}, body = bytes/with-cap 0
	}
	hx_req = web/Request {
		method = 'GET', path = '/dashboard', target = '/dashboard',
		query = {}, params = {}, headers = { 'HX-Request' = 'true', 'HX-Target' = 'live-panel' }, body = bytes/with-cap 0
	}

	_ = assert-eq/or 'htmx-request? false on normal GET' (live/htmx-request? full_req) false [e -> return e]
	_ = assert-eq/or 'htmx-request? true on HX-Request' (live/htmx-request? hx_req) true [e -> return e]
	_ = assert-eq/or 'htmx-target reads HX-Target' (live/htmx-target hx_req) 'live-panel' [e -> return e]

	full_resp = live/render-page-or-partial full_req 'Live Dashboard' [] tree2
	part_resp = live/render-page-or-partial hx_req 'Live Dashboard' [] tree2
	_ = assert-true/or 'Full GET includes <!DOCTYPE html>' (str/contains? (bytes/to-str full_resp.body) '<!DOCTYPE html>') [e -> return e]
	_ = assert-true/or 'HTMX partial omits <!DOCTYPE html>' (not (str/contains? (bytes/to-str part_resp.body) '<!DOCTYPE html>')) [e -> return e]

	oob_badge = live/hx-swap-oob [.div { id = 'toast' } ['Saved']] 'outerHTML'
	_ = assert-true/or 'hx-swap-oob attribute rendered' (str/contains? (html/render oob_badge) 'hx-swap-oob="outerHTML"') [e -> return e]

	trig_resp = live/hx-trigger part_resp { showToast = 'Updated' }
	_ = assert-eq/or 'HX-Trigger JSON header' (map/get trig_resp.headers 'HX-Trigger') '{"showToast":"Updated"}' [e -> return e]

	redir_resp = live/hx-redirect '/login'
	_ = assert-eq/or 'HX-Redirect header' (map/get redir_resp.headers 'HX-Redirect') '/login' [e -> return e]

	println 'web_live demo: all checks passed.'
	0
]

main
Cryptographic Provenance

Ed25519 Signature & Merkle Integrity

Registry URL: https://veko-lang.org/pkg/web_live/1.0.0.vkp
Archive SHA-256: 7ad1171a6b34c0b9b590a45d741493b935c16895fab1accecf60834b477e0361
Merkle Root: 1f741377f906b99e899dbc773a83ce167f935bd5660ba9ef25de75bf50bfba72
Signer Trust Key: ed25519:27406074d8681da1d4e6420f5d887cac15c313334c249ac047ba9d4e2df72b70
Embedded VKP1 Manifest

manifest.json

{"name":"web_live","version":"1.0.0","tier":6.0,"tier_label":"Tier 6: Local-First, IoT, Web & Media","category":"Server-Driven Live Views","description":"Homoiconic HTML virtual-DOM differ, keyed patch ops, Server-Sent Events (SSE) morphing frames, and typed HTMX request/response helpers.","modules":["web_live"],"homepage":"https://veko-lang.org/packages/web_live"}
Bundle Entry PathSizeLeaf SHA-256 Digest
README.md2 KBb68ce983daa2ea8078abd80c23ffd9a277d385f972bb3a25a9878695a60935a5
examples/demo.vk6 KB0be74478e2fa353426c24a1de13e17b8e065b05a3a69dcc182cd143f580ca4c6
mod.vk13 KBded590176a99d084e651b52db69264d3b7c2db69a1dfc1a741027c5073456425