Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor createEventHandle signature #19174

Merged
merged 1 commit into from
Jul 7, 2020

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Jun 22, 2020

This PR refactors the ReactDOM.createEventHandle API so that the signature is simpler and more barebones. Previously, createEventHandle returned an object with the methods setListener and clear, which mimiced what useEvent does.

After utilizing this API internally, primarily behind useEvent, it makes little sense for createEventHandle to have this signature. It still makses sense for useEvent to have this signtuare, but it's not important that the core API works the same. All we really want this core API to do is add an event and remove and event – sort of like a better native addEventListener.

This change means we no longer return an object from createEventHandle, but rather a function that sets the listener. Upon using the setter, you get back an unsubscribe/clear listener function.

const setClick = ReactDOM.createEventHandle('click');
const clear = setClick(domNode, callback);
clear();

To show a more concrete example, previously you might use the API in this way:

const clickHandle = ReactDOM.createEventHandle('click');

function Button({onClick}) {
  const ref = useRef(null);
  useEffect(() => {
    clickHandle.setListener(ref.current, onClick);
  });

  return <button ref={ref} />
}

With the changes in this PR, the above would look like this instead:

const setClickListener = ReactDOM.createEventHandle('click');

function Button({onClick}) {
  const ref = useRef(null);
  useEffect(() => {
    return setClickListener(ref.current, onClick);
  }, [onClick]);

  return <button ref={ref} />
}

This change offers some benefits:

  • reduces the amount of code in ReactDOM
  • improves the runtime performance of using createEventHandle by not having as much overhead
  • avoids accidently leaking nodes that React doesn't manage, by having to explicitly clear down correctly
  • we don't need to garbage collect event listeners when unmount DOM host components

@facebook-github-bot facebook-github-bot added CLA Signed React Core Team Opened by a member of the React Core Team labels Jun 22, 2020
@sizebot
Copy link

sizebot commented Jun 22, 2020

Details of bundled changes.

Comparing: 97b96da...eff2510

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js 0.0% -0.0% 908.83 KB 908.83 KB 206.68 KB 206.67 KB NODE_DEV
ReactDOMForked-prod.js -0.2% -0.2% 390.79 KB 389.96 KB 72.89 KB 72.72 KB FB_WWW_PROD
react-dom-server.node.development.js 0.0% -0.0% 138.39 KB 138.39 KB 36.56 KB 36.56 KB NODE_DEV
react-dom.production.min.js 0.0% -0.0% 123.26 KB 123.26 KB 39.54 KB 39.54 KB NODE_PROD
ReactDOMForked-profiling.js -0.2% -0.2% 401.33 KB 400.49 KB 74.63 KB 74.47 KB FB_WWW_PROFILING
react-dom-server.browser.development.js 0.0% -0.0% 144.55 KB 144.55 KB 36.75 KB 36.75 KB UMD_DEV
react-dom-server.node.production.min.js 0.0% -0.0% 20.68 KB 20.68 KB 7.66 KB 7.66 KB NODE_PROD
react-dom-test-utils.production.min.js 0.0% -0.0% 10.35 KB 10.35 KB 4.07 KB 4.07 KB UMD_PROD
ReactDOMTesting-dev.js -0.2% -0.2% 949.77 KB 947.98 KB 212.94 KB 212.57 KB FB_WWW_DEV
react-dom-test-utils.development.js 0.0% -0.0% 51.15 KB 51.15 KB 14.9 KB 14.9 KB NODE_DEV
ReactDOMTesting-prod.js -0.2% -0.2% 396.7 KB 395.87 KB 75.19 KB 75.05 KB FB_WWW_PROD
react-dom-unstable-fizz.node.development.js 0.0% -0.1% 5.61 KB 5.61 KB 1.87 KB 1.86 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% -0.1% 10.2 KB 10.2 KB 3.99 KB 3.99 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% -0.2% 5.36 KB 5.36 KB 1.8 KB 1.8 KB UMD_DEV
react-dom-unstable-fizz.browser.development.js 0.0% -0.1% 4.87 KB 4.87 KB 1.71 KB 1.7 KB NODE_DEV
react-dom.development.js 0.0% 0.0% 954.63 KB 954.63 KB 209.14 KB 209.14 KB UMD_DEV
react-dom-unstable-fizz.browser.production.min.js 0.0% -0.2% 1.01 KB 1.01 KB 617 B 616 B NODE_PROD
react-dom.production.min.js 0.0% -0.0% 123.1 KB 123.1 KB 40.23 KB 40.23 KB UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 127.05 KB 127.05 KB 41.49 KB 41.49 KB UMD_PROFILING
ReactDOMForked-dev.js -0.2% -0.2% 970.47 KB 968.69 KB 216.94 KB 216.55 KB FB_WWW_DEV
react-dom.profiling.min.js 0.0% -0.0% 127.38 KB 127.38 KB 40.76 KB 40.76 KB NODE_PROFILING
react-dom-server.browser.production.min.js 0.0% -0.0% 20.34 KB 20.34 KB 7.52 KB 7.52 KB UMD_PROD
ReactDOM-dev.js -0.2% -0.2% 976.96 KB 975.18 KB 218.13 KB 217.75 KB FB_WWW_DEV
ReactDOM-prod.js -0.2% -0.2% 391 KB 390.16 KB 72.9 KB 72.73 KB FB_WWW_PROD
react-dom-server.browser.development.js 0.0% -0.0% 137.12 KB 137.12 KB 36.31 KB 36.3 KB NODE_DEV
ReactDOM-profiling.js -0.2% -0.2% 401.53 KB 400.69 KB 74.64 KB 74.48 KB FB_WWW_PROFILING
react-dom-server.browser.production.min.js 0.0% -0.0% 20.26 KB 20.26 KB 7.51 KB 7.51 KB NODE_PROD
ReactDOMServer-dev.js 0.0% -0.0% 142.62 KB 142.62 KB 36.25 KB 36.25 KB FB_WWW_DEV
ReactDOMServer-prod.js 0.0% -0.0% 46.58 KB 46.58 KB 10.9 KB 10.9 KB FB_WWW_PROD
react-dom-test-utils.development.js 0.0% -0.0% 55.53 KB 55.53 KB 15.37 KB 15.37 KB UMD_DEV

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactART-prod.js -0.0% -0.0% 240.49 KB 240.44 KB 42.49 KB 42.48 KB FB_WWW_PROD
react-art.development.js 0.0% -0.0% 689.13 KB 689.13 KB 146.1 KB 146.1 KB UMD_DEV
react-art.development.js 0.0% -0.0% 590.61 KB 590.61 KB 128.31 KB 128.31 KB NODE_DEV
react-art.production.min.js 0.0% -0.0% 77.42 KB 77.42 KB 23.93 KB 23.93 KB NODE_PROD
ReactART-dev.js -0.1% -0.0% 619.1 KB 618.78 KB 132.04 KB 131.99 KB FB_WWW_DEV

ReactDOM: size: 0.0%, gzip: -0.0%

Size changes (experimental)

Generated by 🚫 dangerJS against eff2510

@sizebot
Copy link

sizebot commented Jun 22, 2020

Details of bundled changes.

Comparing: 97b96da...eff2510

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.development.js 0.0% -0.0% 876.52 KB 876.52 KB 200.86 KB 200.86 KB NODE_DEV
ReactDOMForked-prod.js -0.2% -0.2% 401.98 KB 401.14 KB 74.41 KB 74.25 KB FB_WWW_PROD
ReactDOMForked-profiling.js -0.2% -0.2% 412.57 KB 411.74 KB 76.18 KB 76.02 KB FB_WWW_PROFILING
react-dom-server.node.production.min.js 0.0% -0.0% 20.22 KB 20.22 KB 7.59 KB 7.58 KB NODE_PROD
react-dom-test-utils.production.min.js 0.0% -0.0% 10.34 KB 10.34 KB 4.06 KB 4.06 KB UMD_PROD
ReactDOMTesting-dev.js -0.2% -0.2% 977.8 KB 976.02 KB 218.83 KB 218.45 KB FB_WWW_DEV
react-dom-test-utils.development.js 0.0% -0.0% 51.13 KB 51.13 KB 14.89 KB 14.89 KB NODE_DEV
ReactDOMTesting-prod.js -0.2% -0.2% 409.95 KB 409.12 KB 77.1 KB 76.95 KB FB_WWW_PROD
react-dom-test-utils.production.min.js 0.0% -0.1% 10.19 KB 10.19 KB 3.98 KB 3.98 KB NODE_PROD
react-dom.development.js 0.0% -0.0% 920.89 KB 920.89 KB 203.23 KB 203.23 KB UMD_DEV
react-dom.production.min.js 0.0% -0.0% 118.44 KB 118.44 KB 38.82 KB 38.82 KB UMD_PROD
react-dom.profiling.min.js 0.0% -0.0% 122.34 KB 122.34 KB 40.02 KB 40.01 KB UMD_PROFILING
ReactDOMForked-dev.js -0.2% -0.2% 996.02 KB 994.23 KB 222.16 KB 221.79 KB FB_WWW_DEV
react-dom-server.browser.production.min.js 0.0% 0.0% 19.88 KB 19.88 KB 7.44 KB 7.44 KB UMD_PROD
ReactDOM-dev.js -0.2% -0.2% 1002.51 KB 1000.73 KB 223.37 KB 223 KB FB_WWW_DEV
ReactDOM-prod.js -0.2% -0.2% 402.18 KB 401.34 KB 74.43 KB 74.27 KB FB_WWW_PROD
ReactDOM-profiling.js -0.2% -0.2% 412.78 KB 411.94 KB 76.2 KB 76.04 KB FB_WWW_PROFILING
ReactDOMServer-prod.js 0.0% -0.0% 47.44 KB 47.44 KB 11.11 KB 11.11 KB FB_WWW_PROD
react-dom-test-utils.development.js 0.0% -0.0% 55.52 KB 55.52 KB 15.36 KB 15.36 KB UMD_DEV

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactART-prod.js -0.0% -0.0% 247.63 KB 247.58 KB 43.78 KB 43.77 KB FB_WWW_PROD
react-art.development.js 0.0% -0.0% 666.97 KB 666.97 KB 142.03 KB 142.02 KB UMD_DEV
react-art.production.min.js 0.0% -0.0% 109.57 KB 109.57 KB 33.93 KB 33.93 KB UMD_PROD
react-art.development.js 0.0% -0.0% 569.36 KB 569.36 KB 124.24 KB 124.24 KB NODE_DEV
react-art.production.min.js 0.0% -0.0% 74.53 KB 74.53 KB 23.06 KB 23.05 KB NODE_PROD
ReactART-dev.js -0.1% -0.0% 629.11 KB 628.79 KB 134.08 KB 134.02 KB FB_WWW_DEV

ReactDOM: size: 0.0%, gzip: -0.0%

Size changes (stable)

Generated by 🚫 dangerJS against eff2510

@trueadm trueadm force-pushed the revise-create-event-handle branch 3 times, most recently from fa15c98 to 4125918 Compare June 22, 2020 15:15
@codesandbox-ci
Copy link

codesandbox-ci bot commented Jun 22, 2020

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit eff2510:

Sandbox Source
React Configuration

Copy link
Contributor

@bvaughn bvaughn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a simplification overall which is nice. Questions about the one-listener-per-target+type constraint though.

@trueadm trueadm force-pushed the revise-create-event-handle branch from 68af22c to be0c79a Compare July 6, 2020 12:49
Delete target

Delete target

Add back codes

Revise
@trueadm trueadm force-pushed the revise-create-event-handle branch from be0c79a to eff2510 Compare July 7, 2020 11:42
@trueadm trueadm merged commit 4eb9b1d into facebook:master Jul 7, 2020
@trueadm trueadm deleted the revise-create-event-handle branch July 7, 2020 12:05
if (targetListeners !== null) {
targetListeners.delete(listener);
}
const registeredReactDOMEvents = new PossiblyWeakSet();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this mean we leak memory on IE11? It looks like we always add targets to this Set, but it's never cleared (since it's assumed to be a WeakSet in modern browsers).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in #19686.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed React Core Team Opened by a member of the React Core Team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants