mirror of https://github.com/status-im/reagent.git
Remove deprecated interop macros
This commit is contained in:
parent
178aaee030
commit
9173f453b5
|
@ -1,5 +1,4 @@
|
||||||
(ns reagent.interop
|
(ns reagent.interop)
|
||||||
(:require [clojure.string :as string :refer [join]]))
|
|
||||||
|
|
||||||
; taken from cljs.core
|
; taken from cljs.core
|
||||||
; https://github.com/binaryage/cljs-oops/issues/14
|
; https://github.com/binaryage/cljs-oops/issues/14
|
||||||
|
@ -19,73 +18,3 @@
|
||||||
(let [n (dec (count idxv))
|
(let [n (dec (count idxv))
|
||||||
astr (apply str (repeat n "[~{}]"))]
|
astr (apply str (repeat n "[~{}]"))]
|
||||||
`(~'js* ~(str "(~{}[~{}][~{}]" astr " = ~{})") ~array ~idx ~idx2 ~@idxv))))
|
`(~'js* ~(str "(~{}[~{}][~{}]" astr " = ~{})") ~array ~idx ~idx2 ~@idxv))))
|
||||||
|
|
||||||
(defn- js-call [f args]
|
|
||||||
(let [argstr (->> (repeat (count args) "~{}")
|
|
||||||
(join ","))]
|
|
||||||
(list* 'js* (str "~{}(" argstr ")") f args)))
|
|
||||||
|
|
||||||
(defn- dot-args [object member]
|
|
||||||
(assert (or (symbol? member)
|
|
||||||
(keyword? member))
|
|
||||||
(str "Symbol or keyword expected, not " (pr-str member)))
|
|
||||||
(assert (or (not (symbol? object))
|
|
||||||
(not (re-find #"\." (name object))))
|
|
||||||
(str "Dot not allowed in " object))
|
|
||||||
(let [n (name member)
|
|
||||||
field? (or (keyword? member)
|
|
||||||
(= (subs n 0 1) "-"))
|
|
||||||
names (-> (if (symbol? member)
|
|
||||||
(string/replace n #"^-" "")
|
|
||||||
n)
|
|
||||||
(string/split #"\."))]
|
|
||||||
[field? names]))
|
|
||||||
|
|
||||||
(defonce $-warning
|
|
||||||
(delay
|
|
||||||
(try
|
|
||||||
(cljs.util/debug-prn "WARNING: reagent.interop/$ has been deprecated. Consider using ClojureScript JS-interop forms or goog.object namespace instead.")
|
|
||||||
(catch Exception _ nil))))
|
|
||||||
|
|
||||||
(defmacro $
|
|
||||||
"Access member in a javascript object, in a Closure-safe way.
|
|
||||||
'member' is assumed to be a field if it is a keyword or if
|
|
||||||
the name starts with '-', otherwise the named function is
|
|
||||||
called with the optional args.
|
|
||||||
'member' may contain '.', to allow access in nested objects.
|
|
||||||
If 'object' is a symbol it is not allowed contain '.'.
|
|
||||||
|
|
||||||
($ o :foo) is equivalent to (.-foo o), except that it gives
|
|
||||||
the same result under advanced compilation.
|
|
||||||
($ o foo arg1 arg2) is the same as (.foo o arg1 arg2)."
|
|
||||||
{:deprecated true}
|
|
||||||
[object member & args]
|
|
||||||
@$-warning
|
|
||||||
(let [[field names] (dot-args object member)]
|
|
||||||
(if field
|
|
||||||
(do
|
|
||||||
(assert (empty? args)
|
|
||||||
(str "Passing args to field doesn't make sense: " member))
|
|
||||||
`(unchecked-aget ~object ~@names))
|
|
||||||
(js-call (list* 'reagent.interop/unchecked-aget object names) args))))
|
|
||||||
|
|
||||||
(defonce $!-warning
|
|
||||||
(delay
|
|
||||||
(try
|
|
||||||
(cljs.util/debug-prn "WARNING: reagent.interop/$! has been deprecated. Consider using ClojureScript JS-interop forms or goog.object namespace instead.")
|
|
||||||
(catch Exception _ nil))))
|
|
||||||
|
|
||||||
(defmacro $!
|
|
||||||
"Set field in a javascript object, in a Closure-safe way.
|
|
||||||
'field' should be a keyword or a symbol starting with '-'.
|
|
||||||
'field' may contain '.', to allow access in nested objects.
|
|
||||||
If 'object' is a symbol it is not allowed contain '.'.
|
|
||||||
|
|
||||||
($! o :foo 1) is equivalent to (set! (.-foo o) 1), except that it
|
|
||||||
gives the same result under advanced compilation."
|
|
||||||
{:deprecated true}
|
|
||||||
[object field value]
|
|
||||||
@$!-warning
|
|
||||||
(let [[field names] (dot-args object field)]
|
|
||||||
(assert field (str "Field name must start with - in " field))
|
|
||||||
`(unchecked-aset ~object ~@names ~value)))
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
(ns reagent.ratom
|
(ns reagent.ratom
|
||||||
(:refer-clojure :exclude [run!])
|
(:refer-clojure :exclude [run!])
|
||||||
(:require [reagent.debug :as d]))
|
(:require [reagent.debug :as d]
|
||||||
|
[reagent.interop :as interop]))
|
||||||
|
|
||||||
(defmacro reaction [& body]
|
(defmacro reaction [& body]
|
||||||
`(reagent.ratom/make-reaction
|
`(reagent.ratom/make-reaction
|
||||||
|
@ -14,25 +15,6 @@
|
||||||
(deref co#)
|
(deref co#)
|
||||||
co#))
|
co#))
|
||||||
|
|
||||||
; taken from cljs.core
|
|
||||||
; https://github.com/binaryage/cljs-oops/issues/14
|
|
||||||
(defmacro unchecked-aget
|
|
||||||
([array idx]
|
|
||||||
(list 'js* "(~{}[~{}])" array idx))
|
|
||||||
([array idx & idxs]
|
|
||||||
(let [astr (apply str (repeat (count idxs) "[~{}]"))]
|
|
||||||
`(~'js* ~(str "(~{}[~{}]" astr ")") ~array ~idx ~@idxs))))
|
|
||||||
|
|
||||||
; taken from cljs.core
|
|
||||||
; https://github.com/binaryage/cljs-oops/issues/14
|
|
||||||
(defmacro unchecked-aset
|
|
||||||
([array idx val]
|
|
||||||
(list 'js* "(~{}[~{}] = ~{})" array idx val))
|
|
||||||
([array idx idx2 & idxv]
|
|
||||||
(let [n (dec (count idxv))
|
|
||||||
astr (apply str (repeat n "[~{}]"))]
|
|
||||||
`(~'js* ~(str "(~{}[~{}][~{}]" astr " = ~{})") ~array ~idx ~idx2 ~@idxv))))
|
|
||||||
|
|
||||||
(defmacro with-let [bindings & body]
|
(defmacro with-let [bindings & body]
|
||||||
(assert (vector? bindings)
|
(assert (vector? bindings)
|
||||||
(str "with-let bindings must be a vector, not "
|
(str "with-let bindings must be a vector, not "
|
||||||
|
@ -46,8 +28,8 @@
|
||||||
x
|
x
|
||||||
(let [j (quot i 2)]
|
(let [j (quot i 2)]
|
||||||
`(if ~init
|
`(if ~init
|
||||||
(unchecked-aset ~v ~j ~x)
|
(interop/unchecked-aset ~v ~j ~x)
|
||||||
(unchecked-aget ~v ~j)))))
|
(interop/unchecked-aget ~v ~j)))))
|
||||||
bindings))
|
bindings))
|
||||||
[forms destroy] (let [fin (last body)]
|
[forms destroy] (let [fin (last body)]
|
||||||
(if (and (list? fin)
|
(if (and (list? fin)
|
||||||
|
|
|
@ -1,56 +0,0 @@
|
||||||
(ns reagenttest.testinterop
|
|
||||||
(:require [cljs.test :as t :refer-macros [is deftest]]
|
|
||||||
[reagent.debug :refer-macros [dbg]]
|
|
||||||
[reagent.interop :refer-macros [$ $!]]))
|
|
||||||
|
|
||||||
;; (def is-adv (let [o #js{}]
|
|
||||||
;; (set! (.-somethinglong o) true)
|
|
||||||
;; (not= (aget (.keys js/Object o) 0) "somethinglong")))
|
|
||||||
|
|
||||||
(deftest iterop-quote
|
|
||||||
(let [o #js{:foo "foo"
|
|
||||||
:foobar #js{:bar "bar"
|
|
||||||
:bar-foo "bar-foo"}
|
|
||||||
:bar-foo "barfoo"}]
|
|
||||||
|
|
||||||
(is (= "foo" ($ o :foo)))
|
|
||||||
(is (= "bar" ($ o :foobar.bar)))
|
|
||||||
(is (= "barfoo" ($ o :bar-foo)))
|
|
||||||
|
|
||||||
(is (= "foo" ($ o -foo)))
|
|
||||||
(is (= "bar" ($ o -foobar.bar)))
|
|
||||||
(is (= "bar-foo" ($ o -foobar.bar-foo)))
|
|
||||||
(is (= "bar-foo" ($ o :foobar.bar-foo)))
|
|
||||||
|
|
||||||
($! o :foo "foo1")
|
|
||||||
(is (= "foo1" ($ o :foo)))
|
|
||||||
|
|
||||||
($! o -foo "foo2")
|
|
||||||
(is (= "foo2" ($ o -foo)))
|
|
||||||
|
|
||||||
($! o :foobar.bar "bar1")
|
|
||||||
(is (= "bar1" ($ o :foobar.bar)))
|
|
||||||
|
|
||||||
($! o -foobar.bar "bar1")
|
|
||||||
(is (= "bar1" ($ o -foobar.bar)))))
|
|
||||||
|
|
||||||
(deftest interop-quote-call
|
|
||||||
(let [o #js{:bar "bar1"
|
|
||||||
:foo (fn [x]
|
|
||||||
(this-as this
|
|
||||||
(str x ($ this :bar))))}
|
|
||||||
o2 #js{:o o}]
|
|
||||||
(is (= "ybar1" ($ o foo "y")))
|
|
||||||
(is (= "xxbar1" ($ o2 o.foo "xx")))
|
|
||||||
(is (= "abar1" (-> o2
|
|
||||||
($ :o)
|
|
||||||
($ foo "a"))))
|
|
||||||
|
|
||||||
(is (= "bar1" ($ o foo)))
|
|
||||||
(is (= "bar1" ($ o2 o.foo)))
|
|
||||||
|
|
||||||
($! o :bar "bar2")
|
|
||||||
(is (= "bar2" ($ o foo)))
|
|
||||||
|
|
||||||
(is (= "1bar2" ($ ($ o :foo)
|
|
||||||
call o 1)))))
|
|
Loading…
Reference in New Issue