|
1 |
| -import * as React from 'react'; |
| 1 | +import type { Dispatch } from 'react'; |
2 | 2 |
|
3 | 3 | export const initialState = {
|
4 | 4 | fetching: false,
|
@@ -65,27 +65,31 @@ export const hasDepsChanged = <T extends { length: number }>(a: T, b: T) => {
|
65 | 65 | return false;
|
66 | 66 | };
|
67 | 67 |
|
68 |
| -const ReactSharedInternals = |
69 |
| - (React as any).__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED || |
70 |
| - (React as any) |
71 |
| - .__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE; |
| 68 | +let isDispatching = false; |
72 | 69 |
|
73 |
| -export function deferDispatch<Dispatch extends React.Dispatch<any>>( |
74 |
| - setState: Dispatch, |
75 |
| - value: Dispatch extends React.Dispatch<infer State> ? State : void |
76 |
| -) { |
77 |
| - if (!!ReactSharedInternals && process.env.NODE_ENV !== 'production') { |
78 |
| - const currentOwner = ReactSharedInternals.ReactCurrentOwner |
79 |
| - ? ReactSharedInternals.ReactCurrentOwner.current |
80 |
| - : ReactSharedInternals.A && |
81 |
| - ReactSharedInternals.A.getOwner && |
82 |
| - ReactSharedInternals.A.getOwner(); |
83 |
| - if (currentOwner) { |
84 |
| - Promise.resolve(value).then(setState); |
85 |
| - } else { |
86 |
| - setState(value); |
| 70 | +function deferDispatch<F extends Dispatch<any>>( |
| 71 | + setState: F, |
| 72 | + value: F extends Dispatch<infer State> ? State : void |
| 73 | +): void; |
| 74 | + |
| 75 | +function deferDispatch<F extends Dispatch<any>>( |
| 76 | + setState: F |
| 77 | +): ReturnType<F>; |
| 78 | + |
| 79 | +function deferDispatch<F extends Dispatch<any>>( |
| 80 | + setState: F, |
| 81 | + value?: F extends Dispatch<infer State> ? State : void |
| 82 | +): any { |
| 83 | + if (!isDispatching || value === undefined) { |
| 84 | + try { |
| 85 | + isDispatching = true; |
| 86 | + return setState(value); |
| 87 | + } finally { |
| 88 | + isDispatching = false; |
87 | 89 | }
|
88 | 90 | } else {
|
89 |
| - setState(value); |
| 91 | + Promise.resolve(value).then(setState); |
90 | 92 | }
|
91 | 93 | }
|
| 94 | + |
| 95 | +export { deferDispatch }; |
0 commit comments