-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_mount.clj
100 lines (87 loc) · 4.99 KB
/
feature_mount.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
(ns example.feature-mount
"Example of using mounted components."
(:require [strojure.fitter.component :as component]
[strojure.fitter.mount :as mount]
[strojure.fitter.system :as system]))
(set! *warn-on-reflection* true)
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
(defn test-function
"The function which can be used without system start."
{::component/start (fn [system]
(println "Start" 'test-function)
(constantly {:test-function (:system/x system)}))}
[]
{:test-function :default})
(def ^{::component/start (fn [system]
(println "Start" 'test-value)
{:test-value (:system/x system)})
::component/stop! (fn [inst]
(println "Stop" 'test-value inst))}
test-value
"The value which can be used without system start."
{:test-value :default})
(defn test-deps
"The function which depends on other mounted component."
{::component/start (fn [system]
(println "Start" 'test-deps)
(constantly {:test-deps (system `test-value)}))}
[]
{:test-deps :default})
(def ^{::component/start (fn [system]
(println "Start" 'test-suspend)
{:test-suspend (:system/x system)})
::component/stop! (fn [inst]
(println "Stop" 'test-suspend inst))
::component/suspend! (fn [inst _old-system]
(println "Suspend" 'test-suspend inst)
(fn [system]
(println "Resume" 'test-suspend)
{:test-suspend (:system/x system) :resumed true}))}
test-suspend
"The value which can suspend. It is unmounted when suspended."
{:test-suspend :default})
(declare ^{:doc "Declared but not initialized value which cannot be used without system start."
::component/start (fn [system]
(println "Start" 'test-declare)
(constantly {:test-declare (:system/x system)}))}
test-declare)
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
(def ^:private registry
(-> {:system/x (constantly :x)}
(mount/register-symbols #{`test-function `test-deps `test-value `test-suspend `test-declare})))
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
(defn- print-status
[]
(println "Status -" (test-function) test-value test-suspend (test-deps)
(try (test-declare) (catch Throwable e (ex-message e)))))
(comment
(do (print-status)
(with-open [system! (system/init)]
(system/start! system! {:registry registry})
(print-status)
(system/stop! system! {:suspend true})
(print-status)
(system/start! system!)
(print-status))
(print-status))
;Status - {:test-function :default} {:test-value :default} {:test-suspend :default} {:test-deps :default} Attempting to call unbound fn: #'example.how-to-mount/test-declare
;Start test-function
;Start test-deps
;Start test-value
;Start test-suspend
;Start test-declare
;Status - {:test-function :x} {:test-value :x} {:test-suspend :x} {:test-deps {:test-value :x}} {:test-declare :x}
;Stop test-value {:test-value :x}
;Suspend test-suspend {:test-suspend :x}
;Status - {:test-function :default} {:test-value :default} {:test-suspend :default} {:test-deps :default} Attempting to call unbound fn: #'example.how-to-mount/test-declare
;Start test-function
;Start test-deps
;Start test-value
;Resume test-suspend
;Start test-declare
;Status - {:test-function :x} {:test-value :x} {:test-suspend :x, :resumed true} {:test-deps {:test-value :x}} {:test-declare :x}
;Stop test-value {:test-value :x}
;Stop test-suspend {:test-suspend :x, :resumed true}
;Status - {:test-function :default} {:test-value :default} {:test-suspend :default} {:test-deps :default} Attempting to call unbound fn: #'example.how-to-mount/test-declare
)
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••