forked from authts/oidc-client-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.ts
More file actions
63 lines (52 loc) · 2.13 KB
/
Copy pathTimer.ts
File metadata and controls
63 lines (52 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
import { Event } from "./Event";
import { Logger } from "./Logger";
/**
* @internal
*/
export class Timer extends Event<[void]> {
protected readonly _logger = new Logger(`Timer('${this._name}')`);
private _timerHandle: ReturnType<typeof setInterval> | null = null;
private _expiration = 0;
// get the time
public static getEpochTime(): number {
return Math.floor(Date.now() / 1000);
}
public init(durationInSeconds: number): void {
const logger = this._logger.create("init");
durationInSeconds = Math.max(Math.floor(durationInSeconds), 1);
const expiration = Timer.getEpochTime() + durationInSeconds;
if (this.expiration === expiration && this._timerHandle) {
// no need to reinitialize to same expiration, so bail out
logger.debug("skipping since already initialized for expiration at", this.expiration);
return;
}
this.cancel();
logger.debug("using duration", durationInSeconds);
this._expiration = expiration;
// we're using a fairly short timer and then checking the expiration in the
// callback to handle scenarios where the browser device sleeps, and then
// the timers end up getting delayed.
const timerDurationInSeconds = Math.min(durationInSeconds, 5);
this._timerHandle = setInterval(this._callback, timerDurationInSeconds * 1000);
}
public get expiration(): number {
return this._expiration;
}
public cancel(): void {
this._logger.create("cancel");
if (this._timerHandle) {
clearInterval(this._timerHandle);
this._timerHandle = null;
}
}
protected _callback = (): void => {
const diff = this._expiration - Timer.getEpochTime();
this._logger.debug("timer completes in", diff);
if (this._expiration <= Timer.getEpochTime()) {
this.cancel();
super.raise();
}
};
}