Skip to content

Commit 6b9ae71

Browse files
committed
AOT-ify build and minor improvements
1 parent d3bbccb commit 6b9ae71

File tree

6 files changed

+45
-14
lines changed

6 files changed

+45
-14
lines changed

README.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ finally right around the corner, all you need is this small example to get start
2323

2424
The purpose of this example application is to show packaging capabilities of cljfx and
2525
jpackage, to keep it simple some build steps where left out:
26-
- startup time can be improved significantly by AOT-compiling clojure code;
2726
- application package size can be reduced: you can use `jlink` to minify the JDK, and if
2827
your application does not need to use webkit (which is used in this example), you can
2928
exclude cljfx's dependency on javafx-web.
@@ -38,7 +37,13 @@ window.
3837

3938
The build process is 2-step:
4039
1. Assemble an uberjar. Here it's done using Sean Corfield's
41-
[depstar](https://github.com/seancorfield/depstar) library with `clj -A:uberjar` alias.
40+
[depstar](https://github.com/seancorfield/depstar) library:
41+
- `clj -Spom`
42+
- `clj -X:uberjar`
43+
44+
The built uberjar will include AOT-compiled code and can be executed via
45+
`java -jar dist/hn.jar`, but we will use `jpackage` to build OS distributions:
46+
4247
2. Use `jpackage` with common options described in [jpackage/common](jpackage/common) and
4348
platform-specific options having their own files: [jpackage/linux](jpackage/linux),
4449
[jpackage/mac](jpackage/mac) and [jpackage/windows](jpackage/windows). For example, if you

build/uberjar/build.clj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
(ns uberjar.build
2+
(:require [hf.depstar.uberjar])
3+
(:import (javafx.application Platform)))
4+
5+
;; see https://github.com/cljfx/cljfx#aot-compilation-is-complicated
6+
7+
(defn run
8+
[& args]
9+
(try
10+
(apply hf.depstar.uberjar/run args)
11+
(finally
12+
(Platform/exit))))

deps.edn

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1-
{:deps {cljfx {:mvn/version "1.6.7"}
1+
{:deps {cljfx/cljfx {:mvn/version "1.6.7"}
22
cljfx/css {:mvn/version "1.1.0"}
3-
clj-http {:mvn/version "3.9.1"}
3+
clj-http/clj-http {:mvn/version "3.9.1"}
44
metosin/jsonista {:mvn/version "0.2.5"}
5-
crouton {:mvn/version "0.1.2"}}
6-
:aliases {:uberjar {:extra-deps {seancorfield/depstar {:mvn/version "0.5.2"}}
7-
:main-opts ["-m" "hf.depstar.uberjar" "dist/hn.jar"]}}}
5+
crouton/crouton {:mvn/version "0.1.2"}}
6+
:aliases {:uberjar {:extra-deps {seancorfield/depstar {:mvn/version "1.1.136"}}
7+
:replace-paths ["src" "build"]
8+
:exec-fn uberjar.build/run
9+
:exec-args {:jar "dist/hn.jar"
10+
:aot true
11+
:main-class "hn.core"}}}}

src/hn/core.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[hn.event :as event]
55
[hn.view :as view])
66
(:import [javafx.application Platform]
7-
[java.util.concurrent Executors ThreadFactory]))
7+
[java.util.concurrent Executors ThreadFactory])
8+
(:gen-class))
89

910
(defn http-effect [v dispatch!]
1011
(try
@@ -23,7 +24,7 @@
2324
(let [*counter (atom 0)
2425
factory (reify ThreadFactory
2526
(newThread [_ runnable]
26-
(doto (Thread. runnable (str "reveal-agent-pool-" (swap! *counter inc)))
27+
(doto (Thread. runnable (str "clfjx-hn-agent-pool-" (swap! *counter inc)))
2728
(.setDaemon true))))]
2829
(Executors/newCachedThreadPool factory)))
2930

src/hn/event.clj

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,17 @@
1111

1212
(defmulti handle ::type)
1313

14+
(defmethod handle ::exception [{:keys [state exception]}]
15+
{:state (assoc state :error (.getMessage exception))})
16+
1417
(defmethod handle ::load-stories [_]
1518
{:http {:method :get
1619
:url "https://hacker-news.firebaseio.com/v0/topstories.json"
17-
:on-response {::type ::process-stories}}})
20+
:on-response {::type ::process-stories}
21+
:on-exception {::type ::exception}}})
1822

1923
(defmethod handle ::process-stories [{:keys [state response]}]
20-
(let [stories (parse-response-body response)]
24+
(let [stories (take 100 (parse-response-body response))]
2125
(into [[:state (assoc state :stories stories)]]
2226
(map (fn [id]
2327
[:dispatch {::type ::load-story :id id}]))
@@ -26,7 +30,8 @@
2630
(defmethod handle ::load-story [{:keys [id]}]
2731
{:http {:method :get
2832
:url (str "https://hacker-news.firebaseio.com/v0/item/" id ".json")
29-
:on-response {::type ::process-story :id id}}})
33+
:on-response {::type ::process-story :id id}
34+
:on-exception {::type ::exception}}})
3035

3136
(defmethod handle ::process-story [{:keys [state id response]}]
3237
{:state (assoc-in state [:items id] (parse-response-body response))})

src/hn/view.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"-comment" {"-cell" {:-fx-padding [small-spacing default-spacing]}}
5454
"-stories" {"-item" {:-fx-spacing small-spacing}
5555
"-cell" {:-fx-padding [small-spacing default-spacing]}}}
56+
".error" {:-fx-text-fill :red}
5657
".list-cell:empty" {:-fx-background-color :transparent}
5758
".scroll-bar" {:-fx-background-color :transparent
5859
":vertical" {"> .increment-button > .increment-arrow"
@@ -115,7 +116,7 @@
115116
:cell-factory [:setter (fx.lifecycle/detached-prop-map fx.list-cell/props)
116117
:coerce create-cell-factory])))
117118

118-
(defn stories [{:keys [stories items]}]
119+
(defn stories [{:keys [stories items error]}]
119120
{:fx/type :v-box
120121
:children
121122
[{:fx/type :h-box
@@ -126,7 +127,10 @@
126127
:text ""}
127128
{:fx/type :label
128129
:style-class "hn-title"
129-
:text "Hacker News"}]}
130+
:text "Hacker News"}
131+
{:fx/type :label
132+
:style-class "error"
133+
:text error}]}
130134
{:fx/type ext-with-list-cell-factory
131135
:v-box/vgrow :always
132136
:props

0 commit comments

Comments
 (0)