Skip to content

Commit 02e6ee0

Browse files
authored
Merge pull request #40 from launchdarkly/ag/ch15372/fix-history-listeners
[ch15372] Make sure we attach history listeners once we receive some goals
2 parents fd6eeeb + c17f51f commit 02e6ee0

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

src/index.js

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -350,13 +350,33 @@ function initialize(env, user, options) {
350350
});
351351
}
352352

353+
function refreshGoalTracker() {
354+
if (goalTracker) {
355+
goalTracker.dispose();
356+
}
357+
if (goals && goals.length) {
358+
goalTracker = GoalTracker(goals, sendGoalEvent);
359+
}
360+
}
361+
362+
function attachHistoryListeners() {
363+
if (!!(window.history && history.pushState)) {
364+
window.removeEventListener('popstate',refreshGoalTracker);
365+
window.addEventListener('popstate', refreshGoalTracker);
366+
} else {
367+
window.removeEventListener('hashchange', refreshGoalTracker);
368+
window.addEventListener('hashchange', refreshGoalTracker);
369+
}
370+
}
371+
353372
requestor.fetchGoals(function(err, g) {
354373
if (err) {
355374
emitter.maybeReportError(new errors.LDUnexpectedResponseError('Error fetching goals: ' + err.message ? err.message : err));
356375
}
357376
if (g && g.length > 0) {
358377
goals = g;
359378
goalTracker = GoalTracker(goals, sendGoalEvent);
379+
attachHistoryListeners();
360380
}
361381
});
362382

@@ -379,23 +399,6 @@ function initialize(env, user, options) {
379399
events.flush(ident.getUser(), true);
380400
});
381401

382-
function refreshGoalTracker() {
383-
if (goalTracker) {
384-
goalTracker.dispose();
385-
}
386-
if (goals && goals.length) {
387-
goalTracker = GoalTracker(goals, sendGoalEvent);
388-
}
389-
}
390-
391-
if (goals && goals.length > 0) {
392-
if (!!(window.history && history.pushState)) {
393-
window.addEventListener('popstate', refreshGoalTracker);
394-
} else {
395-
window.addEventListener('hashchange', refreshGoalTracker);
396-
}
397-
}
398-
399402
window.addEventListener('message', handleMessage);
400403

401404
var readyPromise = new Promise(function(resolve) {

0 commit comments

Comments
 (0)