Hello, your code is acting ok, as you are not dissocing the data component
from the compute component during its "stop" method .
Remember, this is not object oriented programming. You removed the data
from the data component, but did nothing to the compute component. That is
the reason why the compute component still have a "copy" of the data
component with the old data.
If you want the "correct" behavior (depends of what you want) then your
compute component should look like this:
(defrecord MyComputeComponent [factor mydatacomp]
component/Lifecycle
(start [this]
(println "Starting MyComputeComponent")
this)
(stop [this]
(println "Stopping MyComputeComponent")
(dissoc this :mydatacomp)) ;; or (assoc this :mydatacomp nil))
Saludos!
El miércoles, 18 de enero de 2017, 8:28:24 (UTC-6), Jochen escribió:
>
> Hi…
>
> playing around with Stuart Sierras component library I found some behavior
> (code at end) that I find odd.
> M sample has just two components, data and compute, where data is injected
> into compute. On start data associates a data map and on stop dissociates
> it.
> compute uses a value from data's map to compute.
>
> What I found is that after system stop the started version of data is
> still associated to compute, while the data component in SystemMap is
> stopped. So, when do
>
> (def my-system (make-my-system {:factor 2}))
> (alter-var-root #'my-system component/start-system)
> (alter-var-root #'my-system component/stop-system)
> (my-compute (:mycomputecomp my-system) 16)
>
> my-compute should (as data map is nil'ed on stop) crash but doesn't.
>
> Calling
>
> (alter-var-root #'my-system component/stop-system)
>
> again fixes it and my-compute crashes as expected.
>
> Any ideas?
>
> Ciao
>
> …Jochen
>
> (ns foo.core
> (:require [com.stuartsierra.component :as component]))
>
> (defrecord MyDataComponent [data]
> component/Lifecycle
> (start [this]
> (println "Starting MyDataComponent")
> (assoc this :data {:foo 42}))
> (stop [this]
> (println "Stopping MyDataComponent")
> (dissoc this :data)))
>
> (defn my-data-component []
> (->MyDataComponent nil))
>
> (defn mydatacomp-foo [mydatacomp]
> (get-in mydatacomp [:data :foo]))
>
>
> (defrecord MyComputeComponent [factor mydatacomp]
> component/Lifecycle
> (start [this]
> (println "Starting MyComputeComponent")
> this)
> (stop [this]
> (println "Stopping MyComputeComponent")
> this))
>
> (defn my-compute-component [factor]
> (map->MyComputeComponent {:factor factor}))
>
> (defn my-compute [mycomputecomp offset]
> (let [factor (:factor mycomputecomp)
> foo (mydatacomp-foo (:mydatacomp mycomputecomp))]
> (+ (* factor foo) offset)))
>
> (defn make-my-system [config-options]
> (let [{:keys [factor]} config-options]
> (component/system-map
> :mydatacomp (my-data-component)
> :mycomputecomp (component/using
> (my-compute-component factor)
> [:mydatacomp]))))
>
>
> ;;;;;; running
> ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
> (comment
> (def my-system (make-my-system {:factor 2}))
> (alter-var-root #'my-system component/start-system)
> (alter-var-root #'my-system component/stop-system)
> (my-compute (:mycomputecomp my-system) 16)
> )
>
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.