@@ -32,6 +32,7 @@ const Sync = 1;
3232const Task = 2 ;
3333const Never = Infinity ;
3434
35+ const UNIT_SIZE = 10 ;
3536const MAGIC_NUMBER_OFFSET = 10 ;
3637
3738exports . Done = Done ;
@@ -40,16 +41,27 @@ exports.Never = Infinity;
4041// 1 unit of expiration time represents 10ms.
4142function msToExpirationTime ( ms : number ) : ExpirationTime {
4243 // Always add an offset so that we don't clash with the magic number for Done.
43- return Math . round ( ms / 10 ) + MAGIC_NUMBER_OFFSET ;
44+ return Math . round ( ms / UNIT_SIZE ) + MAGIC_NUMBER_OFFSET ;
4445}
4546exports . msToExpirationTime = msToExpirationTime ;
4647
4748function expirationTimeToMs ( expirationTime : ExpirationTime ) : number {
48- return ( expirationTime - MAGIC_NUMBER_OFFSET ) * 10 ;
49+ return ( expirationTime - MAGIC_NUMBER_OFFSET ) * UNIT_SIZE ;
4950}
5051
51- function ceiling ( time : ExpirationTime , precision : number ) : ExpirationTime {
52- return Math . ceil ( Math . ceil ( time * precision ) / precision ) ;
52+ function ceiling ( num : number , precision : number ) : number {
53+ return Math . ceil ( Math . ceil ( num * precision ) / precision ) ;
54+ }
55+
56+ function bucket (
57+ currentTime : ExpirationTime ,
58+ expirationInMs : number ,
59+ precisionInMs : number ,
60+ ) : ExpirationTime {
61+ return ceiling (
62+ currentTime + expirationInMs / UNIT_SIZE ,
63+ precisionInMs / UNIT_SIZE ,
64+ ) ;
5365}
5466
5567// Given the current clock time and a priority level, returns an expiration time
@@ -68,12 +80,14 @@ function priorityToExpirationTime(
6880 return Sync ;
6981 case TaskPriority :
7082 return Task ;
71- case HighPriority :
83+ case HighPriority : {
7284 // Should complete within ~100ms. 120ms max.
73- return msToExpirationTime ( ceiling ( 100 , 20 ) ) ;
74- case LowPriority :
85+ return bucket ( currentTime , 100 , 20 ) ;
86+ }
87+ case LowPriority : {
7588 // Should complete within ~1000ms. 1200ms max.
76- return msToExpirationTime ( ceiling ( 1000 , 200 ) ) ;
89+ return bucket ( currentTime , 1000 , 200 ) ;
90+ }
7791 case OffscreenPriority :
7892 return Never ;
7993 default :
0 commit comments