Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature/clojurescript #11

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 12 additions & 17 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,22 @@ version: 2
jobs:
build:
docker:
- image: circleci/clojure:tools-deps

working_directory: ~/repo

environment:
LEIN_ROOT: "true"
JVM_OPTS: -Xmx3200m

- image: circleci/clojure:tools-deps-1.10.0.442-node
steps:
- checkout

# Download and cache dependencies
- restore_cache:
keys:
- v1-dependencies-{{ checksum "deps.edn" }}

- run: clojure -R:test -e ""

- 'clj-v1-{{ checksum "deps.edn" }}-{{ checksum "package-lock.json" }}'
- 'clj-v1'
- run: npm ci
- run: mkdir -p test-results
- run: bin/kaocha --plugin kaocha.plugin/junit-xml --junit-xml-file test-results/kaocha/results.xml
- store_test_results:
path: test-results
- save_cache:
key: 'clj-v1-{{checksum "deps.edn"}}-{{ checksum "package-lock.json" }}'
paths:
- ~/.m2
key: v1-dependencies-{{ checksum "deps.edn" }}

- run: clojure -A:test
- ~/.cljs/.aot_cache
- ~/node_modules
- ~/.gitlibs
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ pom.xml.asc
.lein-failures
.nrepl-port
.cpcache/
test-results
.cljs_node_repl
3 changes: 3 additions & 0 deletions bin/kaocha
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: stray blank line before the shebang

#!/usr/bin/env bash
clojure -A:test -m kaocha.runner "$@"
17 changes: 9 additions & 8 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{:paths ["src"]
:deps {edn-query-language/eql {:mvn/version "0.0.9"}
aysylu/loom {:mvn/version "1.0.2"}}
:deps {edn-query-language/eql {:mvn/version "0.0.9"}
aysylu/loom {:mvn/version "1.0.2"}}

:aliases {:provided {:extra-deps {org.clojure/clojure {:mvn/version "1.10.1"}
org.clojure/clojurescript {:mvn/version "1.10.597"}}}
:test {:extra-paths ["test" "dev"]
:extra-deps {com.datomic/datomic-free {:mvn/version "0.9.5697"
:exclusions [joda-time
commons-codec]}
com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner.git"
:sha "f7ef16dc3b8332b0d77bc0274578ad5270fbfedd"}}
:main-opts ["-m" "cognitect.test-runner"]}}}
:extra-deps {lambdaisland/kaocha {:mvn/version "0.0-565"}
lambdaisland/kaocha-cljs {:mvn/version "0.0-59"}
lambdaisland/kaocha-junit-xml {:mvn/version "0.0-70"}
com.datomic/datomic-free {:mvn/version "0.9.5697"
:exclusions [joda-time commons-codec]}
com.cognitect/test-runner {:git/url "https://github.com/cognitect-labs/test-runner.git"
:sha "f7ef16dc3b8332b0d77bc0274578ad5270fbfedd"}}}}}
18 changes: 18 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"devDependencies": {
"isomorphic-ws": "^4.0.1",
"ws": "^7.0.1"
}
}
File renamed without changes.
62 changes: 36 additions & 26 deletions src/pour/core.clj → src/pour/core.cljc
Original file line number Diff line number Diff line change
@@ -1,21 +1,26 @@
(ns pour.core
(:require [edn-query-language.core :as eql])
(:import (clojure.lang Seqable)))
(:require [edn-query-language.core :as eql]))

(defn seqy? [s]
(and (not (:db/id s)) ; ie, a datomic Entity or similar
(and (not (nil? s))
(not (:db/id s))
(not (map? s))
(instance? Seqable s)))
(not (string? s))
(seqable? s)))


(defn pipe [_ {value :value}]
value)

(defn knit [{:keys [resolvers] :as env}
{:keys [dispatch-key value] :as node}]
(future (let [resolver (or (get resolvers dispatch-key)
(fn default-resolver [_ _]
(get value dispatch-key)))]
(resolver env (merge node {:value value})))))
(let [operate (fn []
(let [resolver (or (get resolvers dispatch-key)
(fn default-resolver [_ _]
(get value dispatch-key)))]
(resolver env (merge node {:value value}))))]
#?(:clj (future (operate))
:cljs (operate))))

(defn- matches-union [key value]
(boolean (key value)))
Expand All @@ -24,36 +29,41 @@
(if (map? m)
(->> m
(keep (fn [[k v]]
(let [vv (if (future? v)
(deref v)
v)]
(when (not (nil? vv))
[k vv]))))
(when (not (nil? v))
[k v])))
(into {}))
m))

(declare parse)

(defn process-union [node-params env value child]
(let [custom-dispatch (:union-dispatch node-params)
resolved-custom-dispatcher (when (symbol? custom-dispatch)
(apply resolve [custom-dispatch]))
union-dispatch (or resolved-custom-dispatcher
matches-union)]
(reduce (fn [_ {:keys [union-key children params] :as uc}]
(when (union-dispatch union-key value)
(reduced (parse env {:value value
:children children}))))
{}
(:children child))))

(defn parse [env {:keys [value children] :as node}]
(when value
(let [node-params (:params node)]
(let [node-params (:params node)
extract-value #?(:clj deref
:cljs identity)]
(->> children
(map (fn [child]
{:pending (knit env (merge {:value value} child))
:child child}))
:child child}))
(reduce (fn [acc {:keys [pending child]}]
(let [resolved (deref pending)
(let [resolved (extract-value pending)
{:keys [type key params]} child
v (condp = type
:prop resolved
:union (let [union-dispatch (or (when-let [custom-dispatch (:union-dispatch node-params)]
(and (symbol? custom-dispatch)
(resolve custom-dispatch)))
matches-union)]
(reduce (fn [_ {:keys [union-key children params] :as uc}]
(when (union-dispatch union-key value)
(reduced (parse env {:value value
:children children}))))
{}
(:children child)))
:union (process-union node-params env value child)
:join (if (seqy? resolved)
(->> resolved
(keep (fn [v]
Expand Down
3 changes: 2 additions & 1 deletion test/pour/compose_test.clj → test/pour/compose_test.cljc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns pour.compose-test
(:require [clojure.test :refer :all]
(:require #?@(:clj [[clojure.test :refer [deftest testing is]]]
:cljs [[cljs.test :refer [deftest testing is]]])
[pour.core :as pour]
[pour.compose :as compose]))

Expand Down
35 changes: 24 additions & 11 deletions test/pour/core_test.clj → test/pour/core_test.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns pour.core-test
(:require [clojure.test :refer :all]
[pour.core :as pour]
[datomic.api :as d]))
(:require #?@(:clj [[clojure.test :refer [deftest testing is]]
[datomic.api :as d]]
:cljs [[cljs.test :refer [deftest testing is]]])
[pour.core :as pour]))

(defrecord Test [a b])

Expand All @@ -18,12 +19,19 @@
(is (pour/seqy? '()))
(is (pour/seqy? #{})))

(defn provide-test-entity
"In CLJ, use an actual, real datomic entity. In CLJS, provide something entity-like."
[]
#?(:clj (let [uri "datomic:mem://pour-test"
_ (d/create-database uri)
conn (d/connect uri)]
(d/entity (d/db conn) :db/ident))
:cljs {:db/id 123}))


(deftest datomic-entities
(testing "Datomic entities should not be treated as sequences"
(let [uri "datomic:mem://pour-test"
_ (d/create-database uri)
conn (d/connect uri)]
(is (not (pour/seqy? (d/entity (d/db conn) :db/ident)))))))
(testing "Datomic entity-ish values should not be treated as sequences"
(is (not (pour/seqy? (provide-test-entity))))))

(deftest nils
(testing "nil values on provided keys should mean that the key is also not present in the output"
Expand Down Expand Up @@ -53,11 +61,16 @@
:other [{:foo1 :a}
{:foo1 :b}]}))))

(defn now []
#?(:clj (System/currentTimeMillis)
:cljs (js/Date.now)))

(defn sleepyresolver
"Debug resolver that passes through v after a delay"
[time v]
(fn [& args]
(Thread/sleep time)
#?(:clj (Thread/sleep time)
:cljs (reduce + (range (* time time))))
v))

(deftest async
Expand All @@ -76,7 +89,7 @@
:d3
:d4
{(:d1 {:as :foo}) [:d1 :d2 :d3 :d4 :should :be :ignored]}]
start (System/currentTimeMillis)
start (now)
_ (is (= (pour/pour env q {:a {:a 1}})
{:a {:a 1},
:d1 :d1,
Expand All @@ -87,7 +100,7 @@
:d2 :d2
:d3 :d3
:d4 :d4}}))
duration (- (System/currentTimeMillis) start)]
duration (- (now) start)]
(is (< duration 250)))))


Expand Down
5 changes: 5 additions & 0 deletions tests.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#kaocha/v1
{:tests [{:id :unit
:type :kaocha.type/clojure.test}
{:id :unit-cljs
:type :kaocha.type/cljs}]}