Skip to content

Commit 0d5a3f9

Browse files
committed
backporting PR:59 Guard against race condition
1 parent 6a3c9bc commit 0d5a3f9

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/spaniel-observer.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -193,39 +193,41 @@ export class SpanielObserver implements SpanielObserverInterface {
193193
let { time } = entry;
194194
let target = <SpanielTrackedElement>entry.target;
195195
let record = this.recordStore[target.__spanielId];
196-
record.lastSeenEntry = entry;
197196

198-
if (!this.paused) {
199-
record.thresholdStates.forEach((state: SpanielThresholdState) => {
200-
// Find the thresholds that were crossed. Since you can have multiple thresholds
201-
// for the same ratio, could be multiple thresholds
202-
let hasTimeThreshold = !!state.threshold.time;
203-
let spanielEntry: SpanielObserverEntry = this.generateSpanielEntry(entry, state);
197+
if (record) {
198+
record.lastSeenEntry = entry;
199+
if (!this.paused) {
200+
record.thresholdStates.forEach((state: SpanielThresholdState) => {
201+
// Find the thresholds that were crossed. Since you can have multiple thresholds
202+
// for the same ratio, could be multiple thresholds
203+
let hasTimeThreshold = !!state.threshold.time;
204+
let spanielEntry: SpanielObserverEntry = this.generateSpanielEntry(entry, state);
204205

205-
const ratioSatisfied = entrySatisfiesRatio(entry, state.threshold.ratio);
206+
const ratioSatisfied = entrySatisfiesRatio(entry, state.threshold.ratio);
206207

207-
if (ratioSatisfied && !state.lastSatisfied) {
208-
spanielEntry.entering = true;
209-
if (hasTimeThreshold) {
210-
state.lastVisible = time;
211-
const timerId: number = Number(setTimeout(() => {
208+
if (ratioSatisfied && !state.lastSatisfied) {
209+
spanielEntry.entering = true;
210+
if (hasTimeThreshold) {
211+
state.lastVisible = time;
212+
const timerId: number = Number(setTimeout(() => {
213+
state.visible = true;
214+
spanielEntry.duration = Date.now() - state.lastVisible;
215+
this.callback([spanielEntry]);
216+
}, state.threshold.time));
217+
state.timeoutId = timerId;
218+
} else {
212219
state.visible = true;
213-
spanielEntry.duration = Date.now() - state.lastVisible;
214-
this.callback([spanielEntry]);
215-
}, state.threshold.time));
216-
state.timeoutId = timerId;
217-
} else {
218-
state.visible = true;
219-
this.queuedEntries.push(spanielEntry);
220+
this.queuedEntries.push(spanielEntry);
221+
}
222+
} else if (!ratioSatisfied) {
223+
this.handleThresholdExiting(spanielEntry, state);
220224
}
221-
} else if (!ratioSatisfied) {
222-
this.handleThresholdExiting(spanielEntry, state);
223-
}
224225

225-
state.lastEntry = entry;
226-
state.lastSatisfied = ratioSatisfied;
227-
});
228-
this.flushQueuedEntries();
226+
state.lastEntry = entry;
227+
state.lastSatisfied = ratioSatisfied;
228+
});
229+
this.flushQueuedEntries();
230+
}
229231
}
230232
}
231233
disconnect() {

0 commit comments

Comments
 (0)