<commit_before>(ns reagent.impl.component
  (:require [reagent.impl.util :as util]
            [reagent.impl.batching :as batch]
            [reagent.ratom :as ratom]
            [reagent.interop :refer-macros [.' .!]]
            [reagent.debug :refer-macros [dbg prn dev? warn]]))

(declare ^:dynamic *current-component*)

(declare ^:dynamic *non-reactive*)


(defn extract-props [v]
  (let [p (nth v 1 nil)]
    (if (map? p) p)))

(defn extract-children [v]
  (let [p (nth v 1 nil)
        first-child (if (or (nil? p) (map? p)) 2 1)]
    (if (> (count v) first-child)
      (subvec v first-child))))

(defn get-argv [c]
  (.' c :props.argv))

(defn get-props [c]
  (-> (.' c :props.argv) extract-props))

(defn get-children [c]
  (-> (.' c :props.argv) extract-children))

(defn reagent-component? [c]
  (-> (.' c :props.argv) nil? not))


;;; State

(defn state-atom [this]
  (let [sa (.' this :cljsState)]
    (if-not (nil? sa)
      sa
      (.! this :cljsState (ratom/atom nil)))))

;; ugly circular dependency
(defn as-element [x]
  (js/reagent.impl.template.as-element x))

;;; Rendering

(defn reagent-class? [c]
  (and (fn? c)
       (some? (.' c :prototype.reagentRender))))

(defn do-render-sub [c]
  (let [f (.' c :reagentRender)
        _ (assert (ifn? f))
        p (.' c :props)
        res (if (true? (.' c :cljsLegacyRender))
              (f c)
              (let [argv (.' p :argv)
                    n (count argv)]
                (case n
                  1 (f)
                  2 (f (nth argv 1))
                  3 (f (nth argv 1) (nth argv 2))
                  4 (f (nth argv 1) (nth argv 2) (nth argv 3))
                  5 (f (nth argv 1) (nth argv 2) (nth argv 3) (nth argv 4))
                  (apply f (subvec argv 1)))))]
    (cond
      (vector? res) (as-element res)
      (ifn? res) (let [f (if (reagent-class? res)
                           (fn [& args]
                             (as-element (apply vector res args)))
                           res)]
                   (.! c :reagentRender f)
                   (recur c))
      :else res)))

(declare comp-name)

(defn do-render [c]
  (binding [*current-component* c]
    (if (dev?)
      ;; Log errors, without using try/catch (and mess up call stack)
      (let [ok (array false)]
        (try
          (let [res (do-render-sub c)]
            (aset ok 0 true)
            res)
          (finally
            (when-not (aget ok 0)
              (js/console.error (str "Error rendering component "
                                     (comp-name)))))))
      (do-render-sub c))))


;;; Method wrapping

(def rat-opts {:no-cache true})

(def static-fns
  {:render
   (fn render []
     (this-as c (if *non-reactive*
                  (do-render c)
                  (let [rat (.' c :cljsRatom)]
                    (batch/mark-rendered c)
                    (if (nil? rat)
                      (ratom/run-in-reaction #(do-render c) c "cljsRatom"
                                             batch/queue-render rat-opts)
                      (._run rat))))))})

(defn custom-wrapper [key f]
  (case key
    :getDefaultProps
    (assert false "getDefaultProps not supported yet")

    :getInitialState
    (fn []
      (this-as c
               (reset! (state-atom c) (f c))))

    :componentWillReceiveProps
    (fn [props]
      (this-as c
               (f c (.' props :argv))))

    :shouldComponentUpdate
    (fn [nextprops nextstate]
      (or util/*always-update*
          (this-as c
                   ;; Don't care about nextstate here, we use forceUpdate
                   ;; when only when state has changed anyway.
                   (let [old-argv (.' c :props.argv)
                         new-argv (.' nextprops :argv)]
                     (if (nil? f)
                       (or (nil? old-argv)
                           (nil? new-argv)
                           (not= old-argv new-argv))
                       (f c old-argv new-argv))))))

    :componentWillUpdate
    (fn [nextprops]
      (this-as c
               (f c (.' nextprops :argv))))

    :componentDidUpdate
    (fn [oldprops]
      (this-as c
               (f c (.' oldprops :argv))))

    :componentWillMount
    (fn []
      (this-as c
               (.! c :cljsMountOrder (batch/next-mount-count))
               (when-not (nil? f)
                 (f c))))

    :componentWillUnmount
    (fn []
      (this-as c
               (some-> (.' c :cljsRatom)
                       ratom/dispose!)
               (batch/mark-rendered c)
               (when-not (nil? f)
                 (f c))))

    nil))

(defn default-wrapper [f]
  (if (ifn? f)
    (fn [& args]
      (this-as c (apply f c args)))
    f))

(def dont-wrap #{:render :reagentRender})

(defn dont-bind [f]
  (if (fn? f)
    (doto f<commit_msg>Use functions to access argv<commit_after>(ns reagent.impl.component
  (:require [reagent.impl.util :as util]
            [reagent.impl.batching :as batch]
            [reagent.ratom :as ratom]
            [reagent.interop :refer-macros [.' .!]]
            [reagent.debug :refer-macros [dbg prn dev? warn]]))

(declare ^:dynamic *current-component*)

(declare ^:dynamic *non-reactive*)


;;; Argv access

(defn extract-props [v]
  (let [p (nth v 1 nil)]
    (if (map? p) p)))

(defn extract-children [v]
  (let [p (nth v 1 nil)
        first-child (if (or (nil? p) (map? p)) 2 1)]
    (if (> (count v) first-child)
      (subvec v first-child))))

(defn props-argv [p]
  (.' p :argv))

(defn get-argv [c]
  (.' c :props.argv))

(defn get-props [c]
  (-> (get-argv c) extract-props))

(defn get-children [c]
  (-> (get-argv c) extract-children))

(defn reagent-component? [c]
  (-> (get-argv c) nil? not))


;;; State

(defn state-atom [this]
  (let [sa (.' this :cljsState)]
    (if-not (nil? sa)
      sa
      (.! this :cljsState (ratom/atom nil)))))

;; ugly circular dependency
(defn as-element [x]
  (js/reagent.impl.template.as-element x))


;;; Rendering

(defn reagent-class? [c]
  (and (fn? c)
       (some? (.' c :prototype.reagentRender))))

(defn do-render-sub [c]
  (let [f (.' c :reagentRender)
        _ (assert (ifn? f))
        res (if (true? (.' c :cljsLegacyRender))
              (f c)
              (let [argv (get-argv c)
                    n (count argv)]
                (case n
                  1 (f)
                  2 (f (nth argv 1))
                  3 (f (nth argv 1) (nth argv 2))
                  4 (f (nth argv 1) (nth argv 2) (nth argv 3))
                  5 (f (nth argv 1) (nth argv 2) (nth argv 3) (nth argv 4))
                  (apply f (subvec argv 1)))))]
    (cond
      (vector? res) (as-element res)
      (ifn? res) (let [f (if (reagent-class? res)
                           (fn [& args]
                             (as-element (apply vector res args)))
                           res)]
                   (.! c :reagentRender f)
                   (recur c))
      :else res)))

(declare comp-name)

(defn do-render [c]
  (binding [*current-component* c]
    (if (dev?)
      ;; Log errors, without using try/catch (and mess up call stack)
      (let [ok (array false)]
        (try
          (let [res (do-render-sub c)]
            (aset ok 0 true)
            res)
          (finally
            (when-not (aget ok 0)
              (js/console.error (str "Error rendering component "
                                     (comp-name)))))))
      (do-render-sub c))))


;;; Method wrapping

(def rat-opts {:no-cache true})

(def static-fns
  {:render
   (fn render []
     (this-as c (if *non-reactive*
                  (do-render c)
                  (let [rat (.' c :cljsRatom)]
                    (batch/mark-rendered c)
                    (if (nil? rat)
                      (ratom/run-in-reaction #(do-render c) c "cljsRatom"
                                             batch/queue-render rat-opts)
                      (._run rat))))))})

(defn custom-wrapper [key f]
  (case key
    :getDefaultProps
    (assert false "getDefaultProps not supported yet")

    :getInitialState
    (fn []
      (this-as c
               (reset! (state-atom c) (f c))))

    :componentWillReceiveProps
    (fn [props]
      (this-as c
               (f c (get-argv c))))

    :shouldComponentUpdate
    (fn [nextprops nextstate]
      (or util/*always-update*
          (this-as c
                   ;; Don't care about nextstate here, we use forceUpdate
                   ;; when only when state has changed anyway.
                   (let [old-argv (.' c :props.argv)
                         new-argv (.' nextprops :argv)]
                     (if (nil? f)
                       (or (nil? old-argv)
                           (nil? new-argv)
                           (not= old-argv new-argv))
                       (f c old-argv new-argv))))))

    :componentWillUpdate
    (fn [nextprops]
      (this-as c
               (f c (props-argv nextprops))))

    :componentDidUpdate
    (fn [oldprops]
      (this-as c
               (f c (props-argv oldprops))))

    :componentWillMount
    (fn []
      (this-as c
               (.! c :cljsMountOrder (batch/next-mount-count))
               (when-not (nil? f)
                 (f c))))

    :componentWillUnmount
    (fn []
      (this-as c
               (some-> (.' c :cljsRatom)
                       ratom/dispose!)
               (batch/mark-rendered c)
               (when-not (nil? f)
                 (f c))))

    nil))

(defn default-wrapper [f]
  (if (ifn? f)
    (fn [& args]
      (this-as c (apply f c args)))
    f))

(def dont-wrap #{:render :reagentRender})

(defn dont-bind [f]
  (if (fn? f)
    (doto f
{'fraction_non_alphanumeric': 0.14816737618108192, 'fraction_numerical': 0.004568580625064895, 'mean_word_length': 1.4508905852417302, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1341207', 'n_tokens_mistral': 3273, 'n_tokens_neox': 3123, 'n_words': 915}