Last active
May 2, 2025 11:02
-
-
Save yuhan0/5e3aaccbb1be1925ed4bf066a692d50a to your computer and use it in GitHub Desktop.
Datastar debounce / throttle repro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (ns app.repro | |
| (:require [hyperlith.core :as h] | |
| [clojure.string :as str])) | |
| (defn render [_req] | |
| (h/html | |
| [:main#morph.main | |
| [:div {:style {:display :grid | |
| :grid-gap :1em | |
| :justify-items :stretch | |
| :grid-template-columns (repeat 4 "1fr")}} | |
| [:style "code,input{width:100%;margin:auto 1em}"] | |
| (for [[sig & tags] '[[t1] [t2 noleading] [t3 trail] [t4 noleading trail] | |
| [d1] [d2 leading] [d3 notrail] [d4 leading notrail]] | |
| :let [t? (= \t (first (name sig))) | |
| label (str/join "." | |
| (list* (if t? "throttle" "debounce") tags)) | |
| attr (keyword (str "data-on-keydown" | |
| (str/join "." | |
| (list* (if t? "__throttle" "__debounce") "500ms" tags)))) | |
| prop (format "evt.key==='Backspace' ? $%s = '' : evt.key!=='Tab' ? $%s += evt.key : null" sig sig) | |
| init-attr (str "data-signals-" sig "__ifmissing")]] | |
| [:div {init-attr "''"} | |
| [:code label] | |
| [:br] | |
| [:div {:style {:display :flex :align-items :center}} | |
| [:input {attr prop}] | |
| [:code {:data-text (str "$" sig)}]]])]])) | |
| (def router | |
| (h/router | |
| {[:get "/"] (h/shim-handler [:title {} "repro"]) | |
| [:post "/"] (h/render-handler #'render)})) | |
| (defn -main [& _] | |
| (h/start-app | |
| {:router #'router | |
| :ctx-start (fn []) | |
| :ctx-stop (fn [_]) | |
| :max-refresh-ms 100 | |
| :csrf-secret (h/env :csrf-secret)})) | |
| (h/refresh-all!) | |
| (comment | |
| (-main) | |
| (clojure.java.browse/browse-url "http://localhost:8080/") | |
| ;; stop server | |
| (((h/get-app) :stop)) | |
| ,) |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's actually zero server-side logic going on here, the above just compiles to the following html