Files
rose-ash/web/web-signals.sx
giles fc2b5e502f Step 5p6 lazy loading + Step 6b VM transpilation prep
Lazy module loading (Step 5 piece 6 completion):
- Add define-library wrappers + import declarations to 13 source .sx files
- compile-modules.js generates module-manifest.json with dependency graph
- compile-modules.js strips define-library/import before bytecode compilation
  (VM doesn't handle these as special forms)
- sx-platform.js replaces hardcoded 24-file loadWebStack() with manifest-driven
  recursive loader — only downloads modules the page needs
- Result: 12 modules loaded (was 24), zero errors, zero warnings
- Fallback to full load if manifest missing

VM transpilation prep (Step 6b):
- Refactor lib/vm.sx: 20 accessor functions replace raw dict access
- Factor out collect-n-from-stack, collect-n-pairs, pad-n-nils helpers
- bootstrap_vm.py: transpiles 9 VM logic functions to OCaml
- sx_vm_ref.ml: proof that vm.sx transpiles (preamble has stubs)

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-04 12:18:41 +00:00

95 lines
3.4 KiB
Plaintext

(import (sx dom))
(import (sx browser))
(define-library (sx signals-web)
(export with-marsh-scope dispose-marsh-scope emit-event on-event bridge-event resource)
(begin
;; ==========================================================================
;; web/signals.sx — Web platform signal extensions
;;
;; Extends the core reactive signal spec (spec/signals.sx) with web-specific
;; features: marsh scopes (DOM lifecycle), named stores (page-level state),
;; event bridge (lake→island communication), and async resources.
;;
;; These depend on platform primitives:
;; dom-set-data, dom-get-data, dom-listen, dom-dispatch, event-detail,
;; promise-then
;; ==========================================================================
;; --------------------------------------------------------------------------
;; Marsh scopes — child scopes within islands
;; --------------------------------------------------------------------------
(define with-marsh-scope :effects [mutation io]
(fn (marsh-el (body-fn :as lambda))
(let ((disposers (list)))
(with-island-scope
(fn (d) (append! disposers d))
body-fn)
(dom-set-data marsh-el "sx-marsh-disposers" disposers))))
(define dispose-marsh-scope :effects [mutation io]
(fn (marsh-el)
(let ((disposers (dom-get-data marsh-el "sx-marsh-disposers")))
(when disposers
(for-each (fn ((d :as lambda)) (cek-call d nil)) disposers)
(dom-set-data marsh-el "sx-marsh-disposers" nil)))))
;; --------------------------------------------------------------------------
;; Named stores — page-level signal containers
;; --------------------------------------------------------------------------
;; def-store, use-store, clear-stores are now OCaml primitives
;; (sx_primitives.ml) with a global mutable registry that survives
;; env scoping across bytecode modules and island hydration.
;; --------------------------------------------------------------------------
;; Event bridge — DOM event communication for lake→island
;; --------------------------------------------------------------------------
(define emit-event :effects [io]
(fn (el (event-name :as string) detail)
(dom-dispatch el event-name detail)))
(define on-event :effects [io]
(fn (el (event-name :as string) (handler :as lambda))
(dom-on el event-name handler)))
(define bridge-event :effects [mutation io]
(fn (el (event-name :as string) (target-signal :as signal) transform-fn)
(effect (fn ()
(let ((remove (dom-on el event-name
(fn (e)
(let ((detail (event-detail e))
(new-val (if transform-fn
(cek-call transform-fn (list detail))
detail)))
(reset! target-signal new-val))))))
remove)))))
;; --------------------------------------------------------------------------
;; Resource — async signal with loading/resolved/error states
;; --------------------------------------------------------------------------
(define resource :effects [mutation io]
(fn ((fetch-fn :as lambda))
(let ((state (signal (dict "loading" true "data" nil "error" nil))))
(promise-then
(cek-call fetch-fn nil)
(fn (data)
(reset! state (dict "loading" false "data" data "error" nil)))
(fn (err)
(reset! state (dict "loading" false "data" nil "error" err))))
state)))
))
;; Re-export to global env
(import (sx signals-web))