sxtp: patch + signals primitives (Datastar-borrowed)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 46s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 46s
Adds two new top-level SXTP message types alongside
request/response/condition/event, modelled on Datastar's
datastar-patch-elements and datastar-patch-signals SSE events:
(patch :target "#x" :mode outer :body (~card)) - DOM fragment
morph. Subsumes HTMX swap modes. Mode is outer (default) |
inner | replace | prepend | append | before | after | remove.
(signals :values {:n 3} :only-if-missing false) - reactive
state patch. nil value removes the signal. only-if-missing
skips existing signals (lazy init).
A server response stream can mix both freely; clients dispatch
by head symbol, ordering preserved. Cleaner than HTMX's
swap-mode-per-trigger because the patch shape is decoupled from
the triggering element/attribute.
Spec at applications/sxtp/spec.sx (patch-fields, signals-fields,
patch-modes, example-patch-stream). Constructors / predicates /
accessors / serialise / parse in lib/host/sxtp.sx. 25 new tests
in lib/host/tests/sxtp.sx (predicates, mode normalisation, fixed
field order, remove-without-body, signals round-trip). Host
conformance 129/129 (was 104/104).
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -4,10 +4,10 @@
|
||||
;; at applications/sxtp/spec.sx.
|
||||
;;
|
||||
;; Representation: internally a message is a plain dict tagged by :msg ("request"
|
||||
;; /"response"/"condition"/"event"), with string keys so the keyword==string rule
|
||||
;; makes construction and access trivial. verb/status/type are stored as SYMBOLS
|
||||
;; (they ride the wire bare, not quoted). The wire LIST form is produced/consumed
|
||||
;; only at the serialise/parse boundary:
|
||||
;; /"response"/"condition"/"event"/"patch"/"signals"), with string keys so the
|
||||
;; keyword==string rule makes construction and access trivial. verb/status/type/
|
||||
;; mode are stored as SYMBOLS (they ride the wire bare, not quoted). The wire
|
||||
;; LIST form is produced/consumed only at the serialise/parse boundary:
|
||||
;; sxtp/serialize : msg-dict -> text/sx string
|
||||
;; sxtp/parse : text/sx string -> msg-dict
|
||||
;; A Dream HTTP request/response bridges to/from SXTP via sxtp/from-dream and
|
||||
@@ -35,6 +35,21 @@
|
||||
(fn (etype opts)
|
||||
(merge {:msg "event" :type (sxtp/-sym etype)} opts)))
|
||||
|
||||
;; Patch (Datastar-borrowed) — DOM fragment morph.
|
||||
;; target: CSS selector (required). mode in opts defaults to outer; accepts
|
||||
;; string OR symbol and is normalised. mode values: outer | inner | replace |
|
||||
;; prepend | append | before | after | remove. body: SX subtree (omit for remove).
|
||||
(define sxtp/patch
|
||||
(fn (target opts)
|
||||
(let ((mode (or (get opts :mode) "outer")))
|
||||
(merge opts {:msg "patch" :target target :mode (sxtp/-sym mode)}))))
|
||||
|
||||
;; Signals (Datastar-borrowed) — reactive state patch.
|
||||
;; values: dict of signal-name -> new-value (nil removes). only-if-missing: bool.
|
||||
(define sxtp/signals
|
||||
(fn (values opts)
|
||||
(merge {:msg "signals" :values values} opts)))
|
||||
|
||||
;; ── predicates ─────────────────────────────────────────────────────
|
||||
(define sxtp/-is?
|
||||
(fn (m tag) (and (= (type-of m) "dict") (= (get m :msg) tag))))
|
||||
@@ -42,6 +57,8 @@
|
||||
(define sxtp/response? (fn (m) (sxtp/-is? m "response")))
|
||||
(define sxtp/condition? (fn (m) (sxtp/-is? m "condition")))
|
||||
(define sxtp/event? (fn (m) (sxtp/-is? m "event")))
|
||||
(define sxtp/patch? (fn (m) (sxtp/-is? m "patch")))
|
||||
(define sxtp/signals? (fn (m) (sxtp/-is? m "signals")))
|
||||
|
||||
;; ── accessors ──────────────────────────────────────────────────────
|
||||
(define sxtp/verb (fn (m) (get m :verb)))
|
||||
@@ -56,6 +73,11 @@
|
||||
(define sxtp/stream? (fn (m) (= (get m :stream) true)))
|
||||
(define sxtp/cond-type (fn (m) (get m :type)))
|
||||
(define sxtp/cond-message (fn (m) (get m :message)))
|
||||
(define sxtp/target (fn (m) (get m :target)))
|
||||
(define sxtp/mode (fn (m) (get m :mode)))
|
||||
(define sxtp/values (fn (m) (get m :values)))
|
||||
(define sxtp/only-if-missing? (fn (m) (= (get m :only-if-missing) true)))
|
||||
(define sxtp/transition? (fn (m) (= (get m :transition) true)))
|
||||
|
||||
;; ── status helpers (build responses) ───────────────────────────────
|
||||
(define sxtp/ok (fn (body) (sxtp/response "ok" {:body body})))
|
||||
@@ -121,7 +143,9 @@
|
||||
{:request (list :verb :path :headers :cookies :params :capabilities :body)
|
||||
:response (list :status :headers :set-cookie :body :stream)
|
||||
:condition (list :type :message :path :retry :detail)
|
||||
:event (list :type :id :body :time)})
|
||||
:event (list :type :id :body :time)
|
||||
:patch (list :target :mode :body :transition)
|
||||
:signals (list :values :only-if-missing)})
|
||||
;; A nested SXTP message (a condition/event in a :body) serialises in its own
|
||||
;; list form; plain data values go through the serialize primitive.
|
||||
(define sxtp/-emit-value
|
||||
|
||||
Reference in New Issue
Block a user