optimized protocol invocations for cljs

This commit is contained in:
Nathan Marz 2015-07-03 16:38:06 -04:00
parent a76f124579
commit 80e1e3dd30
2 changed files with 21 additions and 8 deletions

View file

@ -1,4 +1,6 @@
(ns com.rpl.specter.impl
#?(:cljs (:require-macros [com.rpl.specter.prot-opt-invoke
:refer [mk-optimized-invocation]]))
(:use [com.rpl.specter.protocols :only
[comp-paths*
select* transform* collect-val select-full* transform-full*]])
@ -100,20 +102,21 @@
(find-protocol-impl! p/StructureValsPath this))
))
#?(:cljs
(do
(defn structure-path-impl [_]
{:select* select*
:transform* transform*
(defn structure-path-impl [obj]
{:select* (mk-optimized-invocation p/StructurePath obj select* 2)
:transform* (mk-optimized-invocation p/StructurePath obj transform* 2)
})
(defn collector-impl [_]
{:collect-val collect-val
(defn collector-impl [obj]
{:collect-val (mk-optimized-invocation p/Collector obj collect-val 1)
})
(defn structure-vals-path-impl [_]
{:select-full* select-full*
:transform-full* transform-full*
(defn structure-vals-path-impl [obj]
{:select-full* (mk-optimized-invocation p/StructureValsPath obj select-full* 3)
:transform-full* (mk-optimized-invocation p/StructureValsPath obj transform-full* 3)
})
))

View file

@ -0,0 +1,10 @@
(ns com.rpl.specter.prot-opt-invoke)
(defmacro mk-optimized-invocation [protocol obj method num-args]
(let [args (take num-args (repeatedly gensym))]
`(if (~'implements? ~protocol ~obj)
(fn [^not-native o# ~@args]
(~method o# ~@args)
)
~method
)))