Fork me on GitHub
#component
<
2018-07-27
>
guy10:07:14

Bit of an odd question

guy10:07:21

When you create a component using a defrecord

guy10:07:34

and you implement the lifecycle protocol

guy10:07:46

Does the key you assoc as the last line become a base field?

guy10:07:54

(defrecord Person [name age])
;;=> user.Person

(def ertu (->Person "Ertu" 24))
;;=> #'user/ertu

(record? ertu)
;;=> true

(record? (assoc ertu :address "Somewhere"))
;;=> true

;;removing base fields converts record to regular map
(record? (dissoc ertu :name))
;;=> false

guy10:07:28

So with an example component

guy10:07:32

(defrecord ExampleComponent [options cache database scheduler]
  component/Lifecycle

  (start [this]
    (println ";; Starting ExampleComponent")
    ;; In the 'start' method, a component may assume that its
    ;; dependencies are available and have already been started.
    (assoc this :admin (get-user database "admin")))

  (stop [this]
    (println ";; Stopping ExampleComponent")
    ;; Likewise, in the 'stop' method, a component may assume that its
    ;; dependencies will not be stopped until AFTER it is stopped.
    this))

guy10:07:03

If inside the stop you did (dissoc this :admin) would that convert the record to a map?

guy10:07:09

like (record? (dissoc ertu :name))

gfredericks11:07:50

no, you can't add new base fields to a record

gfredericks11:07:03

so you should be able to safely dissoc

gfredericks11:07:11

easy to check that in a repl btw

guy12:07:22

ok thanks! :thumbsup: