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

[react-interactions] Add experimental ReactDOM Listener API #17508

Closed
wants to merge 1 commit into from

Conversation

trueadm
Copy link
Contributor

@trueadm trueadm commented Dec 3, 2019

This PR adds the experimental Listener API to React behind a flag. Note: For now this is only intended to be used internally at Facebook, and we have no initial plans to ship this to open source.

The listener API includes its own React event system that leverages the lessons learned from the existing event system and that of the experimental React Flare event system. There are three new APIs that are added to react-dom:

  • ReactDOM.unstable_createListener
  • ReactDOM.unstable_createRootListener
  • ReactDOM.unstable_addEventPriority

The Listener API works in a similar way to the DOM's addEventListener API, except this API returns a special React Listener object that can be supplied to elements via the internal listeners prop. An example of this follows:

import React from 'react';
import {unstable_createListener} from 'react-dom';

function ClickableButton({ text }) {
  const click = unstable_createListener('click', event => {
    console.log('You clicked the button, event');
  });

  return (
    <button listeners={click}>{text}</button>
  ); 
}

Listener Options

Both the createListener and createRootListener APIs support an optional third argument for specifying listener options. There are two boolean options that can be specified, capture and passive. Here's an example to demonstrate this:

function ClickableButton({ text }) {
  const options = {
    capture: true, // capture is `false` by default
    passive: true, // passive is `true` by default
  };
  const click = unstable_createListener('click', event => {
    console.log('You clicked the button, event');
  }, options);

  return (
    <button listeners={click}>{text}</button>
  ); 
}

Root Listeners

Sometimes, it's ideal to attach listeners to the root document of an app, so events can be captured that don't hit a specific target element. For these cases, the createRootListener API can be used:

import React from 'react';
import {unstable_createRootListener} from 'react-dom';

function ClickableButton({ text }) {
  const click = unstable_createRootListener('click', event => {
    console.log('You clicked the button, event');
  });

  return (
    <button listeners={click}>{text}</button>
  ); 
}

Combining multiple listeners

Elements support having multiple listeners, even of the same type on an element, to do this, arrays can be used (and nested) to supply many listeners. Listeners of the same type will execute in array order of how they're supplied to the element:

function ClickableButton({ text }) {
  const click = unstable_createListener('click', event => {
    ...
  });
  const click2 = unstable_createListener('click', event => {
    ...
  });

  return (
    <button listeners={[click, click2]}>{text}</button>
  ); 
}

Event Object

Unlike the existing React event system, the Listener API supplies only the native object to event callbacks – there are no Synthetic Events. The only caveat is that the native objects get monkey-patched by React, with several of the properties and methods on the event object replaced with alternatives that interface with React's event delegation and propagation system. Calling stopPropagation on the monkey-patched event object will not trigger the native stopPropagation method (unlike the existing React event system, which does), but rather prevent propagation through React's propagation system only.

Event Flushing

With the Listener API, we don't flush on the native event triggering unless we have React Listeners that are actually triggered, this should help reduce needlessly flushing work when the event has no actual action.

This PR depends on #17513

@codesandbox-ci
Copy link

codesandbox-ci bot commented Dec 3, 2019

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 e33346d:

Sandbox Source
young-mountain-sqkef Configuration

@sizebot
Copy link

sizebot commented Dec 3, 2019

Details of bundled changes.

Comparing: acfe4b2...e33346d

react-noop-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-noop-renderer.production.min.js 🔺+2.5% 🔺+1.0% 11.83 KB 12.13 KB 3.78 KB 3.81 KB NODE_PROD
react-noop-renderer-persistent.development.js +1.4% +0.9% 35.1 KB 35.58 KB 8.01 KB 8.09 KB NODE_DEV
react-noop-renderer-persistent.production.min.js 🔺+2.5% 🔺+1.1% 11.85 KB 12.15 KB 3.78 KB 3.82 KB NODE_PROD
react-noop-renderer-flight-server.production.min.js 0.0% 🔺+0.2% 994 B 994 B 556 B 557 B NODE_PROD
react-noop-renderer.development.js +1.4% +0.9% 34.98 KB 35.47 KB 8 KB 8.07 KB NODE_DEV
react-noop-renderer-server.development.js 0.0% +0.1% 1.97 KB 1.97 KB 897 B 898 B NODE_DEV
react-noop-renderer-server.production.min.js 0.0% 🔺+0.2% 888 B 888 B 502 B 503 B NODE_PROD
react-noop-renderer-flight-client.development.js 0.0% +0.4% 1.62 KB 1.62 KB 769 B 772 B NODE_DEV
react-noop-renderer-flight-client.production.min.js 0.0% 🔺+0.5% 561 B 561 B 412 B 414 B NODE_PROD

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom-server.browser.development.js +0.2% +0.1% 136.37 KB 136.67 KB 35.91 KB 35.96 KB NODE_DEV
react-dom.development.js +2.7% +2.0% 955.24 KB 980.57 KB 216.02 KB 220.42 KB UMD_DEV
react-dom-server.browser.production.min.js 0.0% 0.0% 19.85 KB 19.85 KB 7.38 KB 7.38 KB NODE_PROD
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.87 KB 3.87 KB 1.54 KB 1.54 KB UMD_DEV
react-dom.production.min.js 🔺+0.1% -0.0% 116.39 KB 116.46 KB 37.45 KB 37.44 KB UMD_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% 🔺+0.1% 1.2 KB 1.2 KB 701 B 702 B UMD_PROD
react-dom.profiling.min.js +0.1% -0.0% 119.96 KB 120.02 KB 38.56 KB 38.55 KB UMD_PROFILING
react-dom.development.js +2.7% +2.1% 949.3 KB 974.66 KB 214.44 KB 218.87 KB NODE_DEV
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.7 KB 3.7 KB 1.49 KB 1.49 KB NODE_DEV
react-dom.production.min.js 🔺+0.1% -0.1% 116.5 KB 116.57 KB 36.85 KB 36.84 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 60.13 KB 60.13 KB 15.79 KB 15.79 KB UMD_DEV
react-dom.profiling.min.js +0.1% -0.1% 120.19 KB 120.26 KB 37.84 KB 37.81 KB NODE_PROFILING
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 59.8 KB 59.8 KB 15.65 KB 15.66 KB NODE_DEV
react-dom-server.node.development.js +0.2% +0.1% 137.48 KB 137.78 KB 36.13 KB 36.18 KB NODE_DEV
react-dom-test-utils.development.js +0.1% +0.1% 56.2 KB 56.25 KB 15.55 KB 15.56 KB UMD_DEV
react-dom-server.node.production.min.js 0.0% 0.0% 20.26 KB 20.26 KB 7.53 KB 7.53 KB NODE_PROD
react-dom-test-utils.development.js +0.1% +0.1% 54.47 KB 54.53 KB 15.21 KB 15.22 KB NODE_DEV
react-dom-unstable-fizz.node.development.js 0.0% +0.1% 4.4 KB 4.4 KB 1.64 KB 1.64 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% 🔺+0.1% 10.94 KB 10.94 KB 4.08 KB 4.08 KB NODE_PROD
react-dom-server.browser.development.js +0.2% +0.1% 140.44 KB 140.74 KB 36.91 KB 36.96 KB UMD_DEV
react-dom-unstable-fizz.node.production.min.js 0.0% 🔺+0.1% 1.2 KB 1.2 KB 688 B 689 B NODE_PROD
react-dom-server.browser.production.min.js 0.0% 0.0% 19.93 KB 19.93 KB 7.38 KB 7.39 KB UMD_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler.production.min.js 🔺+0.1% 🔺+0.1% 72.57 KB 72.63 KB 21.32 KB 21.33 KB NODE_PROD
react-reconciler-reflection.production.min.js 0.0% 🔺+0.1% 2.85 KB 2.85 KB 1.23 KB 1.24 KB NODE_PROD
react-reconciler-persistent.development.js +1.8% +1.7% 603.21 KB 613.88 KB 127.04 KB 129.26 KB NODE_DEV
react-reconciler-persistent.production.min.js 🔺+0.1% 🔺+0.1% 72.58 KB 72.64 KB 21.32 KB 21.34 KB NODE_PROD
react-reconciler.development.js +1.8% +1.7% 605.74 KB 616.47 KB 128.16 KB 130.37 KB NODE_DEV
react-reconciler-reflection.development.js +0.3% +0.2% 19.08 KB 19.14 KB 6.23 KB 6.24 KB NODE_DEV

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-art.development.js +1.6% +1.5% 674.06 KB 684.86 KB 146.35 KB 148.57 KB UMD_DEV
react-art.production.min.js 🔺+0.1% 🔺+0.1% 104.79 KB 104.85 KB 31.83 KB 31.85 KB UMD_PROD
react-art.development.js +1.8% +1.7% 604.7 KB 615.51 KB 128.95 KB 131.15 KB NODE_DEV
react-art.production.min.js 🔺+0.1% 🔺+0.1% 69.8 KB 69.86 KB 21.01 KB 21.03 KB NODE_PROD

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +1.7% +1.6% 618.97 KB 629.38 KB 132.03 KB 134.15 KB UMD_DEV
react-test-renderer.production.min.js 🔺+0.1% 🔺+0.1% 71.82 KB 71.88 KB 21.93 KB 21.95 KB UMD_PROD
react-test-renderer.development.js +1.7% +1.6% 614.24 KB 624.65 KB 130.85 KB 132.99 KB NODE_DEV
react-test-renderer.production.min.js 🔺+0.1% 🔺+0.1% 71.52 KB 71.58 KB 21.57 KB 21.59 KB NODE_PROD
react-test-renderer-shallow.production.min.js 0.0% 🔺+0.1% 11.58 KB 11.58 KB 3.57 KB 3.58 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% 0.0% 33.05 KB 33.05 KB 8.53 KB 8.53 KB NODE_DEV
react-test-renderer-shallow.production.min.js 0.0% 🔺+0.1% 11.72 KB 11.72 KB 3.68 KB 3.68 KB NODE_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactNativeRenderer-dev.js +1.5% +1.4% 750.09 KB 760.98 KB 158.96 KB 161.26 KB RN_FB_DEV
ReactNativeRenderer-prod.js 🔺+0.1% 🔺+0.1% 277.29 KB 277.49 KB 47.6 KB 47.64 KB RN_FB_PROD
ReactNativeRenderer-profiling.js 0.0% +0.1% 286.22 KB 286.34 KB 49.32 KB 49.35 KB RN_FB_PROFILING
ReactFabric-dev.js +1.4% +1.4% 755.51 KB 766.42 KB 159.79 KB 162.1 KB RN_FB_DEV
ReactFabric-prod.js 🔺+0.1% 🔺+0.1% 268.95 KB 269.14 KB 46.21 KB 46.25 KB RN_FB_PROD
ReactFabric-profiling.js 0.0% +0.1% 279.05 KB 279.19 KB 48.05 KB 48.08 KB RN_FB_PROFILING
ReactNativeRenderer-dev.js +1.5% +1.4% 749.92 KB 760.81 KB 158.88 KB 161.17 KB RN_OSS_DEV
ReactNativeRenderer-prod.js 🔺+0.1% 🔺+0.1% 276.9 KB 277.1 KB 47.53 KB 47.57 KB RN_OSS_PROD
ReactNativeRenderer-profiling.js 0.0% +0.1% 285.83 KB 285.96 KB 49.25 KB 49.28 KB RN_OSS_PROFILING
ReactFabric-dev.js +1.4% +1.4% 755.33 KB 766.24 KB 159.71 KB 162.02 KB RN_OSS_DEV
ReactFabric-prod.js 🔺+0.1% 🔺+0.1% 268.6 KB 268.79 KB 46.13 KB 46.18 KB RN_OSS_PROD
ReactFabric-profiling.js 0.0% +0.1% 278.71 KB 278.84 KB 47.98 KB 48.01 KB RN_OSS_PROFILING

ReactDOM: size: 🔺+0.1%, gzip: -0.0%

Size changes (stable)

Generated by 🚫 dangerJS against e33346d

@sizebot
Copy link

sizebot commented Dec 3, 2019

Details of bundled changes.

Comparing: acfe4b2...e33346d

react-dom

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-dom.profiling.min.js +0.1% -0.1% 123.69 KB 123.75 KB 38.81 KB 38.77 KB NODE_PROFILING
react-dom-server.browser.development.js +0.2% +0.1% 140.46 KB 140.76 KB 36.91 KB 36.96 KB UMD_DEV
react-dom-server.browser.production.min.js 0.0% 0.0% 20.39 KB 20.39 KB 7.48 KB 7.48 KB UMD_PROD
react-dom-test-utils.development.js +0.1% +0.1% 56.21 KB 56.27 KB 15.55 KB 15.56 KB UMD_DEV
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.88 KB 3.88 KB 1.55 KB 1.55 KB UMD_DEV
react-dom-test-utils.production.min.js 0.0% 0.0% 11.18 KB 11.18 KB 4.15 KB 4.15 KB UMD_PROD
ReactDOMServer-dev.js +0.2% +0.1% 139.78 KB 140.05 KB 35.47 KB 35.51 KB FB_WWW_DEV
react-dom-test-utils.development.js +0.1% +0.1% 54.48 KB 54.54 KB 15.22 KB 15.23 KB NODE_DEV
react-dom-unstable-fizz.browser.development.js 0.0% +0.1% 3.71 KB 3.71 KB 1.5 KB 1.5 KB NODE_DEV
react-dom-test-utils.production.min.js 0.0% 🔺+0.1% 10.95 KB 10.95 KB 4.09 KB 4.09 KB NODE_PROD
react-dom-unstable-fizz.browser.production.min.js 0.0% 🔺+0.2% 1.05 KB 1.05 KB 640 B 641 B NODE_PROD
react-dom.development.js +2.7% +2.0% 955.26 KB 980.59 KB 216.05 KB 220.45 KB UMD_DEV
react-dom.production.min.js 🔺+0.1% 0.0% 119.74 KB 119.81 KB 38.46 KB 38.46 KB UMD_PROD
react-dom.profiling.min.js +0.1% 0.0% 123.42 KB 123.49 KB 39.56 KB 39.56 KB UMD_PROFILING
react-dom.development.js +2.7% +2.1% 949.33 KB 974.68 KB 214.46 KB 218.9 KB NODE_DEV
react-dom-server.node.development.js +0.2% +0.1% 137.5 KB 137.8 KB 36.14 KB 36.18 KB NODE_DEV
react-dom.production.min.js 🔺+0.1% -0.1% 119.88 KB 119.95 KB 37.79 KB 37.76 KB NODE_PROD
react-dom-server.node.production.min.js 0.0% 0.0% 20.72 KB 20.72 KB 7.61 KB 7.61 KB NODE_PROD
react-dom-server.browser.development.js +0.2% +0.1% 136.39 KB 136.7 KB 35.91 KB 35.96 KB NODE_DEV
react-dom-server.browser.production.min.js 0.0% 0.0% 20.31 KB 20.31 KB 7.46 KB 7.46 KB NODE_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 60.14 KB 60.14 KB 15.8 KB 15.8 KB UMD_DEV
ReactDOM-dev.js +2.7% +2.2% 977.5 KB 1003.71 KB 217.03 KB 221.78 KB FB_WWW_DEV
ReactDOM-prod.js 🔺+2.1% 🔺+1.7% 402.69 KB 411.16 KB 73.25 KB 74.52 KB FB_WWW_PROD
react-dom-unstable-native-dependencies.development.js 0.0% 0.0% 59.82 KB 59.82 KB 15.66 KB 15.66 KB NODE_DEV
react-dom-unstable-fizz.node.development.js 0.0% +0.1% 4.42 KB 4.42 KB 1.65 KB 1.65 KB NODE_DEV
ReactDOM-profiling.js +2.1% +1.7% 404.49 KB 412.95 KB 74.19 KB 75.47 KB FB_WWW_PROFILING
react-dom-unstable-native-dependencies.production.min.js 0.0% 0.0% 10.48 KB 10.48 KB 3.57 KB 3.57 KB NODE_PROD
react-dom-unstable-fizz.node.production.min.js 0.0% 🔺+0.1% 1.21 KB 1.21 KB 696 B 697 B NODE_PROD

react-test-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-test-renderer.development.js +1.7% +1.6% 619 KB 629.4 KB 132.04 KB 134.16 KB UMD_DEV
react-test-renderer.production.min.js 🔺+0.1% 🔺+0.1% 71.85 KB 71.91 KB 21.95 KB 21.97 KB UMD_PROD
ReactTestRenderer-dev.js +1.7% +1.7% 629.65 KB 640.18 KB 131.22 KB 133.44 KB FB_WWW_DEV
react-test-renderer-shallow.production.min.js 0.0% 0.0% 11.6 KB 11.6 KB 3.58 KB 3.58 KB UMD_PROD
react-test-renderer-shallow.development.js 0.0% 0.0% 33.07 KB 33.07 KB 8.53 KB 8.54 KB NODE_DEV
react-test-renderer-shallow.production.min.js 0.0% 🔺+0.1% 11.73 KB 11.73 KB 3.69 KB 3.69 KB NODE_PROD
react-test-renderer.development.js +1.7% +1.6% 614.27 KB 624.67 KB 130.86 KB 133 KB NODE_DEV
react-test-renderer.production.min.js 🔺+0.1% 🔺+0.1% 71.54 KB 71.6 KB 21.59 KB 21.61 KB NODE_PROD

react-native-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactFabric-prod.js 🔺+0.1% 🔺+0.1% 268.96 KB 269.16 KB 46.22 KB 46.26 KB RN_FB_PROD
ReactNativeRenderer-dev.js +1.5% +1.4% 749.93 KB 760.82 KB 158.89 KB 161.18 KB RN_OSS_DEV
ReactFabric-profiling.js 0.0% +0.1% 279.07 KB 279.2 KB 48.06 KB 48.09 KB RN_FB_PROFILING
ReactNativeRenderer-prod.js 🔺+0.1% 🔺+0.1% 277.3 KB 277.5 KB 47.61 KB 47.65 KB RN_FB_PROD
ReactNativeRenderer-profiling.js 0.0% +0.1% 286.23 KB 286.36 KB 49.33 KB 49.36 KB RN_FB_PROFILING
ReactFabric-dev.js +1.4% +1.4% 755.34 KB 766.25 KB 159.72 KB 162.02 KB RN_OSS_DEV
ReactFabric-prod.js 🔺+0.1% 🔺+0.1% 268.61 KB 268.81 KB 46.14 KB 46.19 KB RN_OSS_PROD
ReactFabric-profiling.js 0.0% +0.1% 278.72 KB 278.86 KB 47.99 KB 48.01 KB RN_OSS_PROFILING
ReactFabric-dev.js +1.4% +1.4% 755.52 KB 766.44 KB 159.8 KB 162.11 KB RN_FB_DEV
ReactNativeRenderer-prod.js 🔺+0.1% 🔺+0.1% 276.91 KB 277.11 KB 47.54 KB 47.58 KB RN_OSS_PROD
ReactNativeRenderer-profiling.js 0.0% +0.1% 285.85 KB 285.97 KB 49.26 KB 49.29 KB RN_OSS_PROFILING
ReactNativeRenderer-dev.js +1.5% +1.4% 750.1 KB 760.99 KB 158.97 KB 161.26 KB RN_FB_DEV

react-noop-renderer

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-noop-renderer.development.js +1.4% +0.9% 35 KB 35.48 KB 8 KB 8.08 KB NODE_DEV
react-noop-renderer-flight-client.development.js 0.0% +0.3% 1.63 KB 1.63 KB 777 B 779 B NODE_DEV
react-noop-renderer.production.min.js 🔺+2.5% 🔺+1.1% 11.85 KB 12.14 KB 3.78 KB 3.82 KB NODE_PROD
react-noop-renderer-flight-client.production.min.js 0.0% 🔺+0.5% 574 B 574 B 421 B 423 B NODE_PROD
react-noop-renderer-flight-server.production.min.js 0.0% 🔺+0.2% 1007 B 1007 B 566 B 567 B NODE_PROD
react-noop-renderer-server.development.js 0.0% +0.1% 1.98 KB 1.98 KB 905 B 906 B NODE_DEV
react-noop-renderer-server.production.min.js 0.0% 🔺+0.2% 901 B 901 B 512 B 513 B NODE_PROD
react-noop-renderer-persistent.development.js +1.4% +0.9% 35.11 KB 35.6 KB 8.02 KB 8.09 KB NODE_DEV
react-noop-renderer-persistent.production.min.js 🔺+2.5% 🔺+1.1% 11.87 KB 12.16 KB 3.79 KB 3.83 KB NODE_PROD

react-art

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
ReactART-dev.js +1.8% +1.8% 618 KB 628.89 KB 128.65 KB 130.92 KB FB_WWW_DEV
ReactART-prod.js 🔺+0.1% 🔺+0.1% 235.6 KB 235.81 KB 39.88 KB 39.93 KB FB_WWW_PROD
react-art.development.js +1.6% +1.5% 674.08 KB 684.88 KB 146.35 KB 148.57 KB UMD_DEV
react-art.production.min.js 🔺+0.1% 🔺+0.1% 106.81 KB 106.87 KB 32.43 KB 32.46 KB UMD_PROD
react-art.development.js +1.8% +1.7% 604.72 KB 615.53 KB 128.96 KB 131.16 KB NODE_DEV
react-art.production.min.js 🔺+0.1% 🔺+0.1% 71.78 KB 71.83 KB 21.49 KB 21.5 KB NODE_PROD

react-reconciler

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react-reconciler-persistent.development.js +1.8% +1.7% 603.22 KB 613.9 KB 127.05 KB 129.27 KB NODE_DEV
react-reconciler-reflection.development.js +0.3% +0.2% 19.09 KB 19.15 KB 6.23 KB 6.25 KB NODE_DEV
react-reconciler-persistent.production.min.js 🔺+0.1% 🔺+0.1% 72.59 KB 72.65 KB 21.33 KB 21.35 KB NODE_PROD
react-reconciler-reflection.production.min.js 0.0% 🔺+0.1% 2.86 KB 2.86 KB 1.24 KB 1.24 KB NODE_PROD
react-reconciler.development.js +1.8% +1.7% 605.75 KB 616.48 KB 128.16 KB 130.37 KB NODE_DEV
react-reconciler.production.min.js 🔺+0.1% 🔺+0.1% 74.81 KB 74.87 KB 21.9 KB 21.92 KB NODE_PROD

ReactDOM: size: 0.0%, gzip: 0.0%

Size changes (experimental)

Generated by 🚫 dangerJS against e33346d

@trueadm trueadm force-pushed the create-listener-api branch 6 times, most recently from 08ab510 to 6d20886 Compare December 4, 2019 12:16
Copy link
Contributor

@necolas necolas left a comment

Choose a reason for hiding this comment

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

Something to note: I've come to really appreciated the documentation and ascii diagrams contained in the ResponderEventPlugin. It's really helpful to get an overview of how a system is designed before reading through the code.

/* Negotiation Performed
+-----------------------+
/ \
Process low level events to + Current Responder + wantsResponderID
determine who to perform negot-| (if any exists at all) |
iation/transition | Otherwise just pass through|
-------------------------------+----------------------------+------------------+
Bubble to find first ID | |
to return true:wantsResponderID| |
| |
+-------------+ | |
| onTouchStart| | |
+------+------+ none | |
| return| |
+-----------v-------------+true| +------------------------+ |
|onStartShouldSetResponder|----->|onResponderStart (cur) |<-----------+
+-----------+-------------+ | +------------------------+ | |
| | | +--------+-------+
| returned true for| false:REJECT +-------->|onResponderReject
| wantsResponderID | | | +----------------+
| (now attempt | +------------------+-----+ |
| handoff) | | onResponder | |
+------------------->| TerminationRequest| |
| +------------------+-----+ |
| | | +----------------+
| true:GRANT +-------->|onResponderGrant|
| | +--------+-------+
| +------------------------+ | |
| | onResponderTerminate |<-----------+
| +------------------+-----+ |
| | | +----------------+
| +-------->|onResponderStart|
| | +----------------+
Bubble to find first ID | |
to return true:wantsResponderID| |
| |
+-------------+ | |
| onTouchMove | | |
+------+------+ none | |
| return| |
+-----------v-------------+true| +------------------------+ |
|onMoveShouldSetResponder |----->|onResponderMove (cur) |<-----------+
+-----------+-------------+ | +------------------------+ | |
| | | +--------+-------+
| returned true for| false:REJECT +-------->|onResponderRejec|
| wantsResponderID | | | +----------------+
| (now attempt | +------------------+-----+ |
| handoff) | | onResponder | |
+------------------->| TerminationRequest| |
| +------------------+-----+ |
| | | +----------------+
| true:GRANT +-------->|onResponderGrant|
| | +--------+-------+
| +------------------------+ | |
| | onResponderTerminate |<-----------+
| +------------------+-----+ |
| | | +----------------+
| +-------->|onResponderMove |
| | +----------------+
| |
| |
Some active touch started| |
inside current responder | +------------------------+ |
+------------------------->| onResponderEnd | |
| | +------------------------+ |
+---+---------+ | |
| onTouchEnd | | |
+---+---------+ | |
| | +------------------------+ |
+------------------------->| onResponderEnd | |
No active touches started| +-----------+------------+ |
inside current responder | | |
| v |
| +------------------------+ |
| | onResponderRelease | |
| +------------------------+ |
| |
+ + */

@trueadm trueadm force-pushed the create-listener-api branch 12 times, most recently from f273405 to 12098ff Compare December 4, 2019 21:09
@trueadm trueadm force-pushed the create-listener-api branch from 12098ff to 931d40e Compare December 4, 2019 21:16
More features

More features

More features

More features

More features

add file

add file

WIP

WIP

FIX

WIP

WIP

WIP

WIP

WIP

WIP

WIP

WIP

WIP

WIP

WIP

Cleanup

Cleanup

Address feedback and refactor some logic

Address feedback

Fix conflict

Fix renderers

Remove comment

Fix custom renderer

Fix

Revert flag

Revert flag

Propagation clean up

Propagation clean up

Used named import
@trueadm trueadm force-pushed the create-listener-api branch from 931d40e to e33346d Compare December 4, 2019 22:05
@jaredpalmer
Copy link
Contributor

jaredpalmer commented Dec 7, 2019

This is really cool. Forgive me if this is super obvious, but is there a going to be a way to dispatch custom events? Use case would be sharing a listener with complex inputs (like react-select, draftjs, etc.)

const inputLogger = unstable_createListener('change', (eventOrValue) => {
   if (isEvent(eventOrValue) {
     console.log(eventOrValue.target.value) 
   } else {
     console.log(value)
   }
}

@trueadm
Copy link
Contributor Author

trueadm commented Dec 8, 2019

@jaredpalmer I'm a bit confused by your example? This API binds around the native browser event, so you'd always get back the native browser event. You can capture custom native events too, but you'd have to dispatch those via the DOM for the event system to pick it up. If you wanted something like your example above, you'd likely have to add another layer on top of the Listener API rather than consuming it directly; then you can pass around any value you want.

@jaredpalmer
Copy link
Contributor

@trueadm got it. that makes sense.

@trueadm
Copy link
Contributor Author

trueadm commented Dec 20, 2019

I'm closing this PR. We might want to re-visit this approach again in the future, but there are so many conflicts that it's probably better to start again (plus there are fundamental flaws in some aspects of this PR's design). For a look an alternative approach we're thinking of, see: #17651.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants