Skip to content

Instantly share code, notes, and snippets.

@mostr
Created February 29, 2016 19:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mostr/372f65ece0ad6ea218d1 to your computer and use it in GitHub Desktop.
Save mostr/372f65ece0ad6ea218d1 to your computer and use it in GitHub Desktop.
(ns core
(:require [mount.core :as mount]
[user-finder :refer [user-finder user-finder-2]]
[users-db :refer [db]))
(mount/start)
(user-finder 2) ; finds user with id = 2
; when I stop users-db it still works (althought db state is stopped)
; this is because state is partially applied fn that resolves db during state's start I guess
(mount/stop #'users-db/db)
(user-finder 2) ; finds user with id = 2
; if there is no partial application it works as expected, but requires db state
(user-finder-2 db 2) ; throws ex that there is no db state
; is there a way to have benefits of this partially applied version (no need for passing db everywhere)
; as well as having behavior of the seconduser-finder-2 - have "live-link" to db state?
(ns finders)
(defn find-user [users id]
(some #(if (= (:id %) id) %) users))
(ns user-finder
(:require [mount.core :refer [defstate]]
[finders :as finders]
[users-db :refer [db]]))
(defstate user-finder :start (partial finders/find-user db))
(defstate user-finder-2 :start finders/find-user)
(ns users-db
(:require [mount.core :refer [defstate]]))
(def users #{{:id 1 :name "foo"}
{:id 2 :name "bar"}})
(defstate db :start users)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment