Skip to content

Commit

Permalink
delay reinitializing session monitor when user is removed to avid rac…
Browse files Browse the repository at this point in the history
…e conditions
  • Loading branch information
brockallen committed Dec 2, 2019
1 parent 26a45b6 commit 0894a05
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/SessionMonitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@

import { Log } from './Log.js';
import { CheckSessionIFrame } from './CheckSessionIFrame.js';
import { Global } from './Global.js';

export class SessionMonitor {

constructor(userManager, CheckSessionIFrameCtor = CheckSessionIFrame) {
constructor(userManager, CheckSessionIFrameCtor = CheckSessionIFrame, timer = Global.timer) {
if (!userManager) {
Log.error("SessionMonitor.ctor: No user manager passed to SessionMonitor");
throw new Error("userManager");
}

this._userManager = userManager;
this._CheckSessionIFrameCtor = CheckSessionIFrameCtor;
this._timer = timer;

this._userManager.events.addUserLoaded(this._start.bind(this));
this._userManager.events.addUserUnloaded(this._stop.bind(this));
Expand Down Expand Up @@ -117,22 +119,28 @@ export class SessionMonitor {
}

if (this._settings.monitorAnonymousSession) {
this._userManager.querySessionStatus().then(session => {
let tmpUser = {
session_state : session.session_state
};
if (session.sub && session.sid) {
tmpUser.profile = {
sub: session.sub,
sid: session.sid
// using a timer to delay re-initialization to avoid race conditions during signout
let timerHandle = this._timer.setInterval(()=>{
this._timer.clearInterval(timerHandle);

this._userManager.querySessionStatus().then(session => {
let tmpUser = {
session_state : session.session_state
};
}
this._start(tmpUser);
})
.catch(err => {
// catch to suppress errors since we're in a callback
Log.error("SessionMonitor: error from querySessionStatus:", err.message);
});
if (session.sub && session.sid) {
tmpUser.profile = {
sub: session.sub,
sid: session.sid
};
}
this._start(tmpUser);
})
.catch(err => {
// catch to suppress errors since we're in a callback
Log.error("SessionMonitor: error from querySessionStatus:", err.message);
});

}, 1000);
}
}

Expand Down

0 comments on commit 0894a05

Please sign in to comment.