Fork me on GitHub
#component
<
2016-06-13
>
roberto03:06:10

has anyone had the need to update a component in place. I have a situation where I created a component that connects to a third party api and holds an auth token that expires in 24 hours. So I need to update that token periodically.

roberto03:06:36

this is an example of how my component looks:

(defprotocol Authentication
  (authenticate [this]))

(defrecord QBaseAPI [username password app-token]
  Authentication
  (authenticate [this]
    (let [ticket (-> (auth username password) :body xml->ticket)]
      (assoc this :ticket ticket)))

  component/Lifecycle
  (start [this]
    (authenticate this))

  (stop [this]
    (assoc this :username nil :password nil :app-token nil :ticket nil)))

(defn create-qbaseapi-component
  [cfg]
  (let [username  (:username cfg)
        password  (:password cfg)
        app-token (:app-token cfg)]
    (->QBaseAPI username password app-token)))

donaldball03:06:17

I’m working on something similar right now, actually. The model I’m trying to implement involves a worker being spawned at start to refresh the state, with the component being derefable to yield the state.

hiredman03:06:57

this (https://github.com/hiredman/songs-of-future-past/blob/master/src/com/manigfeald/sofp/history.clj#L51-L97) extremely gross code spawns a worker that writes to some atoms periodically

roberto03:06:29

I was thinking of having a global atom, but it doesn’t feel right

hiredman03:06:53

the atoms aren't global, they are values for keys in the component