Skip to content

Commit

Permalink
Debounce tracing callbacks to improve tracing efficiency
Browse files Browse the repository at this point in the history
  • Loading branch information
danielcompton committed Nov 29, 2017
1 parent d48fe58 commit 3de2ff8
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Expand Up @@ -5,6 +5,10 @@
- add `purge-event-queue` to the API. See https://github.com/Day8/re-frame-test/issues/13 for motivation.
- added [a new FAQ entry](/docs/FAQs/DoINeedReFrame.md) Reagent looks terrific. Why do I need re-frame?

#### Changed

- Debounce trace callbacks to handle larger batches of traces at once, to improve efficiency.

## 0.10.2 (2017.10.07)

#### New Features
Expand Down
33 changes: 24 additions & 9 deletions src/re_frame/trace.cljc
Expand Up @@ -4,8 +4,9 @@
#?(:cljs (:require-macros [net.cgrand.macrovich :as macros]
[re-frame.trace :refer [finish-trace with-trace merge-trace!]]))
(:require [re-frame.interop :as interop]
[re-frame.loggers :refer [console]]
#?(:clj [net.cgrand.macrovich :as macros])
[re-frame.loggers :refer [console]]))
#?(:cljs [goog.functions])))

(def id (atom 0))
(def ^:dynamic *current-trace* nil)
Expand All @@ -22,6 +23,7 @@
trace-enabled?)

(def trace-cbs (atom {}))
(defonce traces (atom []))

(defn register-trace-cb
"Registers a tracing callback function which will receive a collection of one or more traces.
Expand All @@ -45,19 +47,32 @@
:child-of (or child-of (:id *current-trace*))
:start (interop/now)})

(defn debounce [f interval]
#?(:cljs (goog.functions/debounce f interval)
:clj (f)))

(def run-tracing-callbacks!
(debounce
(fn []
(doseq [[k cb] @trace-cbs]
(try (cb @traces)
#?(:clj (catch Exception e
(console :error "Error thrown from trace cb" k "while storing" @traces e)))
#?(:cljs (catch :default e
(console :error "Error thrown from trace cb" k "while storing" @traces e))))
(reset! traces [])))
50))


(macros/deftime
(defmacro finish-trace [trace]
`(when (is-trace-enabled?)
(let [end# (interop/now)
duration# (- end# (:start ~trace))]
(doseq [[k# cb#] @trace-cbs]
(try (cb# [(assoc ~trace
:duration duration#
:end (interop/now))])
#?(:clj (catch Exception e#
(console :error "Error thrown from trace cb" k# "while storing" ~trace e#)))
#?(:cljs (catch :default e#
(console :error "Error thrown from trace cb" k# "while storing" ~trace e#))))))))
(swap! traces conj (assoc ~trace
:duration duration#
:end (interop/now)))
(run-tracing-callbacks!))))

(defmacro with-trace
"Create a trace inside the scope of the with-trace macro
Expand Down

0 comments on commit 3de2ff8

Please sign in to comment.