@@ -36,6 +36,7 @@ import { coursewarePreloader } from "../../utils/courseware-preloader";
36
36
import { getUploadTaskManager } from "../../utils/upload-task-manager" ;
37
37
import { UploadStatusType , UploadTask } from "../../utils/upload-task-manager/upload-task" ;
38
38
import { ConvertStatusManager } from "./ConvertStatusManager" ;
39
+ import { Scheduler } from "./scheduler" ;
39
40
40
41
export type CloudStorageFile = CloudStorageFileUI &
41
42
Pick < CloudFile , "fileURL" | "taskUUID" | "taskToken" | "region" | "external" > ;
@@ -60,6 +61,8 @@ export class CloudStorageStore extends CloudStorageStoreBase {
60
61
61
62
private i18n : i18n ;
62
63
64
+ private scheduler : Scheduler ;
65
+
63
66
public constructor ( {
64
67
compact,
65
68
insertCourseware,
@@ -74,6 +77,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
74
77
this . insertCourseware = insertCourseware ;
75
78
this . compact = compact ;
76
79
this . i18n = i18n ;
80
+ this . scheduler = new Scheduler ( this . refreshFiles , 10 * 1000 ) ;
77
81
78
82
makeObservable ( this , {
79
83
filesMap : observable ,
@@ -339,7 +343,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
339
343
} : { onCoursewareInserted ?: ( ) => void } = { } ) : ( ) => void {
340
344
this . onCoursewareInserted = onCoursewareInserted ;
341
345
342
- void this . refreshFiles ( ) ;
346
+ this . scheduler . start ( ) ;
343
347
344
348
if (
345
349
this . uploadTaskManager . pending . length <= 0 &&
@@ -352,34 +356,22 @@ export class CloudStorageStore extends CloudStorageStoreBase {
352
356
( ) => this . uploadTaskManager . uploading . length ,
353
357
( currLen , prevLen ) => {
354
358
if ( currLen < prevLen ) {
355
- this . refreshFilesNowDebounced ( ) ;
359
+ console . log ( "[cloud storage]: start now refresh" ) ;
360
+ this . scheduler . invoke ( ) ;
356
361
this . hasMoreFile = true ;
357
362
}
358
363
} ,
359
364
) ;
360
365
361
366
return ( ) => {
362
367
disposer ( ) ;
363
- window . clearTimeout ( this . _refreshFilesTimeout ) ;
364
- this . _refreshFilesTimeout = NaN ;
365
- this . clearRefreshFilesNowTimeout ( ) ;
368
+ this . scheduler . stop ( ) ;
366
369
this . convertStatusManager . cancelAllTasks ( ) ;
367
370
this . onCoursewareInserted = undefined ;
368
371
} ;
369
372
}
370
373
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 > => {
383
375
try {
384
376
const { totalUsage, files : cloudFiles } = await listFiles ( {
385
377
page : 1 ,
@@ -425,22 +417,7 @@ export class CloudStorageStore extends CloudStorageStoreBase {
425
417
} catch ( e ) {
426
418
errorTips ( e ) ;
427
419
}
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
+ } ;
444
421
445
422
private previewCourseware ( file : CloudStorageFile ) : void {
446
423
const { fileURL, taskToken, taskUUID, region } = file ;
0 commit comments