Skip to content

Commit 0784e3d

Browse files
committed
changed timer to use short interval and check expiration manually DuendeArchive#143
1 parent 2c23aa4 commit 0784e3d

7 files changed

Lines changed: 236 additions & 83 deletions

File tree

dist/oidc-client.js

Lines changed: 50 additions & 19 deletions
Large diffs are not rendered by default.

dist/oidc-client.min.js

Lines changed: 18 additions & 18 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: 50 additions & 19 deletions
Large diffs are not rendered by default.

lib/oidc-client.min.js

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

src/Global.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33

44
const timer = {
5-
setTimeout: function (cb, duration) {
6-
return setTimeout(cb, duration);
5+
setInterval: function (cb, duration) {
6+
return setInterval(cb, duration);
77
},
8-
clearTimeout: function (handle) {
9-
return clearTimeout(handle);
8+
clearInterval: function (handle) {
9+
return clearInterval(handle);
1010
}
1111
};
1212

src/Timer.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@ import Log from './Log';
55
import Global from './Global';
66
import Event from './Event';
77

8+
const TimerDuration = 5; // 5 seconds
9+
810
export default class Timer extends Event {
911

1012
constructor(name, timer = Global.timer) {
1113
super(name);
1214
this._timer = timer;
15+
this._nowFunc = () => Date.now() / 1000;
16+
}
17+
18+
get now() {
19+
return parseInt(this._nowFunc());
1320
}
1421

1522
init(duration) {
@@ -18,19 +25,36 @@ export default class Timer extends Event {
1825
if (duration <= 0) {
1926
duration = 1;
2027
}
28+
duration = parseInt(duration);
29+
30+
Log.info("Timer.init timer " + this._name + " for duration:", duration);
31+
this._expiration = this.now + duration;
2132

22-
this._timerHandle = this._timer.setTimeout(this._callback.bind(this), duration * 1000);
33+
// we're using a fairly short timer and then checking the expiration in the
34+
// callback to handle scenarios where the browser device sleeps, and then
35+
// the timers end up getting delayed.
36+
var timerDuration = TimerDuration;
37+
if (duration < timerDuration) {
38+
timerDuration = duration;
39+
}
40+
this._timerHandle = this._timer.setInterval(this._callback.bind(this), timerDuration * 1000);
2341
}
2442

2543
cancel() {
2644
if (this._timerHandle) {
27-
this._timer.clearTimeout(this._timerHandle);
45+
Log.info("Timer.cancel: ", this._name);
46+
this._timer.clearInterval(this._timerHandle);
2847
this._timerHandle = null;
2948
}
3049
}
3150

3251
_callback() {
33-
this._timerHandle = null;
34-
super.raise();
52+
var diff = this._expiration - this.now;
53+
Log.info("Timer._callback; " + this._name + " timer expires in:", diff);
54+
55+
if (this._expiration <= this.now) {
56+
this.cancel();
57+
super.raise();
58+
}
3559
}
3660
}

test/unit/Timer.spec.js

Lines changed: 70 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ class StubWindowTimer {
1313
this.clearTimeoutWasCalled = false;
1414
}
1515

16-
setTimeout(cb, duration) {
16+
setInterval(cb, duration) {
1717
this.callback = cb;
1818
this.duration = duration;
1919
return 5;
2020
}
2121

22-
clearTimeout() {
22+
clearInterval() {
2323
this.clearTimeoutWasCalled = true;
2424
}
2525
}
@@ -39,7 +39,6 @@ describe("Timer", function () {
3939
it("should setup a timer", function () {
4040
subject.init(10);
4141
stubWindowTimer.callback.should.be.ok;
42-
stubWindowTimer.duration.should.equal(10000);
4342
});
4443

4544
it("should use 1 second if duration is too low", function () {
@@ -51,6 +50,13 @@ describe("Timer", function () {
5150
stubWindowTimer.duration.should.equal(1000);
5251
});
5352

53+
it("should use duration if less than default", function () {
54+
subject.init(2);
55+
stubWindowTimer.duration.should.equal(2000);
56+
subject.init(3);
57+
stubWindowTimer.duration.should.equal(3000);
58+
});
59+
5460
it("should cancel previous timer", function () {
5561
subject.init(10);
5662
stubWindowTimer.clearTimeoutWasCalled.should.be.false;
@@ -61,6 +67,58 @@ describe("Timer", function () {
6167
});
6268
});
6369

70+
describe("_callback", function () {
71+
72+
it("should fire when timer expires", function () {
73+
var cb = function () {
74+
cb.wasCalled = true;
75+
};
76+
cb.wasCalled = false;
77+
subject.addHandler(cb);
78+
79+
subject._nowFunc = () => 100;
80+
subject.init(10);
81+
82+
subject._nowFunc = () => 109;
83+
stubWindowTimer.callback();
84+
cb.wasCalled.should.be.false;
85+
86+
subject._nowFunc = () => 110;
87+
stubWindowTimer.callback();
88+
cb.wasCalled.should.be.true;
89+
});
90+
91+
92+
it("should fire if timer late", function () {
93+
var cb = function () {
94+
cb.wasCalled = true;
95+
};
96+
cb.wasCalled = false;
97+
subject.addHandler(cb);
98+
99+
subject._nowFunc = () => 100;
100+
subject.init(10);
101+
102+
subject._nowFunc = () => 109;
103+
stubWindowTimer.callback();
104+
cb.wasCalled.should.be.false;
105+
106+
subject._nowFunc = () => 111;
107+
stubWindowTimer.callback();
108+
cb.wasCalled.should.be.true;
109+
});
110+
111+
it("should cancel window timer", function () {
112+
subject._nowFunc = () => 100;
113+
subject.init(10);
114+
115+
subject._nowFunc = () => 110;
116+
stubWindowTimer.callback();
117+
118+
stubWindowTimer.clearTimeoutWasCalled.should.be.true;
119+
});
120+
});
121+
64122
describe("cancel", function () {
65123

66124
it("should cancel timer", function () {
@@ -87,7 +145,9 @@ describe("Timer", function () {
87145
};
88146
subject.addHandler(cb);
89147

148+
subject._nowFunc = () => 100;
90149
subject.init(10);
150+
subject._nowFunc = () => 110;
91151
stubWindowTimer.callback();
92152

93153
cb.wasCalled.should.be.true;
@@ -103,7 +163,9 @@ describe("Timer", function () {
103163
subject.addHandler(cb);
104164
subject.addHandler(cb);
105165

166+
subject._nowFunc = () => 100;
106167
subject.init(10);
168+
subject._nowFunc = () => 110;
107169
stubWindowTimer.callback();
108170

109171
count.should.equal(4);
@@ -119,9 +181,12 @@ describe("Timer", function () {
119181
};
120182
cb.wasCalled = false;
121183

184+
subject._nowFunc = () => 100;
122185
subject.addHandler(cb);
123186
subject.init(10);
124187
subject.removeHandler(cb);
188+
189+
subject._nowFunc = () => 110;
125190
stubWindowTimer.callback();
126191

127192
cb.wasCalled.should.be.false;
@@ -140,10 +205,12 @@ describe("Timer", function () {
140205
subject.addHandler(cb2);
141206
subject.addHandler(cb1);
142207

208+
subject._nowFunc = () => 100;
143209
subject.init(10);
144210
subject.removeHandler(cb1);
145211
subject.removeHandler(cb1);
146212

213+
subject._nowFunc = () => 110;
147214
stubWindowTimer.callback();
148215

149216
count.should.equal(0);

0 commit comments

Comments
 (0)