Created
October 30, 2019 13:43
-
-
Save gusbicalho/fb4a614af9efd212fcf3e5b772c2c27a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns components-example | |
(:require [com.stuartsierra.component :as component])) | |
(defprotocol Counter | |
(get-c [this]) | |
(inc-c! [this])) | |
(defrecord SimpleCounter [counter] | |
component/Lifecycle | |
(start [this] | |
(assoc this | |
:counter (atom 0))) | |
(stop [this] | |
(dissoc this :counter)) | |
Counter | |
(get-c [_] | |
@counter) | |
(inc-c! [_] | |
(swap! counter inc))) | |
(defn make-simple-counter [] | |
(map->SimpleCounter {})) | |
(get-c (component/start (make-simple-counter))) | |
(defprotocol AccessMetrics | |
(get-metrics [_])) | |
(defrecord CounterWithAccessMetrics [counter-impl | |
access-counter] | |
component/Lifecycle | |
(start [this] | |
(assoc this | |
:access-counter (atom {:get 0 | |
:inc 0}))) | |
(stop [this] | |
(dissoc this :access-counter)) | |
Counter | |
(get-c [_] | |
(swap! access-counter update :get inc) | |
(get-c counter-impl)) | |
(inc-c! [_] | |
(swap! access-counter update :inc inc) | |
(inc-c! counter-impl)) | |
AccessMetrics | |
(get-metrics [_] | |
@access-counter)) | |
(defn make-counter-w-access-metrics [] | |
(map->CounterWithAccessMetrics {})) | |
(def system | |
(component/start-system | |
(component/system-map | |
:counter (component/using | |
(make-counter-w-access-metrics) | |
[:counter-impl]) | |
:counter-impl (make-simple-counter)))) | |
(inc-c! (:counter system)) | |
(get-metrics (:counter system)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment