| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | * |
| 7 | * @flow |
| 8 | */ |
| 9 | |
| 10 | import type {Lane, Lanes} from './ReactFiberLane'; |
| 11 | |
| 12 | import { |
| 13 | NoLane, |
| 14 | SyncLane, |
| 15 | InputContinuousLane, |
| 16 | DefaultLane, |
| 17 | IdleLane, |
| 18 | getHighestPriorityLane, |
| 19 | includesNonIdleWork, |
| 20 | } from './ReactFiberLane'; |
| 21 | |
| 22 | export opaque type EventPriority = Lane; |
| 23 | |
| 24 | export const NoEventPriority: EventPriority = NoLane; |
| 25 | export const DiscreteEventPriority: EventPriority = SyncLane; |
| 26 | export const ContinuousEventPriority: EventPriority = InputContinuousLane; |
| 27 | export const DefaultEventPriority: EventPriority = DefaultLane; |
| 28 | export const IdleEventPriority: EventPriority = IdleLane; |
| 29 | |
| 30 | export function higherEventPriority( |
| 31 | a: EventPriority, |
| 32 | b: EventPriority, |
| 33 | ): EventPriority { |
| 34 | return a !== 0 && a < b ? a : b; |
| 35 | } |
| 36 | |
| 37 | export function lowerEventPriority( |
| 38 | a: EventPriority, |
| 39 | b: EventPriority, |
| 40 | ): EventPriority { |
| 41 | return a === 0 || a > b ? a : b; |
| 42 | } |
| 43 | |
| 44 | export function isHigherEventPriority( |
| 45 | a: EventPriority, |
| 46 | b: EventPriority, |
| 47 | ): boolean { |
| 48 | return a !== 0 && a < b; |
| 49 | } |
| 50 | |
| 51 | export function eventPriorityToLane(updatePriority: EventPriority): Lane { |
| 52 | return updatePriority; |
| 53 | } |
| 54 | |
| 55 | export function lanesToEventPriority(lanes: Lanes): EventPriority { |
| 56 | const lane = getHighestPriorityLane(lanes); |
| 57 | if (!isHigherEventPriority(DiscreteEventPriority, lane)) { |
| 58 | return DiscreteEventPriority; |
| 59 | } |
| 60 | if (!isHigherEventPriority(ContinuousEventPriority, lane)) { |
| 61 | return ContinuousEventPriority; |
| 62 | } |
| 63 | if (includesNonIdleWork(lane)) { |
| 64 | return DefaultEventPriority; |
| 65 | } |
| 66 | return IdleEventPriority; |
| 67 | } |