Skip to content

Commit c868268

Browse files
committed
fork cljss to fix race condition
- theres no need for an atom to track state if we just pass the class through in a functional implementation
1 parent f416156 commit c868268

File tree

4 files changed

+27
-28
lines changed

4 files changed

+27
-28
lines changed

project.clj

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1-
(defproject org.roman01la/cljss "1.6.3"
1+
(defproject com.verypossible/cljss "1.6.4"
22
:description "Clojure Style Sheets"
33

4-
:url "https://github.com/roman01la/cljss"
4+
:repositories [
5+
["releases" {:url "https://clojars.org"
6+
:sign-releases false
7+
:username :env/clojars_username
8+
:password :env/clojars_password}]
9+
["snapshots" {:url "https://clojars.org"
10+
:sign-releases false}]
11+
]
12+
13+
:url "https://github.com/verypossible/cljss"
514

615
:license {:name "Eclipse Public License"
716
:url "http://www.eclipse.org/legal/epl-v10.html"}

src/cljss/builder.clj

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
(map? value)))
99

1010
(defn build-styles [cls styles]
11-
(c/reset-env! {:cls cls})
1211
(let [pseudo (filterv utils/pseudo? styles)
1312
nested (->> styles
1413
(filterv (comp not utils/pseudo?))
1514
(filterv utils/nested?))
16-
[mstatic mvals] (some-> styles :cljss.core/media build-media)
15+
[mstatic mvals] (some-> styles :cljss.core/media ((partial build-media cls)))
1716
styles (dissoc styles :cljss.core/media)
1817
styles (filterv #(and (not (utils/pseudo? %)) (not (utils/nested? %))) styles)
1918
[static vals] (c/collect-styles cls styles)

src/cljss/collect.clj

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,6 @@
11
(ns cljss.collect
22
(:require [cljss.utils :refer [build-css]]))
33

4-
(def env* (atom {:id 0
5-
:cls nil}))
6-
7-
(defn reset-env! [v]
8-
(reset! env* (merge {:id 0 :cls nil} v)))
9-
104
(defn dynamic? [[_ value]]
115
(not (or (string? value)
126
(number? value))))
@@ -15,16 +9,14 @@
159
[rule (str "--var-" cls "-" idx)])
1610

1711
(defn collect-styles [cls styles]
18-
(let [id (:cls @env*)
19-
dynamic (filterv dynamic? styles)
12+
(let [dynamic (filterv dynamic? styles)
2013
static (filterv (comp not dynamic?) styles)
21-
vars
14+
[vars _]
2215
(reduce
23-
(fn [vars ds]
24-
(let [ret (conj vars (varid id (:id @env*) ds))]
25-
(swap! env* update :id inc)
26-
ret))
27-
[]
16+
(fn [[vars idx] ds]
17+
(let [ret (conj vars (varid cls idx ds))]
18+
[ret (inc idx)]))
19+
[[] 0]
2820
dynamic)
2921
vals (mapv (fn [[_ var] [_ exp]] [var exp]) vars dynamic)
3022
static (->> vars

src/cljss/media.clj

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -234,44 +234,43 @@
234234
(clojure.string/join " ")
235235
(str "@media "))))
236236

237-
(defn compile-media-dispatch [styles]
237+
(defn compile-media-dispatch [styles _]
238238
(cond
239239
(contains? styles :media) :media
240240
(contains? styles :styles) :styles))
241241

242242
(defmulti compile-media #'compile-media-dispatch)
243243

244-
(defmethod compile-media :media [{media :media}]
244+
(defmethod compile-media :media [{media :media} cls]
245245
(->> (seq media)
246246
(reduce
247247
(fn [[sstyles svalues] [query styles]]
248-
(let [[static values] (compile-media {:styles styles})
248+
(let [[static values] (compile-media {:styles styles} cls)
249249
query (-compile-media-query query)]
250250
[(str sstyles query static) (concat svalues values)]))
251251
["" []])))
252252

253-
(defmethod compile-media :styles [{styles :styles}]
253+
(defmethod compile-media :styles [{styles :styles} cls]
254254
(let [pseudo (filterv utils/pseudo? styles)
255255
pstyles (->> pseudo
256256
(reduce
257257
(fn [coll [rule styles]]
258-
(conj coll (c/collect-styles (str (:cls @c/env*) (subs (name rule) 1)) styles)))
258+
(conj coll (c/collect-styles (str cls (subs (name rule) 1)) styles)))
259259
[]))
260260
styles (filterv (comp not utils/pseudo?) styles)
261-
[static values] (c/collect-styles (:cls @c/env*) styles)
261+
[static values] (c/collect-styles cls styles)
262262
values (->> pstyles
263263
(mapcat second)
264264
(into values))]
265265
[(str "{" (apply str static (map first pstyles)) "}")
266266
values]))
267267

268-
(defn build-media [styles]
269-
(compile-media {:media styles}))
268+
(defn build-media [cls styles]
269+
(compile-media {:media styles} cls))
270270

271271
(comment
272-
(c/reset-env! {:cls "class"})
273-
274272
(build-media
273+
"class"
275274
{[[:only :screen :and [:min-width "300px"]]
276275
[:print :and [:color]]]
277276
{:font-size 'p

0 commit comments

Comments
 (0)