Skip to content

Commit 0ae404b

Browse files
committed
Feature: clojure-SDK babashka support
Doc: some examples ported to bb Refactor: Cleaned up the bb tasks helpers Feature: bb tests in the github workflow
1 parent e2f552c commit 0ae404b

File tree

17 files changed

+1028
-123
lines changed

17 files changed

+1028
-123
lines changed

.github/workflows/release-sdk.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,12 @@ jobs:
5252
key: cljdeps-${{ hashFiles('**/deps.edn') }}
5353
restore-keys: cljdeps-
5454

55-
- name: Run test suite
55+
- name: Run Clojure test suite
5656
run: bb test:all
5757

58+
- name: Run Babashka test suite
59+
run: bb test:bb
60+
5861
- name: Build jar artifacts
5962
run: bb jar:all
6063

bb.edn

Lines changed: 123 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,126 @@
1-
{:paths ["src/main" "src/bb"]
1+
{:paths ["src/bb"
2+
"sdk/src/main"
3+
"sdk-adapter-http-kit/src/main/"
4+
"sdk-malli-schemas/src/main"
5+
"./src/test/core-sdk"
6+
"./src/test/malli-schemas"]
7+
8+
:deps {io.github.noahtheduke/lazytest {:mvn/version "1.8.0"}
9+
metosin/malli {:mvn/version "0.17.0"}}
10+
211
:tasks
312
{:requires ([tasks :as t])
413

5-
-prep (t/prep-libs)
6-
7-
dev {:task (t/dev :http-kit :ring-jetty :malli-schemas)}
8-
9-
dev:rj9a {:task (t/dev :http-kit :ring-rj9a :malli-schemas)}
10-
11-
dev:empty (t/dev :malli-schemas)
12-
13-
test:all (t/lazytest [:http-kit
14-
:ring-jetty
15-
:malli-schemas
16-
:sdk-brotli]
17-
[:test.paths/core-sdk
18-
:test.paths/malli-schemas
19-
:test.paths/brotli
20-
:test.paths/adapter-ring
21-
:test.paths/adapter-http-kit
22-
:test.paths/adapter-ring-jetty])
23-
24-
test:all-w (t/lazytest [:http-kit
25-
:ring-jetty
26-
:malli-schemas
27-
:sdk-brotli]
28-
[:test.paths/core-sdk
29-
:test.paths/malli-schemas
30-
:test.paths/brotli
31-
:test.paths/adapter-ring
32-
:test.paths/adapter-ring-jetty
33-
:test.paths/adapter-http-kit]
34-
"--watch"
35-
"--delay 1000")
36-
37-
test:rj9a (t/lazytest [:http-kit
38-
:ring-rj9a]
39-
[:test.paths/core-sdk
40-
:test.paths/adapter-ring
41-
:test.paths/adapter-rj9a])
42-
43-
jar:sdk {:doc "Build jar for the common sdk"
44-
:task (t/lib-jar! t/sdk-dir)}
45-
46-
jar:sdk-adapter-ring {:doc "Build jar for the adapter-ring"
47-
:task (t/lib-jar! t/sdk-adapter-ring-dir)}
48-
49-
jar:sdk-adapter-http-kit {:doc "Build jar for the adapter-http-kit"
50-
:task (t/lib-jar! t/sdk-adapter-http-kit-dir)}
51-
52-
jar:sdk-brotli {:doc "Build jar for the Brotli library"
53-
:task (t/lib-jar! t/sdk-brotli-dir)}
54-
55-
jar:sdk-malli-schemas {:doc "Build jar for the malli-schemas"
56-
:task (t/lib-jar! t/sdk-malli-schemas-dir)}
57-
58-
jar:all {:doc "Build the jar for all the libs"
59-
:depends [clean
60-
jar:sdk
61-
jar:sdk-adapter-ring
62-
jar:sdk-adapter-http-kit
63-
jar:sdk-brotli
64-
jar:sdk-malli-schemas]}
65-
66-
install:all {:doc "Install all libraries"
67-
:depends [clean]
68-
:task (doseq [dir t/sdk-lib-dirs]
69-
(t/lib-install! dir))}
70-
71-
clean {:doc "Clean build artifacts"
72-
:task (doseq [dir t/sdk-lib-dirs]
73-
(clojure {:dir dir} "-T:build clean"))}
74-
75-
set-version {:doc "Set the version in all libs"
76-
:task (doseq [dir t/sdk-lib-dirs]
77-
(t/lib-set-version! dir (first *command-line-args*)))}
78-
79-
bump-version {:doc "Bump the version component in all libs. First argument must be one of: major, minor, patch"
80-
:task (doseq [dir sdk-lib-dirs]
81-
(let [component (first *command-line-args*)]
82-
(t/lib-bump! dir component)))}
83-
84-
publish:all {:doc "Publish the clojure sdk libs to clojars"
85-
:depends [jar:all]
86-
:task (doseq [dir sdk-lib-dirs]
87-
(t/lib-publish! dir))}}}
14+
dev
15+
{:doc "Starts a dev repl. Additional deps aliases can be passed as arguments."
16+
:task (t/dev :http-kit :ring-jetty :malli-schemas)}
17+
18+
dev:rj9a
19+
{:doc "Same as `dev` with ring-jetty replaced with rj9a."
20+
:task (t/dev :http-kit :ring-rj9a :malli-schemas)}
21+
22+
dev:empty
23+
{:doc "Dev repl with no adapters."
24+
:task (t/dev :malli-schemas)}
25+
26+
dev:bb
27+
{:doc "Start a babashka nrepl server. An addr like `localhost:1234` can be passed as argument."
28+
:task (t/dev-bb)}
29+
30+
test:all
31+
{:doc "Run all tests once."
32+
:task (t/lazytest [:http-kit
33+
:ring-jetty
34+
:malli-schemas
35+
:sdk-brotli]
36+
[:test.paths/core-sdk
37+
:test.paths/malli-schemas
38+
:test.paths/brotli
39+
:test.paths/adapter-ring
40+
:test.paths/adapter-http-kit
41+
:test.paths/adapter-ring-jetty])}
42+
43+
test:all-w
44+
{:doc "Watch all tests."
45+
:task (t/lazytest [:http-kit
46+
:ring-jetty
47+
:malli-schemas
48+
:sdk-brotli]
49+
[:test.paths/core-sdk
50+
:test.paths/malli-schemas
51+
:test.paths/brotli
52+
:test.paths/adapter-ring
53+
:test.paths/adapter-ring-jetty
54+
:test.paths/adapter-http-kit]
55+
"--watch"
56+
"--delay 1000")}
57+
58+
test:rj9a
59+
{:doc "Same as `test:all` with ring-jetty replaced by rj9a."
60+
:task (t/lazytest [:http-kit
61+
:ring-rj9a]
62+
[:test.paths/core-sdk
63+
:test.paths/adapter-ring
64+
:test.paths/adapter-rj9a])}
65+
66+
test:bb
67+
{:doc "Run core unit tests with babashka."
68+
:task (t/bb-lazytest [:test.paths/core-sdk
69+
:test.paths/malli-schemas])}
70+
71+
jar:sdk
72+
{:doc "Build jar for the common sdk"
73+
:task (t/lib-jar! t/sdk-dir)}
74+
75+
jar:sdk-adapter-ring
76+
{:doc "Build jar for the adapter-ring"
77+
:task (t/lib-jar! t/sdk-adapter-ring-dir)}
78+
79+
jar:sdk-adapter-http-kit
80+
{:doc "Build jar for the adapter-http-kit"
81+
:task (t/lib-jar! t/sdk-adapter-http-kit-dir)}
82+
83+
jar:sdk-brotli
84+
{:doc "Build jar for the Brotli library"
85+
:task (t/lib-jar! t/sdk-brotli-dir)}
86+
87+
jar:sdk-malli-schemas
88+
{:doc "Build jar for the malli-schemas"
89+
:task (t/lib-jar! t/sdk-malli-schemas-dir)}
90+
91+
jar:all
92+
{:doc "Build the jar for all the libs"
93+
:depends [clean
94+
jar:sdk
95+
jar:sdk-adapter-ring
96+
jar:sdk-adapter-http-kit
97+
jar:sdk-brotli
98+
jar:sdk-malli-schemas]}
99+
100+
install:all
101+
{:doc "Install all libraries"
102+
:depends [clean]
103+
:task (doseq [dir t/sdk-lib-dirs]
104+
(t/lib-install! dir))}
105+
106+
clean
107+
{:doc "Clean build artifacts"
108+
:task (doseq [dir t/sdk-lib-dirs]
109+
(clojure {:dir dir} "-T:build clean"))}
110+
111+
set-version
112+
{:doc "Set the version in all libs"
113+
:task (doseq [dir t/sdk-lib-dirs]
114+
(t/lib-set-version! dir (first *command-line-args*)))}
115+
116+
bump-version
117+
{:doc "Bump the version component in all libs. First argument must be one of: major, minor, patch"
118+
:task (doseq [dir sdk-lib-dirs]
119+
(let [component (first *command-line-args*)]
120+
(t/lib-bump! dir component)))}
121+
122+
publish:all
123+
{:doc "Publish the clojure sdk libs to clojars"
124+
:depends [jar:all]
125+
:task (doseq [dir sdk-lib-dirs]
126+
(t/lib-publish! dir))}}}

sdk-adapter-http-kit/src/main/starfederation/datastar/clojure/adapter/http_kit/impl.clj renamed to sdk-adapter-http-kit/src/main/starfederation/datastar/clojure/adapter/http_kit/impl.cljc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
[starfederation.datastar.clojure.utils :as u]
66
[org.httpkit.server :as hk-server])
77
(:import
8-
[java.util.concurrent.locks ReentrantLock]
9-
[java.io ByteArrayOutputStream Closeable]))
8+
java.lang.Exception
9+
[java.io ByteArrayOutputStream Closeable]
10+
[java.util.concurrent.locks ReentrantLock]))
1011

1112

1213
(def basic-profile
@@ -93,9 +94,10 @@
9394

9495
(sse-gen? [_] true)
9596

96-
Closeable
97-
(close [this]
98-
(p/close-sse! this)))
97+
#?@(:bb []
98+
:clj [Closeable
99+
(close [this]
100+
(p/close-sse! this))]))
99101

100102

101103
(defn ->sse-gen

sdk/src/main/starfederation/datastar/clojure/adapter/test.clj renamed to sdk/src/main/starfederation/datastar/clojure/adapter/test.cljc

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@
55
[starfederation.datastar.clojure.protocols :as p]
66
[starfederation.datastar.clojure.utils :as u])
77
(:import
8-
[java.util.concurrent.locks ReentrantLock]
9-
[java.io Closeable]))
8+
#?(:clj [java.io Closeable])
9+
java.lang.StringBuilder
10+
[java.util.concurrent.locks ReentrantLock]))
1011

1112

1213

@@ -46,9 +47,10 @@
4647

4748
(sse-gen? [_] true)
4849

49-
Closeable
50-
(close [this]
51-
(p/close-sse! this)))
50+
#?@(:bb []
51+
:clj [Closeable
52+
(close [this]
53+
(p/close-sse! this))]))
5254

5355

5456
(defn ->sse-response

src/bb-example/bb.edn

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{:paths ["src/main"
2+
"../../sdk/src/main"
3+
"../../sdk-adapter-http-kit/src/main/"
4+
"../../sdk-malli-schemas/src/main"]
5+
:deps {metosin/malli {:mvn/version "0.17.0"}
6+
org.clojars.askonomm/ruuter {:mvn/version "1.3.5"}
7+
ring/ring-core {:mvn/version "1.14.2"}}}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
(ns bb-example.animation
2+
(:require
3+
[bb-example.animation.broadcast :as broadcast]
4+
[bb-example.animation.handlers :as handlers]
5+
[bb-example.animation.rendering :as rendering]
6+
[bb-example.animation.state :as state]
7+
[bb-example.common :as c]
8+
[bb-example.core :as core]
9+
[ruuter.core :as ruuter]
10+
[ring.middleware.params :as r-params]
11+
[starfederation.datastar.clojure.adapter.http-kit :as hk-gen]
12+
13+
[starfederation.datastar.clojure.adapter.http-kit-schemas]
14+
[starfederation.datastar.clojure.api-schemas]))
15+
16+
;; This example let's use play with fat updates and compression
17+
;; to get an idea of the gains compression can help use achieve
18+
;; in terms of network usage.
19+
(broadcast/install-watch!)
20+
21+
(def routes
22+
[(c/GET "/" handlers/home-handler)
23+
(c/GET "/ping/:id" (r-params/wrap-params handlers/ping-handler))
24+
(c/GET "/random-10" handlers/random-pings-handler)
25+
(c/GET "/reset" handlers/reset-handler)
26+
(c/GET "/step1" handlers/step-handler)
27+
(c/GET "/play" handlers/play-handler)
28+
(c/GET "/pause" handlers/pause-handler)
29+
(c/GET "/refresh" handlers/refresh-handler)
30+
(c/POST "/resize" (r-params/wrap-params handlers/resize-handler))
31+
(c/GET "/updates" (handlers/->updates-handler
32+
{hk-gen/write-profile hk-gen/gzip-profile}))])
33+
34+
(defn handler [req]
35+
(ruuter/route routes req))
36+
37+
38+
(defn clear-terminal! []
39+
(binding [*out* (java.io.PrintWriter. System/out)]
40+
(print "\033c")
41+
(flush)))
42+
43+
44+
45+
(comment
46+
(clear-terminal!)
47+
(core/start! #'handler)
48+
*e
49+
state/!state
50+
state/!conns
51+
(reset! state/!conns #{})
52+
53+
(-> state/!state
54+
deref
55+
rendering/page)
56+
(state/resize! 10 10)
57+
(state/resize! 20 20)
58+
(state/resize! 25 25)
59+
(state/resize! 30 30)
60+
(state/resize! 50 50)
61+
(state/reset-state!)
62+
(state/add-random-pings!)
63+
(state/step-state!)
64+
(state/start-animating!))
65+
66+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(ns bb-example.animation.broadcast
2+
(:require
3+
[bb-example.animation.rendering :as rendering]
4+
[bb-example.animation.state :as state]
5+
[starfederation.datastar.clojure.api :as d*]))
6+
7+
8+
(defn send-frame! [sse frame]
9+
(try
10+
(d*/patch-elements! sse frame)
11+
(catch Exception e
12+
(println e))))
13+
14+
15+
(defn broadcast-new-frame! [frame]
16+
(let [sses @state/!conns]
17+
(doseq [sse sses]
18+
(send-frame! sse frame))))
19+
20+
21+
(defn install-watch! []
22+
(add-watch state/!state ::watch
23+
(fn [_k _ref old new]
24+
(when-not (identical? old new)
25+
(let [frame (rendering/render-content new)]
26+
(broadcast-new-frame! frame))))))
27+

0 commit comments

Comments
 (0)