Discussion:
[Question] How to properly start up refs in web applications?
Thais Lima
2018-11-17 21:27:25 UTC
Permalink
Hi,

I've started learning Clojure and I am creating a simple web application
using Compojure and Ring.
I have refs defined in my core.clj like this:

(def my-ref1 (ref {}))
(def my-ref2 (ref {}))
(def my-ref3 (ref 0))


And in my handler.clj, I call the functions from my core namespace that
change those refs inside a dosync.

The problem is: I've created tests for my handler and for my core, when I
ran each of them separately, everything works fine.
But when I ran

lein test

I get errors for my handler-test, because my refs were modified by the
core-test and this messes up my expected results.

Besides that problem, I wonder if there is a proper way of defining those
refs in ring initialization.

What is the proper way of initializing refs in a project?

Thank you,
Thais
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Sean Corfield
2018-11-18 00:32:40 UTC
Permalink
This doesn’t answer your question but I’m wondering why you’re using refs at all? In production Clojure code I think they are extremely rare (we have about 85K lines of Clojure and no refs at all).

Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood

________________________________
From: ***@googlegroups.com <***@googlegroups.com> on behalf of Thais Lima <***@gmail.com>
Sent: Saturday, November 17, 2018 1:27:25 PM
To: Clojure
Subject: [Question] How to properly start up refs in web applications?

Hi,

I've started learning Clojure and I am creating a simple web application using Compojure and Ring.
I have refs defined in my core.clj like this:

(def my-ref1 (ref {}))
(def my-ref2 (ref {}))
(def my-ref3 (ref 0))


And in my handler.clj, I call the functions from my core namespace that change those refs inside a dosync.

The problem is: I've created tests for my handler and for my core, when I ran each of them separately, everything works fine.
But when I ran

lein test

I get errors for my handler-test, because my refs were modified by the core-test and this messes up my expected results.

Besides that problem, I wonder if there is a proper way of defining those refs in ring initialization.

What is the proper way of initializing refs in a project?

Thank you,
Thais

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com<mailto:clojure+***@googlegroups.com>.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Thais Lima
2018-11-18 00:53:26 UTC
Permalink
Hello Sean,

I'm using refs for an exercise in which I have to handle state changes
without a database.
Post by Sean Corfield
This doesn’t answer your question but I’m wondering why you’re using refs
at all? In production Clojure code I think they are extremely rare (we have
about 85K lines of Clojure and no refs at all).
Sean Corfield -- (970) FOR-SEAN -- (904) 302-SEAN
An Architect's View -- http://corfield.org/
"If you're not annoying somebody, you're not really alive."
-- Margaret Atwood
------------------------------
*Sent:* Saturday, November 17, 2018 1:27:25 PM
*To:* Clojure
*Subject:* [Question] How to properly start up refs in web applications?
Hi,
I've started learning Clojure and I am creating a simple web application
using Compojure and Ring.
(def my-ref1 (ref {}))
(def my-ref2 (ref {}))
(def my-ref3 (ref 0))
And in my handler.clj, I call the functions from my core namespace that
change those refs inside a dosync.
The problem is: I've created tests for my handler and for my core, when I
ran each of them separately, everything works fine.
But when I ran
lein test
I get errors for my handler-test, because my refs were modified by the
core-test and this messes up my expected results.
Besides that problem, I wonder if there is a proper way of defining those
refs in ring initialization.
What is the proper way of initializing refs in a project?
Thank you,
Thais
--
You received this message because you are subscribed to the Google Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to a topic in the
Google Groups "Clojure" group.
To unsubscribe from this topic, visit
https://groups.google.com/d/topic/clojure/Bx0lOp3m6bo/unsubscribe.
To unsubscribe from this group and all its topics, send an email to
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Matching Socks
2018-11-18 14:26:26 UTC
Permalink
The ref is a good step, but it seems that accessing it via a global
variable causes a problem.

So don't use a global variable. Pass everything into the Ring handlers via
the formal parameters. Compojure-style Ring handlers have only one formal
parameter, the request map, but you can put anything into it. In short,
someone needs to poke the ref into the request map. In the Ring world,
functions named "wrap-..." often figure in the creation of the main handler
precisely to do this sort of thing. Usually, they put something (like the
ref) in closure with an anonymous Ring handler that copies it into the
request map and delegates to downstream handlers.

By the way, if there is only one value to coordinate (e.g., in a typical
case, one big map of all knowledge), using a ref is "the hard way" and an
atom is simpler to deal with. Same concept, less red tape. And there can
usually be just one value to coordinate! A typical program does not use
three databases, so why should it need three refs?
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Oliver Muellerklein
2018-11-18 00:36:20 UTC
Permalink
Hello,

I also recently started learning Clojure and got throw into developing a
web app using Ring and Clojurescript. Can we be something like pen pal but
for Clojure dev? I have someone guiding me through a lot of the Clojure but
it would be so helpful I think to both of us if we could bounce ideas and
get help etc.

Do you use Cider? It's great. You can great a Clojure REPL as you develop.
I'm not sure what the answer to your problem is. But I think Leinegein is
always looking for a deps.edn file right?

-Oliver
Post by Thais Lima
Hi,
I've started learning Clojure and I am creating a simple web application
using Compojure and Ring.
(def my-ref1 (ref {}))
(def my-ref2 (ref {}))
(def my-ref3 (ref 0))
And in my handler.clj, I call the functions from my core namespace that
change those refs inside a dosync.
The problem is: I've created tests for my handler and for my core, when I
ran each of them separately, everything works fine.
But when I ran
lein test
I get errors for my handler-test, because my refs were modified by the
core-test and this messes up my expected results.
Besides that problem, I wonder if there is a proper way of defining those
refs in ring initialization.
What is the proper way of initializing refs in a project?
Thank you,
Thais
--
You received this message because you are subscribed to the Google Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Oliver Muellerklein
2018-11-18 00:40:37 UTC
Permalink
I am working with Gary Johnson. Here is a great web app dev skeleton he
created.


https://gitlab.com/lambdatronic/clojure-webapp-template
Post by Oliver Muellerklein
Hello,
I also recently started learning Clojure and got throw into developing a
web app using Ring and Clojurescript. Can we be something like pen pal but
for Clojure dev? I have someone guiding me through a lot of the Clojure but
it would be so helpful I think to both of us if we could bounce ideas and
get help etc.
Do you use Cider? It's great. You can great a Clojure REPL as you develop.
I'm not sure what the answer to your problem is. But I think Leinegein is
always looking for a deps.edn file right?
-Oliver
Post by Thais Lima
Hi,
I've started learning Clojure and I am creating a simple web application
using Compojure and Ring.
(def my-ref1 (ref {}))
(def my-ref2 (ref {}))
(def my-ref3 (ref 0))
And in my handler.clj, I call the functions from my core namespace that
change those refs inside a dosync.
The problem is: I've created tests for my handler and for my core, when I
ran each of them separately, everything works fine.
But when I ran
lein test
I get errors for my handler-test, because my refs were modified by the
core-test and this messes up my expected results.
Besides that problem, I wonder if there is a proper way of defining those
refs in ring initialization.
What is the proper way of initializing refs in a project?
Thank you,
Thais
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to ***@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+***@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...