Skip to content

Commit 563cc19

Browse files
committed
don't cancel, then reregister timers when re-initializing timers if same expiration is needed.
1 parent 96533ab commit 563cc19

7 files changed

Lines changed: 147 additions & 88 deletions

File tree

dist/oidc-client.js

Lines changed: 40 additions & 24 deletions
Large diffs are not rendered by default.

dist/oidc-client.min.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/oidc-client.js

Lines changed: 40 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/oidc-client.min.js

Lines changed: 15 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/AccessTokenEvents.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,39 +20,39 @@ export class AccessTokenEvents {
2020
}
2121

2222
load(container) {
23-
Log.debug("AccessTokenEvents.load");
24-
25-
this._cancelTimers();
26-
2723
// only register events if there's an access token and it has an expiration
2824
if (container.access_token && container.expires_in !== undefined) {
2925
let duration = container.expires_in;
30-
Log.debug("access token present, remaining duration:", duration);
26+
Log.debug("AccessTokenEvents.load: access token present, remaining duration:", duration);
3127

3228
if (duration > 0) {
3329
// only register expiring if we still have time
3430
let expiring = duration - this._accessTokenExpiringNotificationTime;
3531
if (expiring <= 0){
3632
expiring = 1;
3733
}
38-
Log.debug("registering expiring timer in:", expiring);
34+
35+
Log.debug("AccessTokenEvents.load: registering expiring timer in:", expiring);
3936
this._accessTokenExpiring.init(expiring);
4037
}
38+
else {
39+
Log.debug("AccessTokenEvents.load: canceling existing expiring timer becase we're past expiration.");
40+
this._accessTokenExpiring.cancel();
41+
}
4142

42-
// always register expired. if it's negative, it will still fire
43+
// if it's negative, it will still fire
4344
let expired = duration + 1;
44-
Log.debug("registering expired timer in:", expired);
45+
Log.debug("AccessTokenEvents.load: registering expired timer in:", expired);
4546
this._accessTokenExpired.init(expired);
4647
}
48+
else {
49+
this._accessTokenExpiring.cancel();
50+
this._accessTokenExpired.cancel();
51+
}
4752
}
4853

4954
unload() {
50-
Log.debug("AccessTokenEvents.unload");
51-
this._cancelTimers();
52-
}
53-
54-
_cancelTimers(){
55-
Log.debug("canceling existing access token timers");
55+
Log.debug("AccessTokenEvents.unload: canceling existing access token timers");
5656
this._accessTokenExpiring.cancel();
5757
this._accessTokenExpired.cancel();
5858
}

src/Timer.js

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,39 @@ const TimerDuration = 5; // seconds
99

1010
export class Timer extends Event {
1111

12-
constructor(name, timer = Global.timer) {
12+
constructor(name, timer = Global.timer, nowFunc = undefined) {
1313
super(name);
1414
this._timer = timer;
15-
this._nowFunc = () => Date.now() / 1000;
15+
16+
if (nowFunc) {
17+
this._nowFunc = nowFunc;
18+
}
19+
else {
20+
this._nowFunc = () => Date.now() / 1000;
21+
}
1622
}
1723

1824
get now() {
1925
return parseInt(this._nowFunc());
2026
}
2127

2228
init(duration) {
23-
this.cancel();
24-
2529
if (duration <= 0) {
2630
duration = 1;
2731
}
2832
duration = parseInt(duration);
2933

34+
var expiration = this.now + duration;
35+
if (this.expiration === expiration && this._timerHandle) {
36+
// no need to reinitialize to same expiration, so bail out
37+
Log.debug("Timer.init timer " + this._name + " skipping initialization since already initialized for expiration:", this.expiration);
38+
return;
39+
}
40+
41+
this.cancel();
42+
3043
Log.debug("Timer.init timer " + this._name + " for duration:", duration);
31-
this._expiration = this.now + duration;
44+
this._expiration = expiration;
3245

3346
// we're using a fairly short timer and then checking the expiration in the
3447
// callback to handle scenarios where the browser device sleeps, and then
@@ -39,6 +52,10 @@ export class Timer extends Event {
3952
}
4053
this._timerHandle = this._timer.setInterval(this._callback.bind(this), timerDuration * 1000);
4154
}
55+
56+
get expiration() {
57+
return this._expiration;
58+
}
4259

4360
cancel() {
4461
if (this._timerHandle) {
@@ -50,7 +67,7 @@ export class Timer extends Event {
5067

5168
_callback() {
5269
var diff = this._expiration - this.now;
53-
Log.debug("Timer._callback; " + this._name + " timer expires in:", diff);
70+
Log.debug("Timer.callback; " + this._name + " timer expires in:", diff);
5471

5572
if (this._expiration <= this.now) {
5673
this.cancel();

test/unit/Timer.spec.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ describe("Timer", function () {
2828

2929
let subject;
3030
let stubWindowTimer;
31+
let now = Date.now() / 1000;
3132

3233
beforeEach(function () {
3334
stubWindowTimer = new StubWindowTimer();
34-
subject = new Timer("test name", stubWindowTimer);
35+
subject = new Timer("test name", stubWindowTimer, () => now);
3536
});
3637

3738
describe("init", function () {
@@ -57,17 +58,26 @@ describe("Timer", function () {
5758
stubWindowTimer.duration.should.equal(3000);
5859
});
5960

60-
it("should cancel previous timer", function () {
61+
it("should cancel previous timer if new time is not the same", function () {
6162
subject.init(10);
6263
stubWindowTimer.clearTimeoutWasCalled.should.be.false;
6364

65+
now = now + 1;
6466
subject.init(10);
6567

6668
stubWindowTimer.clearTimeoutWasCalled.should.be.true;
6769
});
70+
71+
it("should not cancel previous timer if new time is same", function () {
72+
subject.init(10);
73+
stubWindowTimer.clearTimeoutWasCalled.should.be.false;
74+
75+
subject.init(10);
76+
stubWindowTimer.clearTimeoutWasCalled.should.be.false;
77+
});
6878
});
6979

70-
describe("_callback", function () {
80+
describe("_callback", function () {
7181

7282
it("should fire when timer expires", function () {
7383
var cb = function () {

0 commit comments

Comments
 (0)