<commit_before>(ns scrum.reconciler)

(defprotocol IReconciler
  (dispatch! [this controller action args])
  (broadcast! [this action args]))

(deftype Reconciler [state controllers meta]
  Object
  (equiv [this other]
    (-equiv this other))

  IAtom

  IMeta
  (-meta [_] meta)

  IEquiv
  (-equiv [this other]
    (identical? this other))

  IDeref
  (-deref [_]
    (-deref state))

  IWatchable
  (-add-watch [this key callback]
    (add-watch state (list this key)
      (fn [_ _ oldv newv]
        (when (not= oldv newv)
          (callback key this oldv newv))))
    this)

  (-remove-watch [this key]
    (remove-watch state (list this key))
    this)

  IHash
  (-hash [this] (goog/getUid this))

  IReset
  (-reset! [_ newv]
    (-swap! state (constantly newv)))

  ISwap
  (-swap! [this f]
    (reset! state (f (-deref state))))
  (-swap! [this f a]
    (reset! state (f (-deref state) a)))
  (-swap! [this f a b]
    (reset! state (f (-deref state) a b)))
  (-swap! [this f a b rest]
    (reset! state (apply f (-deref state) a b rest)))

  IPrintWithWriter
  (-pr-writer [this writer opts]
    (-write writer "#object [scrum.reconciler.Reconciler ")
    (pr-writer {:val (-deref this)} writer opts)
    (-write writer "]"))

  IReconciler
  (dispatch! [this cname action args]
    (-swap! this #(let [ctrl (get controllers cname)
                        cstate (get % cname)]
                    (->> cstate
                      (ctrl action args)
                      (assoc % cname)))))

  (broadcast! [this action args]
    (doseq [controller (keys controllers)]
      (dispatch! this controller action args))))<commit_msg>remove ISwap & IReset protocols impl and introduce updates batching & scheduling<commit_after>(ns scrum.reconciler)

(def queue (volatile! []))
(def schedule (volatile! []))

(defn- queue-update! [f]
  (vswap! queue conj f))

(defn- schedule-update! [f]
  (vswap! schedule conj f)
  (js/requestAnimationFrame #(when-not (zero? (count @schedule)) (f))))


(defprotocol IReconciler
  (dispatch! [this controller action args])
  (dispatch-sync! [this controller action args])
  (broadcast! [this action args])
  (broadcast-sync! [this action args]))

(deftype Reconciler [state controllers meta]
  Object
  (equiv [this other]
    (-equiv this other))

  IAtom

  IMeta
  (-meta [_] meta)

  IEquiv
  (-equiv [this other]
    (identical? this other))

  IDeref
  (-deref [_]
    (-deref state))

  IWatchable
  (-add-watch [this key callback]
    (add-watch state (list this key)
      (fn [_ _ oldv newv]
        (when (not= oldv newv)
          (callback key this oldv newv))))
    this)

  (-remove-watch [this key]
    (remove-watch state (list this key))
    this)

  IHash
  (-hash [this] (goog/getUid this))

  IPrintWithWriter
  (-pr-writer [this writer opts]
    (-write writer "#object [scrum.reconciler.Reconciler ")
    (pr-writer {:val (-deref this)} writer opts)
    (-write writer "]"))

  IReconciler
  (dispatch! [this cname action args]
    (queue-update! #(let [ctrl (get controllers cname)
                          cstate (get % cname)]
                      (->> cstate
                        (ctrl action args)
                        (assoc % cname))))
    (schedule-update!
     #(swap! state (fn [state]
                     (let [new-state (reduce (fn [state f] (f state)) state @queue)]
                       (vreset! queue [])
                       (vreset! schedule [])
                       new-state)))))

  (dispatch-sync! [this cname action args]
    (swap! state #(let [ctrl (get controllers cname)
                        cstate (get % cname)]
                    (->> cstate
                      (ctrl action args)
                      (assoc % cname)))))

  (broadcast! [this action args]
    (doseq [controller (keys controllers)]
      (dispatch! this controller action args)))

  (broadcast-sync! [this action args]
    (doseq [controller (keys controllers)]
      (dispatch-sync! this controller action args))))
{'fraction_non_alphanumeric': 0.1352838209552388, 'fraction_numerical': 0.0, 'mean_word_length': 2.2976092333058533, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '208852', 'n_tokens_mistral': 1341, 'n_tokens_neox': 1264, 'n_words': 407}