forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathReactFiberExpirationTime.js
72 lines (63 loc) · 1.9 KB
/
ReactFiberExpirationTime.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* Copyright (c) 2013-present, Facebook, Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @providesModule ReactFiberExpirationTime
* @flow
*/
'use strict';
// TODO: Use an opaque type once ESLint et al support the syntax
export type ExpirationTime = number;
const NoWork = 0;
const Sync = 1;
const Task = 2;
const Never = 2147483647; // Max int32: Math.pow(2, 31) - 1
const UNIT_SIZE = 10;
const MAGIC_NUMBER_OFFSET = 3;
exports.Sync = Sync;
exports.Task = Task;
exports.NoWork = NoWork;
exports.Never = Never;
// 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 NoWork.
return ((ms / UNIT_SIZE) | 0) + MAGIC_NUMBER_OFFSET;
}
exports.msToExpirationTime = msToExpirationTime;
function ceiling(num: number, precision: number): number {
return (((num / precision) | 0) + 1) * precision;
}
function computeExpirationBucket(
currentTime: ExpirationTime,
expirationInMs: number,
bucketSizeMs: number,
): ExpirationTime {
return ceiling(
currentTime + expirationInMs / UNIT_SIZE,
bucketSizeMs / UNIT_SIZE,
);
}
exports.computeExpirationBucket = computeExpirationBucket;
// Given the current clock time and an expiration time, returns the
// relative expiration time. Possible values include NoWork, Sync, Task, and
// Never. All other values represent an async expiration time.
function relativeExpirationTime(
currentTime: ExpirationTime,
expirationTime: ExpirationTime,
): ExpirationTime {
switch (expirationTime) {
case NoWork:
case Sync:
case Task:
case Never:
return expirationTime;
}
const delta = expirationTime - currentTime;
if (delta <= 0) {
return Task;
}
return msToExpirationTime(delta);
}
exports.relativeExpirationTime = relativeExpirationTime;