Skip to content

Commit 2598d3f

Browse files
committed
schedule expriring to fire immediately if expiration is soon
1 parent a1f0755 commit 2598d3f

2 files changed

Lines changed: 22 additions & 4 deletions

File tree

‎src/AccessTokenEvents.js‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,24 +20,33 @@ export default class AccessTokenEvents {
2020
}
2121

2222
init(container) {
23+
Log.info("AccessTokenEvents.init");
2324
this.cancel();
2425

2526
// only register events if there's an access token where we care about expiration
2627
if (container.access_token) {
2728
let duration = container.expires_in;
29+
Log.info("access token present, remaining duration:", duration);
2830

29-
let expiring = duration - this._expiringNotificationTime;
30-
if (expiring > 0) {
31+
if (duration > 0) {
3132
// only register expiring if we still have time
33+
let expiring = duration - this._expiringNotificationTime;
34+
if (expiring <= 0){
35+
expiring = 1;
36+
}
37+
Log.info("registering expiring timer in:", expiring);
3238
this._expiring.init(expiring);
3339
}
34-
40+
3541
// always register expired. if it's negative, it will still fire
36-
this._expired.init(duration + 1);
42+
let expired = duration + 1;
43+
Log.info("registering expired timer in:", expired);
44+
this._expired.init(expired);
3745
}
3846
}
3947

4048
cancel() {
49+
Log.info("AccessTokenEvents.canceling timers");
4150
this._expiring.cancel();
4251
this._expired.cancel();
4352
}

‎test/unit/AccessTokenEvents.spec.js‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ describe("AccessTokenEvents", function () {
6666
expiredTimer.duration.should.equal(71);
6767
});
6868

69+
it("should immediately schedule expiring timer if expiration is soon", function () {
70+
subject.init({
71+
access_token:"token",
72+
expires_in : 10
73+
});
74+
75+
expiringTimer.duration.should.equal(1);
76+
});
77+
6978
it("should not initialize expiring timer if already expired", function () {
7079
subject.init({
7180
access_token:"token",

0 commit comments

Comments
 (0)