Skip to content

Commit

Permalink
backporting PR:59 Guard against race condition
Browse files Browse the repository at this point in the history
  • Loading branch information
lynchbomb committed Oct 9, 2018
1 parent 6a3c9bc commit 0d5a3f9
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/spaniel-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,39 +193,41 @@ export class SpanielObserver implements SpanielObserverInterface {
let { time } = entry;
let target = <SpanielTrackedElement>entry.target;
let record = this.recordStore[target.__spanielId];
record.lastSeenEntry = entry;

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

const ratioSatisfied = entrySatisfiesRatio(entry, state.threshold.ratio);
const ratioSatisfied = entrySatisfiesRatio(entry, state.threshold.ratio);

if (ratioSatisfied && !state.lastSatisfied) {
spanielEntry.entering = true;
if (hasTimeThreshold) {
state.lastVisible = time;
const timerId: number = Number(setTimeout(() => {
if (ratioSatisfied && !state.lastSatisfied) {
spanielEntry.entering = true;
if (hasTimeThreshold) {
state.lastVisible = time;
const timerId: number = Number(setTimeout(() => {
state.visible = true;
spanielEntry.duration = Date.now() - state.lastVisible;
this.callback([spanielEntry]);
}, state.threshold.time));
state.timeoutId = timerId;
} else {
state.visible = true;
spanielEntry.duration = Date.now() - state.lastVisible;
this.callback([spanielEntry]);
}, state.threshold.time));
state.timeoutId = timerId;
} else {
state.visible = true;
this.queuedEntries.push(spanielEntry);
this.queuedEntries.push(spanielEntry);
}
} else if (!ratioSatisfied) {
this.handleThresholdExiting(spanielEntry, state);
}
} else if (!ratioSatisfied) {
this.handleThresholdExiting(spanielEntry, state);
}

state.lastEntry = entry;
state.lastSatisfied = ratioSatisfied;
});
this.flushQueuedEntries();
state.lastEntry = entry;
state.lastSatisfied = ratioSatisfied;
});
this.flushQueuedEntries();
}
}
}
disconnect() {
Expand Down

0 comments on commit 0d5a3f9

Please sign in to comment.