|
5 | 5 | [](https://travis-ci.org/binaryage/cljs-zones) |
6 | 6 | [](https://github.com/binaryage/cljs-zones-sample) |
7 | 7 |
|
8 | | -Magical `binding` which survives async calls (with the help of `bound-fn`). |
| 8 | +Magical `binding` macro which survives async calls (with the help of `bound-fn`). |
9 | 9 |
|
10 | 10 | ### Documentation |
11 | 11 |
|
12 | | -You can test it in your browser [with klipse][1]. |
13 | | - |
14 | | -If you want to test it locally, just do the following: |
| 12 | +Example |
15 | 13 |
|
16 | 14 | ```clojure |
17 | | -(ns zones.tests.core |
18 | | - (:refer-clojure :exclude [binding get set]) |
19 | | - (:require [cljs.test :refer-macros [async deftest testing is use-fixtures]] |
20 | | - [zones.core :refer-macros [binding get set bound-fn* bound-fn] :refer [make-zone default-zone]])) |
21 | | - |
22 | | -; -- helpers ---------------------------------------------------------------------------------------------------------------- |
23 | | - |
24 | | -(defn default-zone-str [] |
25 | | - (pr-str default-zone)) |
26 | | - |
27 | | -(defn print-default-zone [& args] |
28 | | - (apply print (concat args [": " (default-zone-str)]))) |
29 | | - |
30 | | -; -- T0 --------------------------------------------------------------------------------------------------------------------- |
31 | | - |
32 | | -(defn test0 [] |
33 | | - (print-default-zone "before") |
34 | | - (binding [v "I'm a dynamically bound value in default zone"] |
35 | | - (print-default-zone "inside") |
36 | | - (js/setTimeout (bound-fn* #(print-default-zone "in async call")) 500)) |
37 | | - (print-default-zone "after")) |
38 | | - |
39 | | -(deftest T0 |
40 | | - (async done |
41 | | - (test0) |
42 | | - (js/setTimeout done 1000))) |
| 15 | +(ns zones.test |
| 16 | + (:refer-clojure :exclude [binding]) |
| 17 | + (:require [zones.core :refer-macros [binding bound-fn] :refer [default-zone]])) |
| 18 | + |
| 19 | +(defn print-default-zone [prefix] |
| 20 | + (println (str prefix ": " (pr-str default-zone)))) |
| 21 | + |
| 22 | +(print-default-zone "before") |
| 23 | +(binding [v "I'm a dynamically bound value in default zone"] |
| 24 | + (print-default-zone "inside") |
| 25 | + (js/setTimeout (bound-fn [] (print-default-zone "in async call")) 500)) |
| 26 | +(print-default-zone "after") |
| 27 | +(println "main done") |
43 | 28 | ``` |
44 | 29 |
|
45 | 30 | Prints: |
46 | 31 |
|
47 | 32 | ``` |
48 | | -Testing zones.tests.core |
49 | | -before : #js {} |
50 | | -inside : #js {:v "I'm a dynamically bound value in default zone"} |
51 | | -after : #js {} |
52 | | -in async call : #js {:v "I'm a dynamically bound value in default zone"} |
| 33 | +before: #js {} |
| 34 | +inside: #js {:v "I'm a dynamically bound value in default zone"} |
| 35 | +after: #js {} |
| 36 | +in async call: #js {:v "I'm a dynamically bound value in default zone"} |
53 | 37 | ``` |
54 | 38 |
|
| 39 | +You can play with an example in your browser [with klipse][1]. |
| 40 | + |
55 | 41 | For more info see [full tests](test/src/tests/zones/tests/core.cljs) and [Travis output](https://travis-ci.org/binaryage/cljs-zones). |
56 | 42 |
|
57 | | -[1]: http://app.klipse.tech/?cljs_in.gist=darwin/0de5c18d636d2c1db7b07d3500de51a0&external-libs=%5Bhttps://raw.githubusercontent.com/binaryage/cljs-zones/1089500c677513241b71b1ca1b73566f412eb3ec/src/lib%5D |
| 43 | +[1]: http://app.klipse.tech/?cljs_in.gist=darwin/1e31b0c33f1ca0e6e0e475b51f95b424&external-libs=%5Bhttps://raw.githubusercontent.com/binaryage/cljs-zones/v0.1.0/src/lib%5D |
0 commit comments