<commit_before>(ns re-view.core
  (:require-macros [re-view.core :refer [defview]])
  (:require [re-db.core :as d]
            [goog.object :as gobj]))

(def ^:dynamic *trigger-state-render* true)

(defn mounted? [c]
  (.isMounted c))

(defn register-cell-mount [db eid]
  (when-let [html-node (d/get @db eid :cell/html-node)]
    (when-let [f (d/get @db eid :cell/on-mount)]
      (f html-node))))

(defn children [this]
  (some-> this .-props .-children))

(defn react-ref
  "Returns the component associated with a component's React ref."
  [component name]
  (some-> (.-refs component) (gobj/get name)))

(defn components-by-view-id [db id]
  (when id
    (->> (d/entities @db [:view/id id])
         (map :view/component)
         (filter mounted?))))

(defn components-by-e [db e]
  (when e (components-by-view-id db (d/get @db e :id))))

(defn register-view
  "Keep index to components based on id"
  ([db this indexes]
   (d/transact! db [(merge indexes
                        {:db/id          -1
                         :view/component this})])))

(defn deregister-view
  "Discard index"
  [db this]
  (js/setTimeout
    #(do
      (d/transact! db [[:db/retract-entity (:db/id (d/entity @db [:view/component this]))]])) 0))

(def render-count (atom 0))

(defn has-forced-props? [this]
  (= (some-> this .-state .-cljs$forcedProps .-render$count) @render-count))

(defn forced-props [this]
  (aget this "state" "cljs$forcedProps" "cljs$props"))

(defn props
  "React only supports supplying props to the root component.
  To enable forceUpdate with new props, we put props in state along with the current
  value of render-counter. We only use the force$value if its render$count is equal to
  the current render."
  [this]
  (if (has-forced-props? this)
    (forced-props this)
    (some-> this .-props .-cljs$props)))

(defn render-component
  "Force render a single component with supplied props"
  ([component] (render-component component nil))
  ([component props]
   (try
     (when props
       (set! (.. component -state -cljs$forcedProps) #js {:render$count (swap! render-count inc)
                                                          :cljs$props   props}))
     (.forceUpdate component (fn []))
     (catch js/Error e
       ;; occasional weird ReferenceError here, maybe related to figwheel reloading?
       ;; stacktrace: https://gist.github.com/mhuebert/63f7e61293bd6001800f
       (.debug js/console #_(cells.meta/cell->label (:id props))
               "React .forceUpdate error, re-rendering whole page" e)
       #_(aset js/window "e" e)
       #_(.error js/console e)
       #_(render-all)))))

(defn parse-props [props]
  (.-cljs$props props))

(defn parse-state [state]
  (.-cljs$state state))

(defn state [this]
  (some-> this .-state .-cljs$state))

(defn prev-props [this]
  (some-> this .-state .-cljs$previousProps))

(defn prev-state [this]
  (some-> this .-state .-cljs$previousState))

(defn next-state [this]
  (some-> this .-state .-cljs$nextState))

(defn advance-state [this]
  (let [prev-state (state this)
        next-state (.. this -state -cljs$nextState)
        prev-props (props this)]
    (set! (.. this -state -cljs$previousProps) prev-props)
    (set! (.. this -state -cljs$state) next-state)
    (set! (.. this -state -cljs$previousState) prev-state)))

(defn update-state! [this f & args]
  (let [new-state (apply f (cons (state this) args))]
    (when (not= new-state (state this))<commit_msg>Move error catch out of lib<commit_after>(ns re-view.core
  (:require-macros [re-view.core :refer [defview]])
  (:require [re-db.core :as d]
            [goog.object :as gobj]))

(def ^:dynamic *trigger-state-render* true)

(defn mounted? [c]
  (.isMounted c))

(defn register-cell-mount [db eid]
  (when-let [html-node (d/get @db eid :cell/html-node)]
    (when-let [f (d/get @db eid :cell/on-mount)]
      (f html-node))))

(defn children [this]
  (some-> this .-props .-children))

(defn react-ref
  "Returns the component associated with a component's React ref."
  [component name]
  (some-> (.-refs component) (gobj/get name)))

(defn components-by-view-id [db id]
  (when id
    (->> (d/entities @db [:view/id id])
         (map :view/component)
         (filter mounted?))))

(defn components-by-e [db e]
  (when e (components-by-view-id db (d/get @db e :id))))

(defn register-view
  "Keep index to components based on id"
  ([db this indexes]
   (d/transact! db [(merge indexes
                        {:db/id          -1
                         :view/component this})])))

(defn deregister-view
  "Discard index"
  [db this]
  (js/setTimeout
    #(do
      (d/transact! db [[:db/retract-entity (:db/id (d/entity @db [:view/component this]))]])) 0))

(def render-count (atom 0))

(defn has-forced-props? [this]
  (= (some-> this .-state .-cljs$forcedProps .-render$count) @render-count))

(defn forced-props [this]
  (aget this "state" "cljs$forcedProps" "cljs$props"))

(defn props
  "React only supports supplying props to the root component.
  To enable forceUpdate with new props, we put props in state along with the current
  value of render-counter. We only use the force$value if its render$count is equal to
  the current render."
  [this]
  (if (has-forced-props? this)
    (forced-props this)
    (some-> this .-props .-cljs$props)))

(defn render-component
  "Force render a single component with supplied props"
  ([component] (render-component component nil))
  ([component props]
   (when props
     (set! (.. component -state -cljs$forcedProps) #js {:render$count (swap! render-count inc)
                                                        :cljs$props   props}))
   (.forceUpdate component (fn []))))

(defn parse-props [props]
  (.-cljs$props props))

(defn parse-state [state]
  (.-cljs$state state))

(defn state [this]
  (some-> this .-state .-cljs$state))

(defn prev-props [this]
  (some-> this .-state .-cljs$previousProps))

(defn prev-state [this]
  (some-> this .-state .-cljs$previousState))

(defn next-state [this]
  (some-> this .-state .-cljs$nextState))

(defn advance-state [this]
  (let [prev-state (state this)
        next-state (.. this -state -cljs$nextState)
        prev-props (props this)]
    (set! (.. this -state -cljs$previousProps) prev-props)
    (set! (.. this -state -cljs$state) next-state)
    (set! (.. this -state -cljs$previousState) prev-state)))

(defn update-state! [this f & args]
  (let [new-state (apply f (cons (state this) args))]
    (when (not= new-state (state this))
{'fraction_non_alphanumeric': 0.16036285362853628, 'fraction_numerical': 0.0032287822878228783, 'mean_word_length': 2.804093567251462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 24, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '881908', 'n_tokens_mistral': 2298, 'n_tokens_neox': 2249, 'n_words': 677}