Skip to content

Commit a02abfe

Browse files
committed
💄
1 parent a9b5a0f commit a02abfe

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/common/cache.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ export class CacheHelper {
2222
if (!obj) {
2323
obj = {};
2424
}
25-
const exp = expiration ? ((+ new Date()) / 1000 + expiration) : undefined;
25+
const exp = expiration ? (Date.now() / 1000 + expiration) : undefined;
2626
obj[key] = { value, expiration: exp };
2727
return this.context.globalState.update(CACHE_KEY, obj);
2828
}
2929

30-
get(key: string) {
30+
get<T>(key: string): T | undefined {
3131
const value = this.context.globalState.get<CacheMap>(CACHE_KEY);
3232
if (!value || !value[key]) {
3333
return undefined;
@@ -36,17 +36,17 @@ export class CacheHelper {
3636
if (!data.expiration) {
3737
return data.value;
3838
}
39-
const now = (+ new Date()) / 1000;
39+
const now = Date.now() / 1000;
4040
return now > data.expiration ? undefined : data.value;
4141
}
4242

43-
async handy<T>(key: string, cb: () => Thenable<{ value: T; ttl?: number }>) {
44-
let d = this.get(key);
45-
if (d === undefined) {
46-
const tmp = await cb();
47-
await this.set(key, tmp.value, tmp.ttl);
48-
d = tmp.value;
43+
async getOrRefresh<T>(key: string, refreshCallback: () => Thenable<{ value: T; ttl?: number }>): Promise<T> {
44+
let value = this.get<T>(key);
45+
if (value === undefined) {
46+
const result = await refreshCallback();
47+
await this.set(key, result.value, result.ttl);
48+
value = result.value;
4949
}
50-
return d as T;
50+
return value;
5151
}
5252
}

src/releaseNotes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ function getResponseCacheTime(resp: Response) {
5050

5151
async function getLastPublish(cacheHelper: CacheHelper) {
5252
const url = `${websiteHost}/changelog/latest`;
53-
return cacheHelper.handy(url, async () => {
53+
return cacheHelper.getOrRefresh(url, async () => {
5454
const resp = await fetch(url);
5555
if (!resp.ok) {
5656
throw new Error(`Getting latest releaseId failed: ${resp.statusText}`);
@@ -75,7 +75,7 @@ class ReleaseNotesPanel {
7575

7676
private async loadChangelog(releaseId: string) {
7777
const url = `${websiteHost}/changelog/raw-markdown?releaseId=${releaseId}`;
78-
const md = await this.cacheHelper.handy(url, async () => {
78+
const md = await this.cacheHelper.getOrRefresh(url, async () => {
7979
const resp = await fetch(url);
8080
if (!resp.ok) {
8181
throw new Error(`Getting raw markdown content failed: ${resp.statusText}`);

0 commit comments

Comments
 (0)