-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(browser): use worker timers to prevent unexpected client close (#…
- Loading branch information
1 parent
a50e85c
commit 35448f3
Showing
7 changed files
with
189 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { clearTimeout as clearT, setTimeout as setT } from 'worker-timers' | ||
import isBrowser from './is-browser' | ||
|
||
export default class PingTimer { | ||
private keepalive: number | ||
|
||
private timer: any | ||
|
||
private checkPing: () => void | ||
|
||
private setTimeout = isBrowser ? setT : setTimeout | ||
|
||
private clearTimeout = isBrowser ? clearT : clearTimeout | ||
|
||
constructor(keepalive: number, checkPing: () => void) { | ||
this.keepalive = keepalive * 1000 | ||
this.checkPing = checkPing | ||
this.setup() | ||
} | ||
|
||
private setup() { | ||
this.timer = this.setTimeout(() => { | ||
this.checkPing() | ||
this.reschedule() | ||
}, this.keepalive) | ||
} | ||
|
||
clear() { | ||
if (this.timer) { | ||
this.clearTimeout(this.timer) | ||
this.timer = null | ||
} | ||
} | ||
|
||
reschedule() { | ||
this.clear() | ||
this.setup() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.