Created
July 3, 2015 14:37
-
-
Save rsslldnphy/5c937167380dd3442076 to your computer and use it in GitHub Desktop.
custom register-sub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(if-not goog.DEBUG | |
(def register-sub rf/register-sub) | |
(do | |
(defonce subcounts (atom {})) | |
(aset js/document "subcounts" #(.table js/console (clj->js | |
(for [[k vs] | |
(reverse (sort-by :total-time @subcounts))] | |
(assoc vs :subscription (str k)))))) | |
(aset js/document "subcountsclear" #(reset! subcounts {})) | |
(aset js/document "subcounttotal" #(pr (apply + 0 (map :total-time (vals @subcounts))))) | |
(defn update-sub-count | |
[subcount duration] | |
(let [call-count (inc (:call-count subcount 0)) | |
total-time (+ duration (:total-time subcount 0)) | |
average-time (/ total-time call-count) | |
min-time (min (:min-time subcount js/Infinity) duration) | |
max-time (max (:max-time subcount 0) duration) | |
first-time (:first-time subcount duration) | |
last-time duration] | |
{:call-count call-count | |
:total-time total-time | |
:average-time average-time | |
:first-time first-time | |
:last-time last-time | |
:min-time min-time | |
:max-time max-time})) | |
(defn register-sub | |
([k _ f] (register-sub k f)) | |
([k f] (rf/register-sub | |
k | |
(fn [db v] (let [child (f db v)] | |
(reaction | |
(let [before (.getTime (js/Date.)) | |
child @child | |
after (.getTime (js/Date.))] | |
(swap! subcounts update k update-sub-count (- after before)) | |
child))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment