Skip to content

Commit

Permalink
Rename UseEvent too
Browse files Browse the repository at this point in the history
  • Loading branch information
sebmarkbage committed Dec 13, 2022
1 parent a1466f0 commit 2d97961
Show file tree
Hide file tree
Showing 16 changed files with 55 additions and 52 deletions.
7 changes: 5 additions & 2 deletions packages/eslint-plugin-react-hooks/src/ExhaustiveDeps.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ export default {
if (name === 'useRef' && id.type === 'Identifier') {
// useRef() return value is stable.
return true;
} else if (isUseEventIdentifier(callee) && id.type === 'Identifier') {
} else if (
isUseEffectEventIdentifier(callee) &&
id.type === 'Identifier'
) {
for (const ref of resolved.references) {
if (ref !== id) {
useEffectEventVariables.add(ref.identifier);
Expand Down Expand Up @@ -1851,7 +1854,7 @@ function isAncestorNodeOf(a, b) {
return a.range[0] <= b.range[0] && a.range[1] >= b.range[1];
}

function isUseEventIdentifier(node) {
function isUseEffectEventIdentifier(node) {
if (__EXPERIMENTAL__) {
return node.type === 'Identifier' && node.name === 'useEffectEvent';
}
Expand Down
12 changes: 6 additions & 6 deletions packages/eslint-plugin-react-hooks/src/RulesOfHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function isInsideComponentOrHook(node) {
return false;
}

function isUseEventIdentifier(node) {
function isUseEffectEventIdentifier(node) {
if (__EXPERIMENTAL__) {
return node.type === 'Identifier' && node.name === 'useEffectEvent';
}
Expand Down Expand Up @@ -135,15 +135,15 @@ export default {
// For a given scope, iterate through the references and add all useEffectEvent definitions. We can
// do this in non-Program nodes because we can rely on the assumption that useEffectEvent functions
// can only be declared within a component or hook at its top level.
function recordAllUseEventFunctions(scope) {
function recordAllUseEffectEventFunctions(scope) {
for (const reference of scope.references) {
const parent = reference.identifier.parent;
if (
parent.type === 'VariableDeclarator' &&
parent.init &&
parent.init.type === 'CallExpression' &&
parent.init.callee &&
isUseEventIdentifier(parent.init.callee)
isUseEffectEventIdentifier(parent.init.callee)
) {
for (const ref of reference.resolved.references) {
if (ref !== reference) {
Expand Down Expand Up @@ -576,7 +576,7 @@ export default {
if (
node.callee.type === 'Identifier' &&
(node.callee.name === 'useEffect' ||
isUseEventIdentifier(node.callee)) &&
isUseEffectEventIdentifier(node.callee)) &&
node.arguments.length > 0
) {
// Denote that we have traversed into a useEffect call, and stash the CallExpr for
Expand Down Expand Up @@ -613,14 +613,14 @@ export default {
FunctionDeclaration(node) {
// function MyComponent() { const onClick = useEffectEvent(...) }
if (isInsideComponentOrHook(node)) {
recordAllUseEventFunctions(context.getScope());
recordAllUseEffectEventFunctions(context.getScope());
}
},

ArrowFunctionExpression(node) {
// const MyComponent = () => { const onClick = useEffectEvent(...) }
if (isInsideComponentOrHook(node)) {
recordAllUseEventFunctions(context.getScope());
recordAllUseEffectEventFunctions(context.getScope());
}
},
};
Expand Down
6 changes: 3 additions & 3 deletions packages/react-dom/src/__tests__/ReactDOMFizzServer-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5460,7 +5460,7 @@ describe('ReactDOMFizzServer', () => {
});

describe('useEffectEvent', () => {
// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('can server render a component with useEffectEvent', async () => {
const ref = React.createRef();
function App() {
Expand Down Expand Up @@ -5491,7 +5491,7 @@ describe('ReactDOMFizzServer', () => {
expect(getVisibleChildren(container)).toEqual(<button>1</button>);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('throws if useEffectEvent is called during a server render', async () => {
const logs = [];
function App() {
Expand Down Expand Up @@ -5523,7 +5523,7 @@ describe('ReactDOMFizzServer', () => {
expect(reportedServerErrors).toEqual([caughtError]);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('does not guarantee useEffectEvent return values during server rendering are distinct', async () => {
function App() {
const onClick1 = React.experimental_useEffectEvent(() => {});
Expand Down
8 changes: 4 additions & 4 deletions packages/react-reconciler/src/ReactFiberCommitWork.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ import {
enableUpdaterTracking,
enableCache,
enableTransitionTracing,
enableUseEventHook,
enableUseEffectEventHook,
enableFloat,
enableLegacyHidden,
enableHostSingletons,
Expand Down Expand Up @@ -454,9 +454,9 @@ function commitBeforeMutationEffectsOnFiber(finishedWork: Fiber) {

switch (finishedWork.tag) {
case FunctionComponent: {
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
if ((flags & Update) !== NoFlags) {
commitUseEventMount(finishedWork);
commitUseEffectEventMount(finishedWork);
}
}
break;
Expand Down Expand Up @@ -706,7 +706,7 @@ function commitHookEffectListMount(flags: HookFlags, finishedWork: Fiber) {
}
}

function commitUseEventMount(finishedWork: Fiber) {
function commitUseEffectEventMount(finishedWork: Fiber) {
const updateQueue: FunctionComponentUpdateQueue | null = (finishedWork.updateQueue: any);
const eventPayloads = updateQueue !== null ? updateQueue.events : null;
if (eventPayloads !== null) {
Expand Down
24 changes: 12 additions & 12 deletions packages/react-reconciler/src/ReactFiberHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
enableTransitionTracing,
enableUseHook,
enableUseMemoCacheHook,
enableUseEventHook,
enableUseEffectEventHook,
enableLegacyCache,
debugRenderPhaseSideEffectsForStrictMode,
} from 'shared/ReactFeatureFlags';
Expand Down Expand Up @@ -2780,7 +2780,7 @@ if (enableUseHook) {
if (enableUseMemoCacheHook) {
(ContextOnlyDispatcher: Dispatcher).useMemoCache = throwInvalidHookError;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(ContextOnlyDispatcher: Dispatcher).useEffectEvent = throwInvalidHookError;
}

Expand Down Expand Up @@ -2814,7 +2814,7 @@ if (enableUseHook) {
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMount: Dispatcher).useMemoCache = useMemoCache;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(HooksDispatcherOnMount: Dispatcher).useEffectEvent = mountEvent;
}
const HooksDispatcherOnUpdate: Dispatcher = {
Expand Down Expand Up @@ -2846,7 +2846,7 @@ if (enableUseMemoCacheHook) {
if (enableUseHook) {
(HooksDispatcherOnUpdate: Dispatcher).use = use;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(HooksDispatcherOnUpdate: Dispatcher).useEffectEvent = updateEvent;
}

Expand Down Expand Up @@ -2879,7 +2879,7 @@ if (enableUseHook) {
if (enableUseMemoCacheHook) {
(HooksDispatcherOnRerender: Dispatcher).useMemoCache = useMemoCache;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(HooksDispatcherOnRerender: Dispatcher).useEffectEvent = updateEvent;
}

Expand Down Expand Up @@ -3059,7 +3059,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMountInDEV: Dispatcher).useMemoCache = useMemoCache;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(HooksDispatcherOnMountInDEV: Dispatcher).useEffectEvent = function useEffectEvent<
Args,
Return,
Expand Down Expand Up @@ -3214,7 +3214,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useMemoCache = useMemoCache;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(HooksDispatcherOnMountWithHookTypesInDEV: Dispatcher).useEffectEvent = function useEffectEvent<
Args,
Return,
Expand Down Expand Up @@ -3369,7 +3369,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(HooksDispatcherOnUpdateInDEV: Dispatcher).useMemoCache = useMemoCache;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(HooksDispatcherOnUpdateInDEV: Dispatcher).useEffectEvent = function useEffectEvent<
Args,
Return,
Expand Down Expand Up @@ -3525,7 +3525,7 @@ if (__DEV__) {
if (enableUseMemoCacheHook) {
(HooksDispatcherOnRerenderInDEV: Dispatcher).useMemoCache = useMemoCache;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(HooksDispatcherOnRerenderInDEV: Dispatcher).useEffectEvent = function useEffectEvent<
Args,
Return,
Expand Down Expand Up @@ -3707,7 +3707,7 @@ if (__DEV__) {
return useMemoCache(size);
};
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(InvalidNestedHooksDispatcherOnMountInDEV: Dispatcher).useEffectEvent = function useEffectEvent<
Args,
Return,
Expand Down Expand Up @@ -3890,7 +3890,7 @@ if (__DEV__) {
return useMemoCache(size);
};
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(InvalidNestedHooksDispatcherOnUpdateInDEV: Dispatcher).useEffectEvent = function useEffectEvent<
Args,
Return,
Expand Down Expand Up @@ -4074,7 +4074,7 @@ if (__DEV__) {
return useMemoCache(size);
};
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
(InvalidNestedHooksDispatcherOnRerenderInDEV: Dispatcher).useEffectEvent = function useEffectEvent<
Args,
Return,
Expand Down
24 changes: 12 additions & 12 deletions packages/react-reconciler/src/__tests__/useEffectEvent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('useEffectEvent', () => {
return <span prop={props.text} />;
}

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('memoizes basic case correctly', () => {
class IncrementButton extends React.PureComponent {
increment = () => {
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('useEffectEvent', () => {
]);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('can be defined more than once', () => {
class IncrementButton extends React.PureComponent {
increment = () => {
Expand Down Expand Up @@ -173,7 +173,7 @@ describe('useEffectEvent', () => {
]);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('does not preserve `this` in event functions', () => {
class GreetButton extends React.PureComponent {
greet = () => {
Expand Down Expand Up @@ -222,7 +222,7 @@ describe('useEffectEvent', () => {
]);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('throws when called in render', () => {
class IncrementButton extends React.PureComponent {
increment = () => {
Expand Down Expand Up @@ -259,7 +259,7 @@ describe('useEffectEvent', () => {
expect(Scheduler).toHaveYielded(['Count: 0', 'Count: 0']);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it("useLayoutEffect shouldn't re-fire when event handlers change", () => {
class IncrementButton extends React.PureComponent {
increment = () => {
Expand Down Expand Up @@ -349,7 +349,7 @@ describe('useEffectEvent', () => {
]);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it("useEffect shouldn't re-fire when event handlers change", () => {
class IncrementButton extends React.PureComponent {
increment = () => {
Expand Down Expand Up @@ -438,7 +438,7 @@ describe('useEffectEvent', () => {
]);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('is stable in a custom hook', () => {
class IncrementButton extends React.PureComponent {
increment = () => {
Expand Down Expand Up @@ -533,7 +533,7 @@ describe('useEffectEvent', () => {
]);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('is mutated before all other effects', () => {
function Counter({value}) {
useInsertionEffect(() => {
Expand All @@ -557,7 +557,7 @@ describe('useEffectEvent', () => {
expect(Scheduler).toHaveYielded(['Effect value: 2', 'Event value: 2']);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it("doesn't provide a stable identity", () => {
function Counter({shouldRender, value}) {
const onClick = useEffectEvent(() => {
Expand Down Expand Up @@ -596,7 +596,7 @@ describe('useEffectEvent', () => {
]);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('event handlers always see the latest committed value', async () => {
let committedEventHandler = null;

Expand Down Expand Up @@ -646,7 +646,7 @@ describe('useEffectEvent', () => {
expect(committedEventHandler()).toBe('Value seen by useEffectEvent: 2');
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('integration: implements docs chat room example', () => {
function createConnection() {
let connectedCallback;
Expand Down Expand Up @@ -735,7 +735,7 @@ describe('useEffectEvent', () => {
expect(Scheduler).toHaveYielded(['Connected! theme: dark']);
});

// @gate enableUseEventHook
// @gate enableUseEffectEventHook
it('integration: implements the docs logVisit example', () => {
class AddToCartButton extends React.PureComponent {
addToCart = () => {
Expand Down
8 changes: 4 additions & 4 deletions packages/react-server/src/ReactFizzHooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {makeId} from './ReactServerFormatConfig';
import {
enableCache,
enableUseHook,
enableUseEventHook,
enableUseEffectEventHook,
enableUseMemoCacheHook,
} from 'shared/ReactFeatureFlags';
import is from 'shared/objectIs';
Expand Down Expand Up @@ -507,7 +507,7 @@ export function useCallback<T>(
return useMemo(() => callback, deps);
}

function throwOnUseEventCall() {
function throwOnUseEffectEventCall() {
throw new Error(
"A function wrapped in useEffectEvent can't be called during rendering.",
);
Expand All @@ -517,7 +517,7 @@ export function useEffectEvent<Args, Return, F: (...Array<Args>) => Return>(
callback: F,
): F {
// $FlowIgnore[incompatible-return]
return throwOnUseEventCall;
return throwOnUseEffectEventCall;
}

// TODO Decide on how to implement this hook for server rendering.
Expand Down Expand Up @@ -651,7 +651,7 @@ export const HooksDispatcher: Dispatcher = {
if (enableCache) {
HooksDispatcher.useCacheRefresh = useCacheRefresh;
}
if (enableUseEventHook) {
if (enableUseEffectEventHook) {
HooksDispatcher.useEffectEvent = useEffectEvent;
}
if (enableUseMemoCacheHook) {
Expand Down
2 changes: 1 addition & 1 deletion packages/shared/ReactFeatureFlags.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ export const enableUseHook = true;
// auto-memoization.
export const enableUseMemoCacheHook = __EXPERIMENTAL__;

export const enableUseEventHook = __EXPERIMENTAL__;
export const enableUseEffectEventHook = __EXPERIMENTAL__;

// Test in www before enabling in open source.
// Enables DOM-server to stream its instruction set as data-attributes
Expand Down
Loading

0 comments on commit 2d97961

Please sign in to comment.