This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2016-07-13
Channels
- # admin-announcements (4)
- # beginners (14)
- # boot (41)
- # capetown (1)
- # carry (10)
- # clojure (168)
- # clojure-czech (11)
- # clojure-mexico (3)
- # clojure-quebec (1)
- # clojure-russia (63)
- # clojure-spec (108)
- # clojure-uk (44)
- # clojurescript (37)
- # component (5)
- # data-science (2)
- # datascript (5)
- # datomic (3)
- # defnpodcast (9)
- # dirac (14)
- # emacs (18)
- # events (1)
- # funcool (2)
- # garden (2)
- # hoplon (48)
- # leiningen (6)
- # numerical-computing (1)
- # off-topic (8)
- # om (61)
- # onyx (22)
- # proton (14)
- # re-frame (50)
- # reagent (2)
- # uncomplicate (1)
- # untangled (41)
- # vim (5)
- # yada (5)
For testing I want to provide different implementations of a component but that component does not actually have any kind of lifecycle and instead just implements a simple protocol:
(defrecord StripeApi [token]
IStripe
(-request [_ method resource params]
(let [verb (case method
:get http/get
:post http/post
:delete http/delete)]
(-> (verb (build-uri resource)
{:query-params params
:basic-auth [token]
:as :json})
(d/catch #(throw (ex-info "Error after calling Stripe API"
{:body (-> % ex-data :body bs/to-string json/decode)
:status (:status %)
:method method :params params}))))))
component/Lifecycle
(start [component] component)
(stop [component] component))
Is there anything wrong with this approach? My thinking is that this will make it easier to swap with a different thing (e.g. an atom that stores requests made). If there are other approaches that serve the same purpose I'd be curious to hear about them as well@martinklepsch: That's an intended use case for component. You do not have to implement Lifecycle
— there is a default no-op implementation for Object
.
If the component has any dependencies, it must be a record or map.
@stuartsierra: thanks!
You're welcome.