-
Notifications
You must be signed in to change notification settings - Fork 0
/
feature_suspend.clj
51 lines (43 loc) · 2.11 KB
/
feature_suspend.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
(ns example.feature-suspend
"Example of suspending components."
(:require [strojure.fitter.component :as component]
[strojure.fitter.system :as system]))
(set! *warn-on-reflection* true)
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
(def ^:private simple-component
(component/of
(fn [_]
(println "Start" 'simple-component)
{:simple-component nil})
(fn [instance]
(println "Stop" 'simple-component instance))))
(def ^:private suspendable-component
(component/of
(fn [{:system/keys [simple]}]
(println "Start" 'suspendable-component simple)
{:suspending-with-stop simple})
(fn [instance]
(println "Stop" 'suspendable-component instance))
(fn [instance old-system]
(println "Suspend" 'suspendable-component)
(fn resume [{:system/keys [simple]}]
(println "Resume" 'suspendable-component simple)
{:suspending-with-stop simple
:resumed {:old-instance instance
:old-simple (:simple old-system)}}))))
(def ^:private registry
{:system/simple simple-component
:system/suspending suspendable-component})
(def ^:private my-system!
(system/init {:registry registry}))
(comment
(-> (system/inspect my-system!)
(select-keys [:suspended :deps :system]))
(system/start! my-system!)
(system/stop! my-system! {:suspend true})
(system/stop! my-system! {:suspend true :filter-keys #{:system/simple}})
(system/start! my-system! {:registry (dissoc registry :system/suspending)})
(system/start! my-system! {:filter-keys #{:system/suspending}})
(system/stop! my-system!)
)
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••