Skip to content

Commit 4f7d8ed

Browse files
committed
add basic timer and callback support
1 parent b22f08d commit 4f7d8ed

3 files changed

Lines changed: 305 additions & 0 deletions

File tree

‎src/Global.js‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,9 @@ export default class Global {
1414
static get XMLHttpRequest(){
1515
return XMLHttpRequest;
1616
}
17+
static get timer(){
18+
return {
19+
setTimeout, clearTimeout
20+
};
21+
}
1722
};

‎src/Timer.js‎

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
import Log from './Log';
5+
import Global from './Global';
6+
7+
export default class Timer {
8+
9+
constructor(timer = Global.timer) {
10+
this._timer = timer;
11+
this._callbacks = [];
12+
}
13+
14+
addHandler(cb) {
15+
this._callbacks.push(cb);
16+
}
17+
18+
removeHandler(cb) {
19+
var idx = this._callbacks.findIndex(item => item === cb);
20+
if (idx >= 0) {
21+
this._callbacks.splice(idx, 1);
22+
}
23+
}
24+
25+
init(duration) {
26+
this.cancel();
27+
28+
if (duration <= 0) {
29+
duration = 1;
30+
}
31+
32+
this._timerHandle = this._timer.setTimeout(this._callback.bind(this), duration * 1000);
33+
}
34+
35+
cancel() {
36+
if (this._timerHandle) {
37+
this._timer.clearTimeout(this._timerHandle);
38+
this._timerHandle = null;
39+
}
40+
}
41+
42+
_callback() {
43+
this._timerHandle = null;
44+
for (var cb of this._callbacks) {
45+
cb();
46+
}
47+
}
48+
}
49+
50+
/*
51+
52+
function configureTokenExpiring(mgr) {
53+
54+
function callback() {
55+
handle = null;
56+
mgr._callTokenExpiring();
57+
}
58+
59+
var handle = null;
60+
61+
function cancel() {
62+
if (handle) {
63+
window.clearTimeout(handle);
64+
handle = null;
65+
}
66+
}
67+
68+
function setup(duration) {
69+
handle = window.setTimeout(callback, duration * 1000);
70+
}
71+
72+
function configure() {
73+
cancel();
74+
75+
if (!mgr.expired) {
76+
var duration = mgr.expires_in;
77+
if (duration > 60) {
78+
setup(duration - 60);
79+
}
80+
else {
81+
callback();
82+
}
83+
}
84+
}
85+
86+
configure();
87+
88+
mgr.addOnTokenObtained(configure);
89+
mgr.addOnTokenRemoved(cancel);
90+
}
91+
92+
function configureAutoRenewToken(mgr) {
93+
94+
if (mgr._settings.silent_redirect_uri && mgr._settings.silent_renew) {
95+
96+
mgr.addOnTokenExpiring(function () {
97+
mgr.renewTokenSilentAsync().catch(function (e) {
98+
mgr._callSilentTokenRenewFailed();
99+
console.error(e && e.message || "Unknown error");
100+
});
101+
});
102+
103+
}
104+
}
105+
106+
function configureTokenExpired(mgr) {
107+
108+
function callback() {
109+
handle = null;
110+
111+
if (mgr._token) {
112+
mgr.saveToken(null);
113+
}
114+
115+
mgr._callTokenExpired();
116+
}
117+
118+
var handle = null;
119+
120+
function cancel() {
121+
if (handle) {
122+
window.clearTimeout(handle);
123+
handle = null;
124+
}
125+
}
126+
127+
function setup(duration) {
128+
handle = window.setTimeout(callback, duration * 1000);
129+
}
130+
131+
function configure() {
132+
cancel();
133+
if (mgr.expires_in > 0) {
134+
// register 1 second beyond expiration so we don't get into edge conditions for expiration
135+
setup(mgr.expires_in + 1);
136+
}
137+
}
138+
139+
configure();
140+
141+
mgr.addOnTokenObtained(configure);
142+
mgr.addOnTokenRemoved(cancel);
143+
}
144+
145+
*/

‎test/unit/Timer.spec.js‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3+
4+
import Timer from '../../src/Timer';
5+
6+
import chai from 'chai';
7+
chai.should();
8+
let assert = chai.assert;
9+
10+
class StubWindowTimer {
11+
12+
constructor() {
13+
this.clearTimeoutWasCalled = false;
14+
}
15+
16+
setTimeout(cb, duration) {
17+
this.callback = cb;
18+
this.duration = duration;
19+
return 5;
20+
}
21+
22+
clearTimeout() {
23+
this.clearTimeoutWasCalled = true;
24+
}
25+
}
26+
27+
describe("Timer", function () {
28+
29+
let subject;
30+
let stubWindowTimer;
31+
32+
beforeEach(function () {
33+
stubWindowTimer = new StubWindowTimer();
34+
subject = new Timer(stubWindowTimer);
35+
});
36+
37+
describe("init", function () {
38+
39+
it("should setup a timer", function () {
40+
subject.init(10);
41+
stubWindowTimer.callback.should.be.ok;
42+
stubWindowTimer.duration.should.equal(10000);
43+
});
44+
45+
it("should use 1 second if duration is too low", function () {
46+
subject.init(0);
47+
stubWindowTimer.duration.should.equal(1000);
48+
subject.init(-1);
49+
stubWindowTimer.duration.should.equal(1000);
50+
subject.init(-5);
51+
stubWindowTimer.duration.should.equal(1000);
52+
});
53+
54+
it("should cancel previous timer", function () {
55+
subject.init(10);
56+
stubWindowTimer.clearTimeoutWasCalled.should.be.false;
57+
58+
subject.init(10);
59+
60+
stubWindowTimer.clearTimeoutWasCalled.should.be.true;
61+
});
62+
});
63+
64+
describe("cancel", function () {
65+
66+
it("should cancel timer", function () {
67+
subject.init(10);
68+
stubWindowTimer.clearTimeoutWasCalled.should.be.false;
69+
70+
subject.cancel();
71+
72+
stubWindowTimer.clearTimeoutWasCalled.should.be.true;
73+
});
74+
75+
it("should do nothing if no existing timer", function () {
76+
subject.cancel();
77+
78+
stubWindowTimer.clearTimeoutWasCalled.should.be.false;
79+
});
80+
});
81+
82+
describe("addHandler", function () {
83+
84+
it("should allow callback to be invoked", function () {
85+
var cb = function () {
86+
cb.wasCalled = true;
87+
};
88+
subject.addHandler(cb);
89+
90+
subject.init(10);
91+
stubWindowTimer.callback();
92+
93+
cb.wasCalled.should.be.true;
94+
});
95+
96+
it("should allow multiple callbacks", function () {
97+
var count = 0;
98+
var cb = function () {
99+
count++;
100+
};
101+
subject.addHandler(cb);
102+
subject.addHandler(cb);
103+
subject.addHandler(cb);
104+
subject.addHandler(cb);
105+
106+
subject.init(10);
107+
stubWindowTimer.callback();
108+
109+
count.should.equal(4);
110+
});
111+
112+
});
113+
114+
describe("removeHandler", function () {
115+
116+
it("should remove callback from being invoked", function () {
117+
var cb = function () {
118+
cb.wasCalled = true;
119+
};
120+
cb.wasCalled = false;
121+
122+
subject.addHandler(cb);
123+
subject.init(10);
124+
subject.removeHandler(cb);
125+
stubWindowTimer.callback();
126+
127+
cb.wasCalled.should.be.false;
128+
});
129+
130+
it("should remove individual callback", function () {
131+
var count = 0;
132+
var cb1 = function () {
133+
count++;
134+
};
135+
var cb2 = function () {
136+
cb2.wasCalled = true;
137+
};
138+
139+
subject.addHandler(cb1);
140+
subject.addHandler(cb2);
141+
subject.addHandler(cb1);
142+
143+
subject.init(10);
144+
subject.removeHandler(cb1);
145+
subject.removeHandler(cb1);
146+
147+
stubWindowTimer.callback();
148+
149+
count.should.equal(0);
150+
cb2.wasCalled.should.be.true;
151+
});
152+
153+
});
154+
155+
});

0 commit comments

Comments
 (0)