Skip to content

Commit 07dec32

Browse files
authored
fix(project): leak loop refreshing files (#1496)
- fix(project): missing phone check in wechat
1 parent 0ad70d3 commit 07dec32

File tree

8 files changed

+88
-68
lines changed

8 files changed

+88
-68
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This class is used to run some task() at some interval.
3+
*/
4+
export class Scheduler {
5+
private running = false;
6+
private timer = 0;
7+
8+
public constructor(
9+
public readonly task: () => void | Promise<void>,
10+
public readonly interval: number,
11+
) {}
12+
13+
public start(): void {
14+
this.running = true;
15+
this.invoke();
16+
}
17+
18+
public invoke = async (): Promise<void> => {
19+
window.clearTimeout(this.timer);
20+
await this.task();
21+
if (this.running) {
22+
this.timer = window.setTimeout(this.invoke, this.interval);
23+
}
24+
};
25+
26+
public stop(): void {
27+
this.running = false;
28+
window.clearTimeout(this.timer);
29+
}
30+
}

desktop/renderer-app/src/pages/CloudStoragePage/store.tsx

+10-33
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { createResourcePreview, FileInfo } from "./CloudStorageFilePreview";
3636
import { getFileExt, isPPTX } from "../../utils/file";
3737
import { ConvertStatusManager } from "./ConvertStatusManager";
3838
import { queryH5ConvertingStatus } from "../../api-middleware/h5-converting";
39+
import { Scheduler } from "./scheduler";
3940

4041
export type CloudStorageFile = CloudStorageFileUI &
4142
Pick<CloudFile, "fileURL" | "taskUUID" | "taskToken" | "region" | "external">;
@@ -60,6 +61,8 @@ export class CloudStorageStore extends CloudStorageStoreBase {
6061

6162
private i18n: i18n;
6263

64+
private scheduler: Scheduler;
65+
6366
public constructor({
6467
compact,
6568
insertCourseware,
@@ -74,6 +77,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
7477
this.insertCourseware = insertCourseware;
7578
this.compact = compact;
7679
this.i18n = i18n;
80+
this.scheduler = new Scheduler(this.refreshFiles, 10 * 1000);
7781

7882
makeObservable(this, {
7983
filesMap: observable,
@@ -343,7 +347,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
343347
}: { onCoursewareInserted?: () => void } = {}): () => void {
344348
this.onCoursewareInserted = onCoursewareInserted;
345349

346-
void this.refreshFiles();
350+
this.scheduler.start();
347351

348352
if (
349353
this.uploadTaskManager.pending.length <= 0 &&
@@ -356,34 +360,22 @@ export class CloudStorageStore extends CloudStorageStoreBase {
356360
() => this.uploadTaskManager.uploading.length,
357361
(currLen, prevLen) => {
358362
if (currLen < prevLen) {
359-
this.refreshFilesNowDebounced();
363+
console.log("[cloud storage]: start now refresh");
364+
this.scheduler.invoke();
360365
this.hasMoreFile = true;
361366
}
362367
},
363368
);
364369

365370
return () => {
366371
disposer();
367-
window.clearTimeout(this._refreshFilesTimeout);
368-
this._refreshFilesTimeout = NaN;
369-
this.clearRefreshFilesNowTimeout();
372+
this.scheduler.stop();
370373
this.convertStatusManager.cancelAllTasks();
371374
this.onCoursewareInserted = undefined;
372375
};
373376
}
374377

375-
private _refreshFilesTimeout = NaN;
376-
private _refreshFilesNowTimeout = NaN;
377-
378-
private clearRefreshFilesNowTimeout(): void {
379-
window.clearTimeout(this._refreshFilesNowTimeout);
380-
this._refreshFilesNowTimeout = NaN;
381-
}
382-
383-
private async refreshFiles(): Promise<void> {
384-
window.clearTimeout(this._refreshFilesTimeout);
385-
this.clearRefreshFilesNowTimeout();
386-
378+
private refreshFiles = async (): Promise<void> => {
387379
try {
388380
const { totalUsage, files: cloudFiles } = await listFiles({
389381
page: 1,
@@ -429,22 +421,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
429421
} catch (e) {
430422
errorTips(e);
431423
}
432-
433-
if (!this._refreshFilesNowTimeout) {
434-
this.refreshFilesDebounced(10 * 1000);
435-
}
436-
}
437-
438-
private refreshFilesDebounced(timeout = 500): void {
439-
window.clearTimeout(this._refreshFilesTimeout);
440-
this._refreshFilesTimeout = window.setTimeout(this.refreshFiles.bind(this), timeout);
441-
}
442-
443-
private refreshFilesNowDebounced(timeout = 800): void {
444-
this.clearRefreshFilesNowTimeout();
445-
console.log("[cloud storage]: start now refresh");
446-
this._refreshFilesNowTimeout = window.setTimeout(this.refreshFiles.bind(this), timeout);
447-
}
424+
};
448425

449426
private previewCourseware(file: CloudStorageFile): void {
450427
const fileInfo: FileInfo = {

desktop/renderer-app/src/pages/LoginPage/index.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,9 @@ export const LoginPage = observer(function LoginPage() {
8383
(userInfo: LoginProcessResult) => {
8484
globalStore.updateUserInfo(userInfo);
8585
setLoginResult_(userInfo);
86-
pushHistory(RouteNameType.HomePage);
86+
if (NEED_BINDING_PHONE ? userInfo.hasPhone : true) {
87+
pushHistory(RouteNameType.HomePage);
88+
}
8789
},
8890
[globalStore, pushHistory],
8991
);

desktop/renderer-app/src/stores/global-store.ts

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export class GlobalStore {
9090

9191
public logout = (): void => {
9292
this.userInfo = null;
93+
this.lastLoginCheck = null;
9394
};
9495

9596
public updateCheckNewVersionDate = (): void => {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* This class is used to run some task() at some interval.
3+
*/
4+
export class Scheduler {
5+
private running = false;
6+
private timer = 0;
7+
8+
public constructor(
9+
public readonly task: () => void | Promise<void>,
10+
public readonly interval: number,
11+
) {}
12+
13+
public start(): void {
14+
this.running = true;
15+
this.invoke();
16+
}
17+
18+
public invoke = async (): Promise<void> => {
19+
window.clearTimeout(this.timer);
20+
await this.task();
21+
if (this.running) {
22+
this.timer = window.setTimeout(this.invoke, this.interval);
23+
}
24+
};
25+
26+
public stop(): void {
27+
this.running = false;
28+
window.clearTimeout(this.timer);
29+
}
30+
}

web/flat-web/src/pages/CloudStoragePage/store.tsx

+10-33
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import { coursewarePreloader } from "../../utils/courseware-preloader";
3636
import { getUploadTaskManager } from "../../utils/upload-task-manager";
3737
import { UploadStatusType, UploadTask } from "../../utils/upload-task-manager/upload-task";
3838
import { ConvertStatusManager } from "./ConvertStatusManager";
39+
import { Scheduler } from "./scheduler";
3940

4041
export type CloudStorageFile = CloudStorageFileUI &
4142
Pick<CloudFile, "fileURL" | "taskUUID" | "taskToken" | "region" | "external">;
@@ -60,6 +61,8 @@ export class CloudStorageStore extends CloudStorageStoreBase {
6061

6162
private i18n: i18n;
6263

64+
private scheduler: Scheduler;
65+
6366
public constructor({
6467
compact,
6568
insertCourseware,
@@ -74,6 +77,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
7477
this.insertCourseware = insertCourseware;
7578
this.compact = compact;
7679
this.i18n = i18n;
80+
this.scheduler = new Scheduler(this.refreshFiles, 10 * 1000);
7781

7882
makeObservable(this, {
7983
filesMap: observable,
@@ -339,7 +343,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
339343
}: { onCoursewareInserted?: () => void } = {}): () => void {
340344
this.onCoursewareInserted = onCoursewareInserted;
341345

342-
void this.refreshFiles();
346+
this.scheduler.start();
343347

344348
if (
345349
this.uploadTaskManager.pending.length <= 0 &&
@@ -352,34 +356,22 @@ export class CloudStorageStore extends CloudStorageStoreBase {
352356
() => this.uploadTaskManager.uploading.length,
353357
(currLen, prevLen) => {
354358
if (currLen < prevLen) {
355-
this.refreshFilesNowDebounced();
359+
console.log("[cloud storage]: start now refresh");
360+
this.scheduler.invoke();
356361
this.hasMoreFile = true;
357362
}
358363
},
359364
);
360365

361366
return () => {
362367
disposer();
363-
window.clearTimeout(this._refreshFilesTimeout);
364-
this._refreshFilesTimeout = NaN;
365-
this.clearRefreshFilesNowTimeout();
368+
this.scheduler.stop();
366369
this.convertStatusManager.cancelAllTasks();
367370
this.onCoursewareInserted = undefined;
368371
};
369372
}
370373

371-
private _refreshFilesTimeout = NaN;
372-
private _refreshFilesNowTimeout = NaN;
373-
374-
private clearRefreshFilesNowTimeout(): void {
375-
window.clearTimeout(this._refreshFilesNowTimeout);
376-
this._refreshFilesNowTimeout = NaN;
377-
}
378-
379-
private async refreshFiles(): Promise<void> {
380-
window.clearTimeout(this._refreshFilesTimeout);
381-
this.clearRefreshFilesNowTimeout();
382-
374+
private refreshFiles = async (): Promise<void> => {
383375
try {
384376
const { totalUsage, files: cloudFiles } = await listFiles({
385377
page: 1,
@@ -425,22 +417,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
425417
} catch (e) {
426418
errorTips(e);
427419
}
428-
429-
if (!this._refreshFilesNowTimeout) {
430-
this.refreshFilesDebounced(10 * 1000);
431-
}
432-
}
433-
434-
private refreshFilesDebounced(timeout = 500): void {
435-
window.clearTimeout(this._refreshFilesTimeout);
436-
this._refreshFilesTimeout = window.setTimeout(this.refreshFiles.bind(this), timeout);
437-
}
438-
439-
private refreshFilesNowDebounced(timeout = 800): void {
440-
this.clearRefreshFilesNowTimeout();
441-
console.log("[cloud storage]: start now refresh");
442-
this._refreshFilesNowTimeout = window.setTimeout(this.refreshFiles.bind(this), timeout);
443-
}
420+
};
444421

445422
private previewCourseware(file: CloudStorageFile): void {
446423
const { fileURL, taskToken, taskUUID, region } = file;

web/flat-web/src/pages/LoginPage/index.tsx

+3-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ export const LoginPage = observer(function LoginPage() {
7070
(userInfo: LoginProcessResult) => {
7171
globalStore.updateUserInfo(userInfo);
7272
setLoginResult_(userInfo);
73-
pushHistory(RouteNameType.HomePage);
73+
if (NEED_BINDING_PHONE ? userInfo.hasPhone : true) {
74+
pushHistory(RouteNameType.HomePage);
75+
}
7476
},
7577
[globalStore, pushHistory],
7678
);

web/flat-web/src/stores/GlobalStore.ts

+1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ export class GlobalStore {
102102

103103
public logout = (): void => {
104104
this.userInfo = null;
105+
this.lastLoginCheck = null;
105106
};
106107

107108
public hideRecordHintTips = (): void => {

0 commit comments

Comments
 (0)