main
js 55 lines 1.75 KB
Raw
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 {EventPriority} from 'react-reconciler/src/ReactEventPriorities';
11
12 import {getEventPriority} from '../events/ReactDOMEventListener';
13 import {
14 NoEventPriority,
15 DefaultEventPriority,
16 } from 'react-reconciler/src/ReactEventPriorities';
17
18 import ReactDOMSharedInternals from 'shared/ReactDOMSharedInternals';
19
20 export function setCurrentUpdatePriority(
21 newPriority: EventPriority,
22 // Closure will consistently not inline this function when it has arity 1
23 // however when it has arity 2 even if the second arg is omitted at every
24 // callsite it seems to inline it even when the internal length of the function
25 // is much longer. I hope this is consistent enough to rely on across builds
26 IntentionallyUnusedArgument?: empty,
27 ): void {
28 ReactDOMSharedInternals.p /* currentUpdatePriority */ = newPriority;
29 }
30
31 export function getCurrentUpdatePriority(): EventPriority {
32 return ReactDOMSharedInternals.p; /* currentUpdatePriority */
33 }
34
35 export function resolveUpdatePriority(): EventPriority {
36 const updatePriority = ReactDOMSharedInternals.p; /* currentUpdatePriority */
37 if (updatePriority !== NoEventPriority) {
38 return updatePriority;
39 }
40 const currentEvent = window.event;
41 if (currentEvent === undefined) {
42 return DefaultEventPriority;
43 }
44 return getEventPriority(currentEvent.type);
45 }
46
47 export function runWithPriority<T>(priority: EventPriority, fn: () => T): T {
48 const previousPriority = getCurrentUpdatePriority();
49 try {
50 setCurrentUpdatePriority(priority);
51 return fn();
52 } finally {
53 setCurrentUpdatePriority(previousPriority);
54 }
55 }