Skip to content

Commit

Permalink
Use current time to calculate expiration time
Browse files Browse the repository at this point in the history
  • Loading branch information
acdlite committed Aug 11, 2017
1 parent 8db47ad commit 8972ae8
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/renderers/shared/fiber/ReactFiberExpirationTime.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const Sync = 1;
const Task = 2;
const Never = Infinity;

const UNIT_SIZE = 10;
const MAGIC_NUMBER_OFFSET = 10;

exports.Done = Done;
Expand All @@ -40,16 +41,27 @@ exports.Never = Infinity;
// 1 unit of expiration time represents 10ms.
function msToExpirationTime(ms: number): ExpirationTime {
// Always add an offset so that we don't clash with the magic number for Done.
return Math.round(ms / 10) + MAGIC_NUMBER_OFFSET;
return Math.round(ms / UNIT_SIZE) + MAGIC_NUMBER_OFFSET;
}
exports.msToExpirationTime = msToExpirationTime;

function expirationTimeToMs(expirationTime: ExpirationTime): number {
return (expirationTime - MAGIC_NUMBER_OFFSET) * 10;
return (expirationTime - MAGIC_NUMBER_OFFSET) * UNIT_SIZE;
}

function ceiling(time: ExpirationTime, precision: number): ExpirationTime {
return Math.ceil(Math.ceil(time * precision) / precision);
function ceiling(num: number, precision: number): number {
return Math.ceil(Math.ceil(num * precision) / precision);
}

function bucket(
currentTime: ExpirationTime,
expirationInMs: number,
precisionInMs: number,
): ExpirationTime {
return ceiling(
currentTime + expirationInMs / UNIT_SIZE,
precisionInMs / UNIT_SIZE,
);
}

// Given the current clock time and a priority level, returns an expiration time
Expand All @@ -68,12 +80,14 @@ function priorityToExpirationTime(
return Sync;
case TaskPriority:
return Task;
case HighPriority:
case HighPriority: {
// Should complete within ~100ms. 120ms max.
return msToExpirationTime(ceiling(100, 20));
case LowPriority:
return bucket(currentTime, 100, 20);
}
case LowPriority: {
// Should complete within ~1000ms. 1200ms max.
return msToExpirationTime(ceiling(1000, 200));
return bucket(currentTime, 1000, 200);
}
case OffscreenPriority:
return Never;
default:
Expand Down

0 comments on commit 8972ae8

Please sign in to comment.