|
| 1 | +/** |
| 2 | + * Copyright 2013-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + * |
| 9 | + * @providesModule ReactFiberExpirationTime |
| 10 | + * @flow |
| 11 | + */ |
| 12 | + |
| 13 | +'use strict'; |
| 14 | + |
| 15 | +import type {PriorityLevel} from 'ReactPriorityLevel'; |
| 16 | +const { |
| 17 | + NoWork, |
| 18 | + SynchronousPriority, |
| 19 | + TaskPriority, |
| 20 | + HighPriority, |
| 21 | + LowPriority, |
| 22 | + OffscreenPriority, |
| 23 | +} = require('ReactPriorityLevel'); |
| 24 | + |
| 25 | +const invariant = require('fbjs/lib/invariant'); |
| 26 | + |
| 27 | +// TODO: Use an opaque type once ESLint et al support the syntax |
| 28 | +export type ExpirationTime = number; |
| 29 | + |
| 30 | +const Done = 0; |
| 31 | +exports.Done = Done; |
| 32 | + |
| 33 | +const Never = Infinity; |
| 34 | +exports.Never = Infinity; |
| 35 | + |
| 36 | +// 1 unit of expiration time represents 10ms. |
| 37 | +function msToExpirationTime(ms: number): ExpirationTime { |
| 38 | + // Always add 1 so that we don't clash with the magic number for Done. |
| 39 | + return Math.round(ms / 10) + 1; |
| 40 | +} |
| 41 | +exports.msToExpirationTime = msToExpirationTime; |
| 42 | + |
| 43 | +function expirationTimeToMs(expirationTime: ExpirationTime): number { |
| 44 | + return (expirationTime - 1) * 10; |
| 45 | +} |
| 46 | + |
| 47 | +function ceiling(time: ExpirationTime, precision: number): ExpirationTime { |
| 48 | + return Math.ceil(Math.ceil(time * precision) / precision); |
| 49 | +} |
| 50 | + |
| 51 | +// Given the current clock time and a priority level, returns an expiration time |
| 52 | +// that represents a point in the future by which some work should complete. |
| 53 | +// The lower the priority, the further out the expiration time. We use rounding |
| 54 | +// to batch like updates together. The further out the expiration time, the |
| 55 | +// more we want to batch, so we use a larger precision when rounding. |
| 56 | +function priorityToExpirationTime( |
| 57 | + currentTime: ExpirationTime, |
| 58 | + priorityLevel: PriorityLevel, |
| 59 | +): ExpirationTime { |
| 60 | + switch (priorityLevel) { |
| 61 | + case NoWork: |
| 62 | + return Done; |
| 63 | + case SynchronousPriority: |
| 64 | + // Return a number lower than the current time, but higher than Done. |
| 65 | + return 1; |
| 66 | + case TaskPriority: |
| 67 | + // Return the current time, so that this work completes in this batch. |
| 68 | + return currentTime; |
| 69 | + case HighPriority: |
| 70 | + // Should complete within ~100ms. 120ms max. |
| 71 | + return msToExpirationTime(ceiling(100, 20)); |
| 72 | + case LowPriority: |
| 73 | + // Should complete within ~1000ms. 1200ms max. |
| 74 | + return msToExpirationTime(ceiling(1000, 200)); |
| 75 | + case OffscreenPriority: |
| 76 | + return Never; |
| 77 | + default: |
| 78 | + invariant( |
| 79 | + false, |
| 80 | + 'Switch statement should be exhuastive. ' + |
| 81 | + 'This error is likely caused by a bug in React. Please file an issue.', |
| 82 | + ); |
| 83 | + } |
| 84 | +} |
| 85 | +exports.priorityToExpirationTime = priorityToExpirationTime; |
| 86 | + |
| 87 | +// Given the current clock time and an expiration time, returns the |
| 88 | +// corresponding priority level. The more time has advanced, the higher the |
| 89 | +// priority level. |
| 90 | +function expirationTimeToPriorityLevel( |
| 91 | + currentTime: ExpirationTime, |
| 92 | + expirationTime: ExpirationTime, |
| 93 | +): PriorityLevel { |
| 94 | + // First check for magic values |
| 95 | + if (expirationTime === Done) { |
| 96 | + return NoWork; |
| 97 | + } |
| 98 | + if (expirationTime === Never) { |
| 99 | + return OffscreenPriority; |
| 100 | + } |
| 101 | + if (expirationTime < currentTime) { |
| 102 | + return SynchronousPriority; |
| 103 | + } |
| 104 | + if (expirationTime === currentTime) { |
| 105 | + return TaskPriority; |
| 106 | + } |
| 107 | + // Keep this value in sync with priorityToExpirationTime. |
| 108 | + if (expirationTimeToMs(expirationTime) < 120) { |
| 109 | + return HighPriority; |
| 110 | + } |
| 111 | + return LowPriority; |
| 112 | +} |
| 113 | +exports.expirationTimeToPriorityLevel = expirationTimeToPriorityLevel; |
0 commit comments