Skip to content

Commit

Permalink
Merge pull request #18 from nubank/sync-engine-return-on-channel
Browse files Browse the repository at this point in the history
Allow running the sync engine with a return on a channel
  • Loading branch information
aredington authored Dec 7, 2023
2 parents 8da600d + c38e9f0 commit b9a2a47
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# Changelog

## 1.15.0 / 2023-12-07
- Allow running the lazy synchronous engine with a channel return.

## 1.14.1 / 2023-12-05
- Fix bug that allowed a leaf body to be run multiple times in engines which used the lazy environment.

Expand Down
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defproject dev.nu/nodely "1.14.1"
(defproject dev.nu/nodely "1.15.0"
:description "Decoupling data fetching from data dependency declaration"
:url "https://github.com/nubank/nodely"
:license {:name "MIT"}
Expand Down
1 change: 1 addition & 0 deletions src/nodely/api/v0.clj
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
:or {engine :core-async.lazy-scheduling}
:as opts}]
(case engine
:sync.lazy (nodely.engine.lazy/eval-key-channel env k)
:core-async.lazy-scheduling (lazy-scheduling/eval-key-channel env k opts)
:applicative.core-async (nodely.engine.applicative/eval-key-contextual env k (assoc opts ::applicative/context applicative.core-async/context)))))

Expand Down
5 changes: 5 additions & 0 deletions src/nodely/engine/lazy.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns nodely.engine.lazy
(:refer-clojure :exclude [eval resolve])
(:require
[clojure.core.async :as async]
[nodely.data :as data]
[nodely.engine.core :as core]))

Expand All @@ -12,6 +13,10 @@
[env k]
(data/get-value (eval env k) k))

(defn eval-key-channel
[env k]
(async/thread (eval-key env k)))

(defn eval-node
[node env]
(eval-key (assoc env ::target node) ::target))
Expand Down
16 changes: 15 additions & 1 deletion test/nodely/api_test.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
(ns nodely.api-test
(:refer-clojure :exclude [cond])
(:require
[clojure.test :refer :all]))
[clojure.core.async :as async]
[clojure.test :refer :all]
[nodely.api.v0 :as api :refer [>leaf >value eval-key-channel]]))

(def env {:x (>value 2)
:y (>value 3)
:z (>leaf (+ ?x ?y))})

(deftest eval-key-channel-test
(testing "returning a result to a channel with :sync.lazy"
(is (= 5 (async/<!! (eval-key-channel env :z {::api/engine :sync.lazy})))))
(testing "returning a result to a channel with :core-async.lazy-scheduling"
(is (= 5 (async/<!! (eval-key-channel env :z {::api/engine :core-async.lazy-scheduling})))))
(testing "returning a result to a channel with :applicative.core-async"
(is (= 5 (async/<!! (eval-key-channel env :z {::api/engine :applicative.core-async}))))))

#_(deftest nested-cond-macros
(testing "using internal variables"
Expand Down
15 changes: 15 additions & 0 deletions test/nodely/engine/lazy_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns nodely.engine.lazy-test
(:refer-clojure :exclude [eval resolve])
(:require
[clojure.core.async :as async]
[clojure.test :refer :all]
[matcher-combinators.test :refer [match?]]
[nodely.data :as data]
Expand All @@ -27,3 +28,17 @@
(+ x y))))
{:x 2
:y 3})))))

(def eval-key-channel-env {:x (data/value 2)
:y (data/value 3)
:z (data/branch (data/leaf [:x] (fn [{:keys [x]}] (odd? x)))
(data/value :odd)
(data/leaf [:y :x]
(fn [{:keys [x y]}]
(+ x y))))})

(deftest eval-key-channel
(testing "eval and getting a channel back"
(is (match? 5
(async/<!!
(lazy/eval-key-channel eval-key-channel-env :z))))))

0 comments on commit b9a2a47

Please sign in to comment.