-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clojure sdk: Fixes, clojure snippets (#672)
* Clojure sdk: Fixes, new website snippets Docs: corrected the readme's example Fix: changed return value in the jetty implementation Chore: Changed dependency coordinate of http-kit to a newer maven dependency Fix: using setTimeout in the redirect sugar function Fix: mistake in http-kit management in examples Docs: redirect example Feature: added another testing utility Docs: added missing snippets for the Datastar website * Fix: typo
- Loading branch information
Showing
20 changed files
with
415 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
;; NOTE: Track the next release of http-kit to switch to maven dep | ||
{:paths ["src/main"] | ||
:deps {http-kit/http-kit {:git/url "https://github.com/http-kit/http-kit" :git/sha "76b869fc34536ad0c43afa9a98d971a0fc32c644"}}} | ||
:deps {http-kit/http-kit {:mvn/version "2.9.0-alpha2"}}} | ||
|
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
(ns examples.redirect | ||
(:require | ||
[examples.common :as c] | ||
[examples.utils :as u] | ||
[dev.onionpancakes.chassis.core :refer [html]] | ||
[reitit.ring :as rr] | ||
[ring.util.response :as ruresp] | ||
[starfederation.datastar.clojure.api :as d*] | ||
[starfederation.datastar.clojure.adapter.http-kit :refer [->sse-response]])) | ||
|
||
|
||
(def home-page | ||
(html | ||
(c/page-scaffold | ||
[[:h1 "Test page"] | ||
[:div.#indicator | ||
[:button {:data-on-click (d*/sse-get "/redirect-me")} | ||
"Start redirect"]]]))) | ||
|
||
|
||
(defn home [_] | ||
(ruresp/response home-page)) | ||
|
||
|
||
(def guide-page | ||
(html | ||
(c/page-scaffold | ||
[[:h1 "You have been redirected"] | ||
[:a {:href "/" } "Home"]]))) | ||
|
||
|
||
(defn guide [_] | ||
(ruresp/response guide-page)) | ||
|
||
|
||
(defn redirect-handler [ring-request] | ||
(->sse-response ring-request | ||
{:on-open | ||
(fn [sse] | ||
(d*/merge-fragment! sse | ||
(html [:div#indicator "Redirecting in 3 seconds..."])) | ||
(Thread/sleep 3000) | ||
(d*/redirect! sse "/guide") | ||
(d*/close-sse! sse))})) | ||
|
||
|
||
|
||
|
||
(def router (rr/router | ||
[["/" {:handler home}] | ||
["/guide" {:handler guide}] | ||
["/redirect-me" {:handler redirect-handler}]])) | ||
|
||
|
||
(def default-handler (rr/create-default-handler)) | ||
|
||
|
||
(def handler | ||
(rr/ring-handler router default-handler)) | ||
|
||
|
||
|
||
(comment | ||
(u/reboot-hk-server! #'handler)) | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
(ns examples.snippets.polling1 | ||
(:require | ||
[dev.onionpancakes.chassis.core :refer [html]] | ||
[starfederation.datastar.clojure.api :as d*] | ||
[starfederation.datastar.clojure.adapter.test :refer [->sse-response]])) | ||
|
||
|
||
(comment | ||
(require | ||
'[starfederation.datastar.clojure.api :as d*] | ||
'[starfederation.datastar.clojure.adapter.http-kit :refer [->sse-response]] | ||
'[some.hiccup.library :refer [html]])) | ||
|
||
(import | ||
'java.time.format.DateTimeFormatter | ||
'java.time.LocalDateTime) | ||
|
||
(def formatter (DateTimeFormatter/ofPattern "YYYY-MM-DD HH:mm:ss")) | ||
|
||
(defn handler [ring-request] | ||
(->sse-response ring-request | ||
{:on-open | ||
(fn [sse] | ||
(d*/merge-fragment! sse | ||
(html [:div#time {:data-on-interval__duration.5s (d*/sse-get "/endpoint")} | ||
(LocalDateTime/.format (LocalDateTime/now) formatter)])) | ||
(d*/close-sse! sse))})) | ||
|
||
(comment | ||
(handler {})) | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
(ns examples.snippets.polling2 | ||
(:require | ||
[dev.onionpancakes.chassis.core :refer [html]] | ||
[starfederation.datastar.clojure.api :as d*] | ||
[starfederation.datastar.clojure.adapter.test :as at :refer [->sse-response]])) | ||
|
||
|
||
(comment | ||
(require | ||
'[starfederation.datastar.clojure.api :as d*] | ||
'[starfederation.datastar.clojure.adapter.http-kit :refer [->sse-response]] | ||
'[some.hiccup.library :refer [html]])) | ||
|
||
(import | ||
'java.time.format.DateTimeFormatter | ||
'java.time.LocalDateTime) | ||
|
||
(def date-time-formatter (DateTimeFormatter/ofPattern "YYYY-MM-DD HH:mm:ss")) | ||
(def seconds-formatter (DateTimeFormatter/ofPattern "ss")) | ||
|
||
(defn handler [ring-request] | ||
(->sse-response ring-request | ||
{:on-open | ||
(fn [sse] | ||
(let [now (LocalDateTime/now) | ||
current-time (LocalDateTime/.format now date-time-formatter) | ||
seconds (LocalDateTime/.format now seconds-formatter) | ||
duration (if (neg? (compare seconds "50")) | ||
"5" | ||
"1")] | ||
(d*/merge-fragment! sse | ||
(html [:div#time {(str "data-on-interval__duration." duration "s") | ||
(d*/sse-get "/endpoint")} | ||
current-time])) | ||
|
||
(d*/close-sse! sse)))})) | ||
|
||
|
||
(comment | ||
(handler {})) | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
(ns examples.snippets.redirect1 | ||
(:require | ||
[dev.onionpancakes.chassis.core :refer [html]] | ||
[starfederation.datastar.clojure.api :as d*] | ||
[starfederation.datastar.clojure.adapter.test :refer [->sse-response]])) | ||
|
||
|
||
(comment | ||
(require | ||
'[starfederation.datastar.clojure.api :as d*] | ||
'[starfederation.datastar.clojure.adapter.http-kit :refer [->sse-response]] | ||
'[some.hiccup.library :refer [html]])) | ||
|
||
|
||
(defn handler [ring-request] | ||
(->sse-response ring-request | ||
{:on-open | ||
(fn [sse] | ||
(d*/merge-fragment! sse | ||
(html [:div#indicator "Redirecting in 3 seconds..."])) | ||
(Thread/sleep 3000) | ||
(d*/execute-script! sse "window.location = \"/guide\"") | ||
(d*/close-sse! sse))})) | ||
|
||
(comment | ||
(handler {})) | ||
|
||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
(ns examples.snippets.redirect2 | ||
(:require | ||
[dev.onionpancakes.chassis.core :refer [html]] | ||
[starfederation.datastar.clojure.api :as d*] | ||
[starfederation.datastar.clojure.adapter.test :refer [->sse-response]])) | ||
|
||
|
||
(comment | ||
(require | ||
'[starfederation.datastar.clojure.api :as d*] | ||
'[starfederation.datastar.clojure.adapter.http-kit :refer [->sse-response]] | ||
'[some.hiccup.library :refer [html]])) | ||
|
||
|
||
(defn handler [ring-request] | ||
(->sse-response ring-request | ||
{:on-open | ||
(fn [sse] | ||
(d*/merge-fragment! sse | ||
(html [:div#indicator "Redirecting in 3 seconds..."])) | ||
(Thread/sleep 3000) | ||
(d*/execute-script! sse | ||
"setTimeout(() => window.location = \"/guide\"") | ||
(d*/close-sse! sse))})) | ||
|
||
|
||
(comment | ||
(handler {})) | ||
|
||
|
Oops, something went wrong.