Skip to content

Commit

Permalink
fix(project): leak loop refreshing files (#1496)
Browse files Browse the repository at this point in the history
- fix(project): missing phone check in wechat
  • Loading branch information
hyrious authored Apr 22, 2022
1 parent 0ad70d3 commit 07dec32
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 68 deletions.
30 changes: 30 additions & 0 deletions desktop/renderer-app/src/pages/CloudStoragePage/scheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* This class is used to run some task() at some interval.
*/
export class Scheduler {
private running = false;
private timer = 0;

public constructor(
public readonly task: () => void | Promise<void>,
public readonly interval: number,
) {}

public start(): void {
this.running = true;
this.invoke();
}

public invoke = async (): Promise<void> => {
window.clearTimeout(this.timer);
await this.task();
if (this.running) {
this.timer = window.setTimeout(this.invoke, this.interval);
}
};

public stop(): void {
this.running = false;
window.clearTimeout(this.timer);
}
}
43 changes: 10 additions & 33 deletions desktop/renderer-app/src/pages/CloudStoragePage/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { createResourcePreview, FileInfo } from "./CloudStorageFilePreview";
import { getFileExt, isPPTX } from "../../utils/file";
import { ConvertStatusManager } from "./ConvertStatusManager";
import { queryH5ConvertingStatus } from "../../api-middleware/h5-converting";
import { Scheduler } from "./scheduler";

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

private i18n: i18n;

private scheduler: Scheduler;

public constructor({
compact,
insertCourseware,
Expand All @@ -74,6 +77,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
this.insertCourseware = insertCourseware;
this.compact = compact;
this.i18n = i18n;
this.scheduler = new Scheduler(this.refreshFiles, 10 * 1000);

makeObservable(this, {
filesMap: observable,
Expand Down Expand Up @@ -343,7 +347,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
}: { onCoursewareInserted?: () => void } = {}): () => void {
this.onCoursewareInserted = onCoursewareInserted;

void this.refreshFiles();
this.scheduler.start();

if (
this.uploadTaskManager.pending.length <= 0 &&
Expand All @@ -356,34 +360,22 @@ export class CloudStorageStore extends CloudStorageStoreBase {
() => this.uploadTaskManager.uploading.length,
(currLen, prevLen) => {
if (currLen < prevLen) {
this.refreshFilesNowDebounced();
console.log("[cloud storage]: start now refresh");
this.scheduler.invoke();
this.hasMoreFile = true;
}
},
);

return () => {
disposer();
window.clearTimeout(this._refreshFilesTimeout);
this._refreshFilesTimeout = NaN;
this.clearRefreshFilesNowTimeout();
this.scheduler.stop();
this.convertStatusManager.cancelAllTasks();
this.onCoursewareInserted = undefined;
};
}

private _refreshFilesTimeout = NaN;
private _refreshFilesNowTimeout = NaN;

private clearRefreshFilesNowTimeout(): void {
window.clearTimeout(this._refreshFilesNowTimeout);
this._refreshFilesNowTimeout = NaN;
}

private async refreshFiles(): Promise<void> {
window.clearTimeout(this._refreshFilesTimeout);
this.clearRefreshFilesNowTimeout();

private refreshFiles = async (): Promise<void> => {
try {
const { totalUsage, files: cloudFiles } = await listFiles({
page: 1,
Expand Down Expand Up @@ -429,22 +421,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
} catch (e) {
errorTips(e);
}

if (!this._refreshFilesNowTimeout) {
this.refreshFilesDebounced(10 * 1000);
}
}

private refreshFilesDebounced(timeout = 500): void {
window.clearTimeout(this._refreshFilesTimeout);
this._refreshFilesTimeout = window.setTimeout(this.refreshFiles.bind(this), timeout);
}

private refreshFilesNowDebounced(timeout = 800): void {
this.clearRefreshFilesNowTimeout();
console.log("[cloud storage]: start now refresh");
this._refreshFilesNowTimeout = window.setTimeout(this.refreshFiles.bind(this), timeout);
}
};

private previewCourseware(file: CloudStorageFile): void {
const fileInfo: FileInfo = {
Expand Down
4 changes: 3 additions & 1 deletion desktop/renderer-app/src/pages/LoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ export const LoginPage = observer(function LoginPage() {
(userInfo: LoginProcessResult) => {
globalStore.updateUserInfo(userInfo);
setLoginResult_(userInfo);
pushHistory(RouteNameType.HomePage);
if (NEED_BINDING_PHONE ? userInfo.hasPhone : true) {
pushHistory(RouteNameType.HomePage);
}
},
[globalStore, pushHistory],
);
Expand Down
1 change: 1 addition & 0 deletions desktop/renderer-app/src/stores/global-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export class GlobalStore {

public logout = (): void => {
this.userInfo = null;
this.lastLoginCheck = null;
};

public updateCheckNewVersionDate = (): void => {
Expand Down
30 changes: 30 additions & 0 deletions web/flat-web/src/pages/CloudStoragePage/scheduler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* This class is used to run some task() at some interval.
*/
export class Scheduler {
private running = false;
private timer = 0;

public constructor(
public readonly task: () => void | Promise<void>,
public readonly interval: number,
) {}

public start(): void {
this.running = true;
this.invoke();
}

public invoke = async (): Promise<void> => {
window.clearTimeout(this.timer);
await this.task();
if (this.running) {
this.timer = window.setTimeout(this.invoke, this.interval);
}
};

public stop(): void {
this.running = false;
window.clearTimeout(this.timer);
}
}
43 changes: 10 additions & 33 deletions web/flat-web/src/pages/CloudStoragePage/store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import { coursewarePreloader } from "../../utils/courseware-preloader";
import { getUploadTaskManager } from "../../utils/upload-task-manager";
import { UploadStatusType, UploadTask } from "../../utils/upload-task-manager/upload-task";
import { ConvertStatusManager } from "./ConvertStatusManager";
import { Scheduler } from "./scheduler";

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

private i18n: i18n;

private scheduler: Scheduler;

public constructor({
compact,
insertCourseware,
Expand All @@ -74,6 +77,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
this.insertCourseware = insertCourseware;
this.compact = compact;
this.i18n = i18n;
this.scheduler = new Scheduler(this.refreshFiles, 10 * 1000);

makeObservable(this, {
filesMap: observable,
Expand Down Expand Up @@ -339,7 +343,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
}: { onCoursewareInserted?: () => void } = {}): () => void {
this.onCoursewareInserted = onCoursewareInserted;

void this.refreshFiles();
this.scheduler.start();

if (
this.uploadTaskManager.pending.length <= 0 &&
Expand All @@ -352,34 +356,22 @@ export class CloudStorageStore extends CloudStorageStoreBase {
() => this.uploadTaskManager.uploading.length,
(currLen, prevLen) => {
if (currLen < prevLen) {
this.refreshFilesNowDebounced();
console.log("[cloud storage]: start now refresh");
this.scheduler.invoke();
this.hasMoreFile = true;
}
},
);

return () => {
disposer();
window.clearTimeout(this._refreshFilesTimeout);
this._refreshFilesTimeout = NaN;
this.clearRefreshFilesNowTimeout();
this.scheduler.stop();
this.convertStatusManager.cancelAllTasks();
this.onCoursewareInserted = undefined;
};
}

private _refreshFilesTimeout = NaN;
private _refreshFilesNowTimeout = NaN;

private clearRefreshFilesNowTimeout(): void {
window.clearTimeout(this._refreshFilesNowTimeout);
this._refreshFilesNowTimeout = NaN;
}

private async refreshFiles(): Promise<void> {
window.clearTimeout(this._refreshFilesTimeout);
this.clearRefreshFilesNowTimeout();

private refreshFiles = async (): Promise<void> => {
try {
const { totalUsage, files: cloudFiles } = await listFiles({
page: 1,
Expand Down Expand Up @@ -425,22 +417,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
} catch (e) {
errorTips(e);
}

if (!this._refreshFilesNowTimeout) {
this.refreshFilesDebounced(10 * 1000);
}
}

private refreshFilesDebounced(timeout = 500): void {
window.clearTimeout(this._refreshFilesTimeout);
this._refreshFilesTimeout = window.setTimeout(this.refreshFiles.bind(this), timeout);
}

private refreshFilesNowDebounced(timeout = 800): void {
this.clearRefreshFilesNowTimeout();
console.log("[cloud storage]: start now refresh");
this._refreshFilesNowTimeout = window.setTimeout(this.refreshFiles.bind(this), timeout);
}
};

private previewCourseware(file: CloudStorageFile): void {
const { fileURL, taskToken, taskUUID, region } = file;
Expand Down
4 changes: 3 additions & 1 deletion web/flat-web/src/pages/LoginPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ export const LoginPage = observer(function LoginPage() {
(userInfo: LoginProcessResult) => {
globalStore.updateUserInfo(userInfo);
setLoginResult_(userInfo);
pushHistory(RouteNameType.HomePage);
if (NEED_BINDING_PHONE ? userInfo.hasPhone : true) {
pushHistory(RouteNameType.HomePage);
}
},
[globalStore, pushHistory],
);
Expand Down
1 change: 1 addition & 0 deletions web/flat-web/src/stores/GlobalStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class GlobalStore {

public logout = (): void => {
this.userInfo = null;
this.lastLoginCheck = null;
};

public hideRecordHintTips = (): void => {
Expand Down

0 comments on commit 07dec32

Please sign in to comment.