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

Expose injectIntoDevTools() to renderers #11463

Merged
merged 1 commit into from
Nov 6, 2017
Merged

Conversation

gaearon
Copy link
Collaborator

@gaearon gaearon commented Nov 5, 2017

An alternative to #11445.
This lets third party renderers inject themselves into the devtools.

Previously, renderers had to reach into reconciler directly to do this.
However, this wouldn't work for third party renderers.

Instead, I put injectIntoDevTools() directly on the ReactFiberReconciler() return value.
This way any renderer can choose to call it (or not call it).

Also moves us closer to not using cross-package /src/ imports.

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 5, 2017

@iamdustan Can you verify this solves your use case?

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.

Nice 👍

}
return findFiberByHostInstance(instance);
},
});
Copy link
Contributor

Choose a reason for hiding this comment

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

Are the wrapper functions necessary? Couldn't we just do...

    injectIntoDevTools(devToolsConfig: DevToolsConfig<I, TI>): boolean {
      const {findFiberByHostInstance} = devToolsConfig;
      const findFiberByHostInstanceImpl = findFiberByHostInstance
        ? findFiberByHostInstance
        : () => null;

      return ReactFiberDevToolsHook.injectInternals({
        ...devToolsConfig,
        findHostInstanceByFiber: findHostInstance,
        findFiberByHostInstance: findFiberByHostInstanceImpl,
      });
    },

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That would work too. I don't feel strongly as they're only called once.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I tried using your approach, but it seems like Flow fails to catch some issues for some reason now. I'd prefer keeping it explicit so it's better typed.

Copy link
Contributor

Choose a reason for hiding this comment

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

Boo, Flow.

Okay. No biggie.

@gaearon gaearon merged commit 8e7cb85 into facebook:master Nov 6, 2017
@gaearon gaearon deleted the inject-dt branch November 6, 2017 13:09
Copy link
Contributor

@iamdustan iamdustan left a comment

Choose a reason for hiding this comment

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

I missed the merge by a few minutes, but copying this build into Yomguithereal/react-blessed#60 and updating accordingly didn’t seem to work out of the box like before. I think that we need the FiberTreeReflection exposed as well 😬

findFiberByHostInstance?: (instance: I | TI) => Fiber,
// Used by RN in-app inspector.
// This API is unfortunately RN-specific.
// TODO: Change it to accept Fiber instead and type it properly.
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

// Used by RN in-app inspector.
// This API is unfortunately RN-specific.
// TODO: Change it to accept Fiber instead and type it properly.
getInspectorDataForViewTag?: (tag: number) => Object,
Copy link
Contributor

Choose a reason for hiding this comment

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

does the isAppActive() method no longer exist?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Can you remind me where it is used?

Copy link
Contributor

Choose a reason for hiding this comment

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

It used to be part of the devtools config I think. https://github.com/facebook/react-devtools/blob/ef493abba0d08c0243bdb1f2183edc797d5d366c/packages/react-devtools-core/src/backend.js#L13-L18

I think the usage was so that an app could indicate whether it should take over the devtools connection or not, but not really sure. Or maybe to return false while still starting up (according to this comment)

https://github.com/facebook/react-devtools/blob/ef493abba0d08c0243bdb1f2183edc797d5d366c/packages/react-devtools-core/src/backend.js#L52-L55

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

It's not required. I added it for RN so that multiple apps on simulator don't steal devtools from each other.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also, it's part of the options to connectToDevTools() call which sets up a websocket connection.

This is not related to the injectIntoDevTools call which injects renderer into a global variable.

rendererPackageName: string,
// Note: this actually *does* depend on Fiber internal fields.
// Used by "inspect clicked DOM element" in React DevTools.
findFiberByHostInstance?: (instance: I | TI) => Fiber,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the devtools integration used to work without this method 🤔

With this being required I think we’d also need ReactFiberTreeReflection.findCurrentHostFiber and ReactFiberTreeReflection.findCurrentHostFiberWithNoPortals also exposed.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I don't think we need them. This one doesn't have to be the current one. So as long as you save the fiber on some field on the instance (or use a weakmap), you can get one back.

Copy link
Contributor

@bvaughn bvaughn Nov 6, 2017

Choose a reason for hiding this comment

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

It still does. The implementation (below) is:

        findFiberByHostInstance(instance: I | TI): Fiber | null {
          if (!findFiberByHostInstance) {
            // Might not be implemented by the renderer.
            return null;
          }
          return findFiberByHostInstance(instance);
        },

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That's also true. (Though it's nice to implement if you can I guess.)

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 6, 2017

but copying this build into Yomguithereal/react-blessed#60 and updating accordingly didn’t seem to work out of the box like before

By “updating accordingly” you mean...? It’s not clear what you added there :-)

It won't just work out of the box, you need to call this method. See changes I did in ReactDOM for example.

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 6, 2017

If you can give me a branch that doesn't work I can take a look.

@iamdustan
Copy link
Contributor

I just pushed to Yomguithereal/react-blessed#60 with the “update accordingly” bit 😄

@iamdustan
Copy link
Contributor

You can test it by copying a fresh build from master in, run react-devtools in one tab and then yarn animation in another tab.

@iamdustan iamdustan mentioned this pull request Nov 6, 2017
11 tasks
@gaearon
Copy link
Collaborator Author

gaearon commented Nov 6, 2017

I just pushed to Yomguithereal/react-blessed#60 with the “update accordingly” bit

I think you're passing a config for require('react-devtools-core').connectToDevTools(). This is not related to the injectIntoDevTools() function I added. I'm sorry these two sound similar. There's a few moving parts and I couldn't come up with better names.

The config you need to pass to connectToDevTools() to establish a websocket connection stays the same.

The config you need to pass to renderer.injectIntoDevTools() is different. You can look at changes to ReactDOM.js in this file to see an example of it. injectIntoDevTools() is roughly equivalent to injectInternals() call in the past.

@iamdustan
Copy link
Contributor

👍 got it! This is working like a charm. Thanks, Dan!

devtools

@gaearon
Copy link
Collaborator Author

gaearon commented Nov 6, 2017

Sweet.

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.

5 participants