Skip to content

Commit ef7904e

Browse files
committed
feat: tapestry.core/send
Introduce a function for working with agents using a virtual executor
1 parent a2bf3ba commit ef7904e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/tapestry/core.clj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
[java.lang VirtualThread]
66
[java.time Duration])
77
(:require [manifold.stream :as s]
8-
[manifold.deferred :as d]))
8+
[manifold.deferred :as d])
9+
(:refer-clojure :exclude [send]))
910

1011
(def ^{:dynamic true
1112
:no-doc true} *local-semaphore*
@@ -330,3 +331,10 @@
330331
true (s/buffer n)
331332
true (s/realize-each)
332333
seq? (s/stream->seq)))))
334+
335+
(defn send
336+
"A version of `send` that uses a loom fiber for the execution of the function `f`.
337+
338+
See `clojure.core/send` for details"
339+
[a f & args]
340+
(apply send-via -static-executor a f args))

test/tapestry/core_test.clj

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,20 @@
188188
(let [success? (atom false)]
189189
@(d/chain' (sut/fiber true) (partial reset! success?))
190190
(is @success?))))
191+
192+
(deftest send-test
193+
(let [a (agent 0)]
194+
(testing "without arguments"
195+
(sut/send a inc)
196+
(await a)
197+
(is (= 1 @a)))
198+
(testing "with argument"
199+
(sut/send a (constantly 0))
200+
(sut/send a + 2 3)
201+
(await a)
202+
(is (= 5 @a)))
203+
(testing "with multiple arguments"
204+
(sut/send a (constantly 0))
205+
(sut/send a + 1 2 3 4)
206+
(await a)
207+
(is (= 10 @a)))))

0 commit comments

Comments
 (0)