Skip to content

Commit

Permalink
Remove hash-able requirement for runtime objects
Browse files Browse the repository at this point in the history
  • Loading branch information
djblue committed Jan 7, 2024
1 parent ec222ea commit 486bcc5
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
29 changes: 19 additions & 10 deletions src/portal/runtime.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -111,21 +111,30 @@
(conj atoms a))))) a)
(set-timeout #(notify session-id a) 0))))

(defn- hashable? [value]
(try
(and (hash value) true)
(catch #?(:clj Exception :cljr Exception :cljs :default) _
false)))

(defn- value->key
"Include metadata when capturing values in cache."
[value]
[:value value (meta value) (type value)])
(when (hashable? value)
[:value value (meta value) (type value)]))

(defn- value->id [value]
(let [k (value->key value)]
(-> (:value-cache *session*)
(swap!
(fn [cache]
(if (contains? cache k)
cache
(let [id (next-id *session*)]
(assoc cache [:id id] value k id)))))
(get k))))
(let [k (value->key value)
out (atom nil)]
(swap!
(:value-cache *session*)
(fn [cache]
(if-let [id (and k (get cache k))]
(do (reset! out id) cache)
(let [id (next-id *session*)]
(reset! out id)
(cond-> (assoc cache [:id id] value) k (assoc k id))))))
@out))

(defn- value->id? [value]
(get @(:value-cache *session*) (value->key value)))
Expand Down
18 changes: 18 additions & 0 deletions test/portal/runtime_test.cljc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
(ns portal.runtime-test
(:require [clojure.test :refer [deftest is]]
[portal.runtime :as rt]))

(deftest un-hashable-values
(let [value #?(:bb :skip
:clj (reify Object
(hashCode [_] (throw (Exception. "test"))))
:cljs (reify IHash
(-hash [_] (throw (js/Error. "test"))))
:default :skip)
session {:id (atom 0)
:value-cache (atom {})}]
(when-not (= :skip value)
(binding [rt/*session* session]
(is (= 1 (#'rt/value->id value)) "un-hashable values should produce a mapping")
(is (= 1 (count @(:value-cache session))) "un-hashable values only capture one-way mapping")
(is (= 2 (#'rt/value->id value)) "future captures introduce a new mapping")))))
4 changes: 3 additions & 1 deletion test/portal/test_runner.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(:require [clojure.pprint :as pp]
[clojure.test :as t]
[portal.client.jvm :as p]
[portal.runtime-test]
[portal.runtime.api-test]
[portal.runtime.bench-cson :as bench]
[portal.runtime.cson-test]
Expand Down Expand Up @@ -34,7 +35,8 @@

(defn -main []
(let [{:keys [fail error]}
(run-tests 'portal.runtime.api-test
(run-tests 'portal.runtime-test
'portal.runtime.api-test
'portal.runtime.cson-test
'portal.runtime.edn-test
'portal.runtime.fs-test
Expand Down
4 changes: 3 additions & 1 deletion test/portal/test_runtime_runner.cljs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns portal.test-runtime-runner
(:require [clojure.test :as t]
[portal.runtime-test]
[portal.runtime.api-test]
[portal.runtime.bench-cson :as bench]
[portal.runtime.cson-test]
Expand All @@ -11,7 +12,8 @@

(defn -main []
(runner/run
#(t/run-tests 'portal.runtime.api-test
#(t/run-tests 'portal.runtime-test
'portal.runtime.api-test
'portal.runtime.cson-test
'portal.runtime.edn-test
'portal.runtime.fs-test
Expand Down

0 comments on commit 486bcc5

Please sign in to comment.