-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add DeployReload class to check whether system needs to be updated
- Loading branch information
Showing
10 changed files
with
146 additions
and
8 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
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
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,3 @@ | ||
{ | ||
"buildTime": 17053021998210 | ||
} |
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
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
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,75 @@ | ||
/** | ||
* fetchUrl: 请求的地址 | ||
* fetchKey: 获取的字段 | ||
* checkTimeout: 多久检查一次 | ||
* execute: 是否立即执行 | ||
* reloadCallback: 更新执行回调 | ||
*/ | ||
interface DeployReloadOptions { | ||
fetchUrl: string; | ||
fetchKey: string; | ||
checkTimeout?: number; | ||
execute?: boolean; | ||
reloadCallback?: () => void; | ||
} | ||
|
||
export class DeployReload { | ||
private fetchUrl: string; | ||
private fetchKey: string; | ||
private stashBuildTime: number = 0; | ||
private checkTimeout: number; // Second | ||
private execute: boolean = false; | ||
private intervalInstance: any; | ||
|
||
private reloadCallback: () => void; | ||
|
||
constructor(options: DeployReloadOptions) { | ||
this.fetchUrl = options.fetchUrl; | ||
this.fetchKey = options.fetchKey; | ||
this.checkTimeout = options.checkTimeout || 5; | ||
this.execute = options.execute || false; | ||
|
||
this.reloadCallback = options.reloadCallback || (() => {}); | ||
|
||
this.init(); | ||
} | ||
|
||
private init() { | ||
this.getBuildTimeValue().then((value) => { | ||
this.stashBuildTime = value; | ||
}); | ||
|
||
const timeoutValue = this.checkTimeout * 1000; | ||
const callback = this.checkBuildTime.bind(this); | ||
this.execute && callback(); | ||
this.intervalInstance = setInterval(callback, timeoutValue); | ||
|
||
document.addEventListener('visibilitychange', () => { | ||
if (document.visibilityState === 'visible') { | ||
console.log('22222'); | ||
this.execute && callback(); | ||
|
||
this.intervalInstance = setInterval(callback, timeoutValue); | ||
} else { | ||
clearInterval(this.intervalInstance); | ||
} | ||
}); | ||
} | ||
|
||
private async getBuildTimeValue() { | ||
const response = await fetch(`${this.fetchUrl}?time=${new Date().getTime()}`); | ||
const result = (await response.json()) as Record<string, any>; | ||
return result[this.fetchKey] as number; | ||
} | ||
|
||
private async checkBuildTime() { | ||
const currentBuildTime = await this.getBuildTimeValue(); | ||
if (currentBuildTime > this.stashBuildTime) { | ||
this.reload(); | ||
} | ||
} | ||
|
||
private reload() { | ||
this.reloadCallback(); | ||
} | ||
} |
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