mount

rustam.gilaztdinov 2018-03-15T09:14:23.000164Z

@tolitius hello! Can you please clarify this thing for me In dev/user.my namespace I have this:

(ns <http://user.my|user.my>
  (:require [clojure.tools.namespace.repl :refer [refresh]]
            [mount.core :as mount]))

(defn start []
  (mount/in-cljc-mode)
  (mount/start))

(defn stop []
  (mount/stop))

(defn go []
  (start)
  :started)

(defn reset []
  (stop)
  (refresh :after '<http://user.my/go|user.my/go>)
  :ok)

I want to to use a mount with cljs, more generally β€” sharing config on clj and cljs side So, in src/cljc I create namespace config.cljc, which look like this
(ns my-lib.config
  #?(:clj
     (:require [config.core :refer [load-env]] ;; this is yogthos/config
               [taoensso.timbre :as log]
               [mount.core :refer [defstate]]))
  #?(:cljs
     (:require-macros [mount.core :refer [defstate]])))

#?(:clj
   (defn load-config []
     (log/info "loading configuration")
     (load-env)))

#?(:clj
   (defstate ^{:on-reload :noop} config
     :start (load-config)))

#?(:cljs
   (defstate ^{:on-reload :noop} frontend-environment
     :start (:env @config)))
When I started repl, all states starting good, then I load namespase with config in repl and try to see what’s happening:
;; this is clj-repl
my-lib.config&gt; (:env @config)
{:host "localhost", :port 3001, :database "data/dev"}

;; this is cljs-repl
my-lib.config&gt; frontend-environment
nil

my-lib.config&gt; @frontend-environment
#object[Error Error: No protocol method IDeref.-deref defined for type undefined: ]
cljs.core.missing_protocol@http://localhost:3449/js/compiled/app.js:129:336
cljs.core._deref@http://localhost:3449/js/compiled/app.js:223:435
cljs.core.deref@http://localhost:3449/js/compiled/app.js:366:402
@http://localhost:3449/js/compiled/app.js line 3469 &gt; eval:1:226
@http://localhost:3449/js/compiled/app.js line 3469 &gt; eval:1:189
@http://localhost:3449/js/compiled/app.js line 3469 &gt; eval:1:2
figwheel.client.utils.eval_helper@http://localhost:3449/js/compiled/app.js:3469:309
What I do wrong?

rustam.gilaztdinov 2018-03-15T09:52:15.000165Z

Even in .cljs namespaces I can’t work with mount, for example

(ns my-lib.state
    (:require-macros [mount.core :refer [defstate]]))
In cljs-repl:
my-lib.state&gt; (defstate a :start 12)
---- Compiler Warning on cljs form line:1 column:1 ----

Use of undeclared Var mount.core/running-noop?

1 (defstate a :start 12)
^---
---- Compiler Warning ----
---- Compiler Warning on cljs form line:1 column:1 ----

Use of undeclared Var mount.core/mount-it
1 (defstate a :start 12)
^---
---- Compiler Warning ----
---- Compiler Warning on cljs form line:1 column:1 ----

Use of undeclared Var mount.core/current-state
1 (defstate a :start 12)
^---
---- Compiler Warning ----
#object[ReferenceError ReferenceError: mount is not defined]
nil

2018-03-15T17:32:53.000630Z

tolitius 2018-03-15T19:45:53.000112Z

@rustam.gilaztdinov you need to require mount as well so cljs understands how to evaluate defstate macro (i.e. since it is using mount functions):

tolitius 2018-03-15T19:46:15.000209Z

connected! &gt;&gt;
To quit, type: :cljs/quit
nil
cljs.user=&gt; (require-macros '[mount.core :refer [defstate]])
nil
cljs.user=&gt; (defstate a :start 12)
WARNING: Use of undeclared Var mount.core/running-noop? at line 1 &lt;cljs repl&gt;
WARNING: Use of undeclared Var mount.core/mount-it at line 1 &lt;cljs repl&gt;
WARNING: Use of undeclared Var mount.core/current-state at line 1 &lt;cljs repl&gt;
#'cljs.user/a
but:
cljs.user=&gt; (require '[mount.core :as mount])
nil
cljs.user=&gt; (defstate a :start 12)
#'cljs.user/a
cljs.user=&gt; @a
12

rustam.gilaztdinov 2018-03-15T20:00:22.000068Z

doesn't work for me 😞

rustam.gilaztdinov 2018-03-15T20:02:01.000513Z

(ns my-lib.state
  (:require-macros [mount.core :refer [defstate]])
  (:require [mount.core :as mount]))

rustam.gilaztdinov 2018-03-15T20:02:17.000716Z

my-lib.state&gt; (defstate a :start 12)
#object[ReferenceError ReferenceError: mount is not defined]
nil

rustam.gilaztdinov 2018-03-15T20:02:54.000437Z

@tolitius ^

tolitius 2018-03-15T20:04:47.000620Z

ok, one problem at a time

tolitius 2018-03-15T20:05:01.000633Z

did it fix the warnings?

tolitius 2018-03-15T20:05:13.000133Z

i.e.

Use of undeclared Var mount.core...

rustam.gilaztdinov 2018-03-15T20:05:14.000709Z

yes

tolitius 2018-03-15T20:05:46.000453Z

ok, good. which browser are you using to connect to the cljs repl?

rustam.gilaztdinov 2018-03-15T20:06:04.000052Z

chrome

tolitius 2018-03-15T20:06:50.000162Z

how do you start the cljs repl?

rustam.gilaztdinov 2018-03-15T20:07:12.000335Z

figwheel

rustam.gilaztdinov 2018-03-15T20:07:26.000426Z

from cider in spacemacs

tolitius 2018-03-15T20:08:09.000508Z

there is something off in a way cljs repl (in your case) evals the forms, in this case defstate

tolitius 2018-03-15T20:09:31.000717Z

I use neither πŸ™‚ so it is harder to debug, but could you start your cljs repl in a different way and try? for example I use "terminal" and "boot" to start the cljs repl.

tolitius 2018-03-15T20:10:42.000164Z

i.e. this is what I use: https://github.com/tolitius/mount/blob/master/build.boot#L114-L119

tolitius 2018-03-15T20:10:49.000419Z

do you know boot at all?

tolitius 2018-03-15T20:10:56.000089Z

(i.e. use it)

rustam.gilaztdinov 2018-03-15T20:12:17.000543Z

it's really dumb -- but after lein clean mount in cljs works now! πŸŽ‰

tolitius 2018-03-15T20:12:22.000191Z

I remember this error on cljs side when the browser could not evaluate the forms, it was caused by the faulty repl connection. since I don't use emacs/figwheel it is harder for me to guess

tolitius 2018-03-15T20:12:37.000319Z

ah.. ok. great πŸ™‚

tolitius 2018-03-15T20:12:58.000394Z

yea, that is one of the reasons I started using boot for everything

rustam.gilaztdinov 2018-03-15T20:13:15.000527Z

thx, can you help me with .cljc files?

tolitius 2018-03-15T20:13:23.000181Z

to avoid hours of debugging that is caused by stale state introduced by lein

rustam.gilaztdinov 2018-03-15T20:13:52.000044Z

maybe tools.deps will help us in a future?

tolitius 2018-03-15T20:15:10.000426Z

could be, have not used it yet (docs are interesting though)

rustam.gilaztdinov 2018-03-15T20:15:31.000137Z

agree

tolitius 2018-03-15T20:15:58.000257Z

> can you help me with .cljc files? do you still have an undefined type problem?

rustam.gilaztdinov 2018-03-15T20:16:54.000566Z

no, another one, give me a minute, pls)

tolitius 2018-03-15T20:17:37.000061Z

ok, I have to run, post it here. I'll look later on

rustam.gilaztdinov 2018-03-15T20:19:13.000175Z

I have this cljc namespace

(ns my-lib.config
  (:require
   #?@(:clj
        [[config.core :refer [load-env]]
         [taoensso.timbre :as log]
         [mount.core :refer [defstate]]]
       :cljs
       [[mount.core :as mount]]))

  #?(:cljs
     (:require-macros
            [mount.core :refer [defstate]])))

#?(:clj
   (defn load-config []
     (log/info "loading configuration")
     (load-env)))

#?(:clj
   (defstate ^{:on-reload :noop} config
     :start (load-config)))

#?(:cljs
   (defstate ^{:on-reload :noop} frontend-environment
     :start (:env @config)))

rustam.gilaztdinov 2018-03-15T20:19:47.000438Z

and on clj-repl config works well

rustam.gilaztdinov 2018-03-15T20:20:05.000206Z

I want to share this config to cljs-side

rustam.gilaztdinov 2018-03-15T20:21:21.000411Z

In cljs-repl, when I evaluate namespace -- I see this warning

WARNING: Use of undeclared Var my-lib.config/config at line 25 /Users/..../config.cljc

rustam.gilaztdinov 2018-03-15T20:22:27.000383Z

and this error

my-lib.config&gt; @frontend-environment
#object[Error Error: No protocol method IDeref.-deref defined for type undefined: ]
   cljs.core/missing-protocol (jar:file:/Users/../.m2/repository/org/clojure/clojurescript/1.9.908/clojurescript-1.9.908.jar!/cljs/core.cljs:304:4)
   cljs.core/-deref (jar:file:/Users/../.m2/repository/org/clojure/clojurescript/1.9.908/clojurescript-1.9.908.jar!/cljs/core.cljs:659:1)
   cljs$core$deref (jar:file:/Users/../.m2/repository/org/clojure/clojurescript/1.9.908/clojurescript-1.9.908.jar!/cljs/core.cljs:1437:4)
nil

rustam.gilaztdinov 2018-03-15T20:34:00.000622Z

I think -- this is because defstate frontend-environment relies to @config, which is #?clj related