From d3e797de989b090702387b20458065aafb3d5c7c Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 21:25:48 +0100 Subject: [PATCH 01/11] clj -> cljc --- src/pour/{compose.clj => compose.cljc} | 0 src/pour/{core.clj => core.cljc} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/pour/{compose.clj => compose.cljc} (100%) rename src/pour/{core.clj => core.cljc} (100%) diff --git a/src/pour/compose.clj b/src/pour/compose.cljc similarity index 100% rename from src/pour/compose.clj rename to src/pour/compose.cljc diff --git a/src/pour/core.clj b/src/pour/core.cljc similarity index 100% rename from src/pour/core.clj rename to src/pour/core.cljc From d0b65594f1024ecb98a44c862362491196c1c616 Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 21:32:08 +0100 Subject: [PATCH 02/11] mv tests to cljc --- test/pour/{compose_test.clj => compose_test.cljc} | 0 test/pour/{core_test.clj => core_test.cljc} | 7 ++++--- 2 files changed, 4 insertions(+), 3 deletions(-) rename test/pour/{compose_test.clj => compose_test.cljc} (100%) rename test/pour/{core_test.clj => core_test.cljc} (95%) diff --git a/test/pour/compose_test.clj b/test/pour/compose_test.cljc similarity index 100% rename from test/pour/compose_test.clj rename to test/pour/compose_test.cljc diff --git a/test/pour/core_test.clj b/test/pour/core_test.cljc similarity index 95% rename from test/pour/core_test.clj rename to test/pour/core_test.cljc index 8a533de..5bcb2df 100644 --- a/test/pour/core_test.clj +++ b/test/pour/core_test.cljc @@ -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]) From 1afd47598fa52fc1ba0089016f7b776688635832 Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 21:37:37 +0100 Subject: [PATCH 03/11] use real entities in java, fake ones in cljs --- test/pour/core_test.cljc | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/test/pour/core_test.cljc b/test/pour/core_test.cljc index 5bcb2df..d825657 100644 --- a/test/pour/core_test.cljc +++ b/test/pour/core_test.cljc @@ -19,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" From 4727ed290e601cd639ce95c4df50067cabe19b2d Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 21:37:48 +0100 Subject: [PATCH 04/11] use cljs.test in compose tests --- test/pour/compose_test.cljc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/pour/compose_test.cljc b/test/pour/compose_test.cljc index 00c0382..f9094a3 100644 --- a/test/pour/compose_test.cljc +++ b/test/pour/compose_test.cljc @@ -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])) From 7e65dc6caf99dbdfa37642c4cc9905484fa386fd Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 22:07:38 +0100 Subject: [PATCH 05/11] add kaocha --- .circleci/config.yml | 29 ++++++++++++----------------- bin/kaocha | 3 +++ deps.edn | 17 +++++++++-------- tests.edn | 5 +++++ 4 files changed, 29 insertions(+), 25 deletions(-) create mode 100755 bin/kaocha create mode 100644 tests.edn diff --git a/.circleci/config.yml b/.circleci/config.yml index 6abd295..3ebe4c5 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -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 install + - 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 \ No newline at end of file + - ~/.cljs/.aot_cache + - ~/node_modules + - ~/.gitlibs \ No newline at end of file diff --git a/bin/kaocha b/bin/kaocha new file mode 100755 index 0000000..924d1e1 --- /dev/null +++ b/bin/kaocha @@ -0,0 +1,3 @@ + +#!/usr/bin/env bash +clojure -A:test -m kaocha.runner "$@" diff --git a/deps.edn b/deps.edn index ff8d9ea..782f80d 100644 --- a/deps.edn +++ b/deps.edn @@ -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"}}}}} diff --git a/tests.edn b/tests.edn new file mode 100644 index 0000000..692202a --- /dev/null +++ b/tests.edn @@ -0,0 +1,5 @@ +#kaocha/v1 +{:tests [{:id :unit + :type :kaocha.type/clojure.test} + {:id :unit-cljs + :type :kaocha.type/cljs}]} \ No newline at end of file From bfa71eeeb2d05001fa3d5b65a9e0063e9ed3975b Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 22:26:10 +0100 Subject: [PATCH 06/11] prefer seqable? and explict checks on stringyness, nils etc --- src/pour/core.cljc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/pour/core.cljc b/src/pour/core.cljc index 3db9ecc..5dad61a 100644 --- a/src/pour/core.cljc +++ b/src/pour/core.cljc @@ -1,11 +1,13 @@ (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)) ; ie, a datomic Entity or similar (not (map? s)) - (instance? Seqable s))) + (not (string? s)) + (seqable? s))) + (defn pipe [_ {value :value}] value) From 00d6fba70b8d45df5aed0dbb45da9b1ba94606de Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 22:26:20 +0100 Subject: [PATCH 07/11] add kaocha test-results to ignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index a4cb69a..cde4e0f 100644 --- a/.gitignore +++ b/.gitignore @@ -12,3 +12,4 @@ pom.xml.asc .lein-failures .nrepl-port .cpcache/ +test-results From 04d08108ed9b978e786583d56ce383f821b7c77e Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 22:52:56 +0100 Subject: [PATCH 08/11] add ws package for kaocha --- .circleci/config.yml | 6 +++--- package-lock.json | 18 ++++++++++++++++++ package.json | 6 ++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 package-lock.json create mode 100644 package.json diff --git a/.circleci/config.yml b/.circleci/config.yml index 3ebe4c5..7e663fe 100644 --- a/.circleci/config.yml +++ b/.circleci/config.yml @@ -7,15 +7,15 @@ jobs: - checkout - restore_cache: keys: - - 'clj-v1-{{ checksum "deps.edn" }}' # -{{ checksum "package-lock.json" }}' + - 'clj-v1-{{ checksum "deps.edn" }}-{{ checksum "package-lock.json" }}' - 'clj-v1' - # - run: npm install + - 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" }}' + key: 'clj-v1-{{checksum "deps.edn"}}-{{ checksum "package-lock.json" }}' paths: - ~/.m2 - ~/.cljs/.aot_cache diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..2a9d798 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,18 @@ +{ + "requires": true, + "lockfileVersion": 1, + "dependencies": { + "isomorphic-ws": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/isomorphic-ws/-/isomorphic-ws-4.0.1.tgz", + "integrity": "sha512-BhBvN2MBpWTaSHdWRb/bwdZJ1WaehQ2L1KngkCkfLUGF0mAWAT1sQUQacEmQ0jXkFw/czDXPNQSL5u2/Krsz1w==", + "dev": true + }, + "ws": { + "version": "7.2.1", + "resolved": "https://registry.npmjs.org/ws/-/ws-7.2.1.tgz", + "integrity": "sha512-sucePNSafamSKoOqoNfBd8V0StlkzJKL2ZAhGQinCfNQ+oacw+Pk7lcdAElecBF2VkLNZRiIb5Oi1Q5lVUVt2A==", + "dev": true + } + } +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..e7c4bf0 --- /dev/null +++ b/package.json @@ -0,0 +1,6 @@ +{ + "devDependencies": { + "isomorphic-ws": "^4.0.1", + "ws": "^7.0.1" + } +} From 336f07097e2d6712756cbf1adb0b2b850252142a Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 22:53:11 +0100 Subject: [PATCH 09/11] cross platform now --- test/pour/core_test.cljc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/test/pour/core_test.cljc b/test/pour/core_test.cljc index d825657..d40d7dc 100644 --- a/test/pour/core_test.cljc +++ b/test/pour/core_test.cljc @@ -61,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 @@ -84,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, @@ -95,7 +100,7 @@ :d2 :d2 :d3 :d3 :d4 :d4}})) - duration (- (System/currentTimeMillis) start)] + duration (- (now) start)] (is (< duration 250))))) From a81544c034db9dee808b041a97fdb4c7d3831e40 Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 23:10:05 +0100 Subject: [PATCH 10/11] ignore node repl --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index cde4e0f..148ed74 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ pom.xml.asc .nrepl-port .cpcache/ test-results +.cljs_node_repl From ad5b5f44da98d46424f1870e2c06c3994c6014af Mon Sep 17 00:00:00 2001 From: Dan Peddle Date: Sun, 26 Jan 2020 23:14:26 +0100 Subject: [PATCH 11/11] wip cljs --- src/pour/core.cljc | 54 ++++++++++++++++++++++++++-------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/pour/core.cljc b/src/pour/core.cljc index 5dad61a..6024b2d 100644 --- a/src/pour/core.cljc +++ b/src/pour/core.cljc @@ -3,7 +3,7 @@ (defn seqy? [s] (and (not (nil? s)) - (not (:db/id s)) ; ie, a datomic Entity or similar + (not (:db/id s)) (not (map? s)) (not (string? s)) (seqable? s))) @@ -14,10 +14,13 @@ (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))) @@ -26,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]