Skip to content

Commit 1205a80

Browse files
committed
Simplify keypress debounce code by using "immediate" mode
1 parent 4ac9f5b commit 1205a80

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

src/raven.js

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ function Raven() {
6464
this._breadcrumbs = [];
6565
this._breadcrumbLimit = 20;
6666
this._lastCapturedEvent = null;
67-
this._keypressTimeout = null;
67+
this._keypressTimeout;
6868
this._location = window.location;
6969
this._lastHref = this._location && this._location.href;
7070

@@ -622,19 +622,16 @@ Raven.prototype = {
622622
/**
623623
* Wraps addEventListener to capture UI breadcrumbs
624624
* @param evtName the event name (e.g. "click")
625-
* @param fn the function being wrapped
626625
* @returns {Function}
627626
* @private
628627
*/
629628
_breadcrumbEventHandler: function(evtName) {
630629
var self = this;
631630
return function (evt) {
632-
// if there's a keypress event still queued up (debounce
633-
// hasn't flushed yet), flush it immediately before processing
634-
// this event
635-
if (self._keypressTimeout) {
636-
self._keypressCapture();
637-
}
631+
// reset keypress timeout; e.g. triggering a 'click' after
632+
// a 'keypress' will reset the keypress debounce so that a new
633+
// set of keypresses can be recorded
634+
self._keypressTimeout = null;
638635

639636
// It's possible this handler might trigger multiple times for the same
640637
// event (e.g. event propagation through node ancestors). Ignore if we've
@@ -662,25 +659,18 @@ Raven.prototype = {
662659
};
663660
},
664661

665-
_keypressCapture: function(evt) {
666-
evt = evt || this._lastKeypressEvent;
667-
this._lastKeypressEvent = null;
668-
669-
if (this._keypressTimeout) {
670-
clearTimeout(this._keypressTimeout);
671-
this._keypressTimeout = null;
672-
}
673-
674-
return this._breadcrumbEventHandler('input')(evt);
675-
},
676-
662+
/**
663+
* Wraps addEventListener to capture keypress UI events
664+
* @returns {Function}
665+
* @private
666+
*/
677667
_keypressEventHandler: function() {
678-
var self = this;
679-
var debounceDuration = 1000; // milliseconds
668+
var self = this,
669+
debounceDuration = 1000; // milliseconds
680670

681671
// TODO: if somehow user switches keypress target before
682672
// debounce timeout is triggered, we will only capture
683-
// a single breadcrumb from the LAST target (acceptable?)
673+
// a single breadcrumb from the FIRST target (acceptable?)
684674

685675
return function (evt) {
686676
var target = evt.target,
@@ -692,10 +682,15 @@ Raven.prototype = {
692682
if (!tagName || tagName !== 'INPUT' && tagName !== 'TEXTAREA')
693683
return;
694684

695-
clearTimeout(self._keypressTimeout);
696-
self._lastKeypressEvent = evt;
685+
// record first keypress in a series, but ignore subsequent
686+
// keypresses until debounce clears
687+
var timeout = self._keypressTimeout;
688+
if (!timeout) {
689+
self._breadcrumbEventHandler('input')(evt);
690+
}
691+
clearTimeout(timeout);
697692
self._keypressTimeout = setTimeout(function () {
698-
self._keypressCapture(evt);
693+
self._keypressTimeout = null;
699694
}, debounceDuration);
700695
};
701696
},
@@ -1201,10 +1196,6 @@ Raven.prototype = {
12011196
// Send along our own collected metadata with extra
12021197
data.extra['session:duration'] = now() - this._startTime;
12031198

1204-
// flush debounced keypress breadcrumb capture (if there is one)
1205-
if (this._keypressTimeout) {
1206-
this._keypressCapture();
1207-
}
12081199
if (this._breadcrumbs && this._breadcrumbs.length > 0) {
12091200
// intentionally make shallow copy so that additions
12101201
// to breadcrumbs aren't accidentally sent in this request

0 commit comments

Comments
 (0)