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

Add WithResources to shared components #1636

Closed
wants to merge 1 commit into from
Closed

Add WithResources to shared components #1636

wants to merge 1 commit into from

Conversation

jtomasek
Copy link

As described here: #1539 (comment)

WithResources is an utility which simplifies work with Firehose and adds some features:

  • Firehose wont render children if model does not exist in k8s. WithResources will - both cases has their place

  • Resources accepts resourceMap which is object instead of array as in Firehose which brings us a few enhancements:

    • resourceMap keys are used as firehose resource prop which makes it easier if there is more queries for the same model, object use enforces use of unique keys

    • resource from resourceMap can be flagged as required - if yes we wait with child rendering until the resource is loaded (or we can show loading)

  • there is also way to define your own error handling

  • no need to check Firehose's loaded prop - when injected resource is undefined it means it is still loading

@openshift-ci-robot openshift-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label May 30, 2019
@openshift-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: jtomasek
To fully approve this pull request, please assign additional approvers.
We suggest the following additional approver: kyoto

If they are not already assigned, you can assign the PR to them by writing /assign @kyoto in a comment when ready.

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot
Copy link
Contributor

@jtomasek: The following test failed, say /retest to rerun them all:

Test name Commit Details Rerun command
ci/prow/e2e-aws-console-olm 9806cab link /test e2e-aws-console-olm

Full PR test history. Your PR dashboard. Please help us cut down on flakes by linking to an open issue when you hit one in your PR.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here.

if (resource) {
if (resource.loaded) {
childrenProps[resourceKey] = resource.data;
} else if (resourceConfig.required) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to do small refactoring when already moving the code?
I like isRequired more, as it is boolean.

const { onError, loaderComponent, children } = props;

React.useEffect(() => checkErrors(errors, onError), [errors]);
React.useEffect(() => setResourcesState(getResourcesState(props)), [props]);
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the second argument [props] for, please?

Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, getResourcesState(props) will result in the same value as for the useState() default above. Can you please elaborate this flow?

Copy link
Author

Choose a reason for hiding this comment

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

Second argument is the array of values that the effect depends on, the effect gets run only when one of the values changes. When an empty array is passed, the effect is only run on mount (same as componentDidMount). (https://reactjs.org/docs/hooks-reference.html#useeffect).

Original implementation used static getDerivedStateFromProps function to populate the state from props. First the state was initialized in constructor:
this.state = Resources.getDerivedStateFromProps(props);. To mimic this I am defining the useState hook and initialize the state by calling the function getResourcesState(props).
Then to update the state on props change I am using useEffect hook which gets run only when props change.

@spadgett
Copy link
Member

We're introducing a second component like Firehose, but with slightly different semantics. Now you have to think about which to use and remember the subtle differences between them. This seems like a bad direction. I would much rather focus on making the existing Firehose component better. Then you have a consistent interface everywhere you need data. It seems like most of the above could be accomplished with only a few flags added to Firehose.

@spadgett
Copy link
Member

cc @alecmerdler @christianvogt

@jtomasek
Copy link
Author

We're introducing a second component like Firehose...

I tend to agree, I am introducing this because a lot of components from kubevirt/web-ui are using it. We'll need to take a look at how big the impact is converting those to use Firehose directly.

@alecmerdler
Copy link
Contributor

FYI we've attempted to implement a new <Firehose> before using a firehoseFor() constructor function, which addressed the type-safety gaps and other issues here.

@spadgett spadgett added this to the v4.2 milestone May 30, 2019
@spadgett
Copy link
Member

I tend to agree, I am introducing this because a lot of components from kubevirt/web-ui are using it. We'll need to take a look at how big the impact is converting those to use Firehose directly.

Right, understood.

I did notice WithResources is used often inside of table cells. I worry about that. Watches are expensive on the backend, and you could even run into browser WebSocket limits using them that way. So we might need to look at the usage generally anyway.

@vojtechszocs
Copy link
Contributor

@spadgett We chatted about this briefly today with @jelkosz and @rawagner and there's an agreement to improve existing Firehose instead of adding another component. As we move code from KubeVirt fork & web-ui-components library, we can adapt it to use the improved Firehose.

We're introducing a second component like Firehose, but with slightly different semantics. Now you have to think about which to use and remember the subtle differences between them. This seems like a bad direction.

I agree 👍 WithResources exists because we wanted to minimize changes to existing Console code in the fork. Now that we're moving code into Console monorepo, we should align with its code.

@mareklibra
Copy link
Contributor

@jtomasek , I think we can close this already, right?

@jtomasek
Copy link
Author

@jtomasek , I think we can close this already, right?

Yes, this change is no longer needed. Closing.

@jtomasek jtomasek closed this Jul 11, 2019
@jtomasek jtomasek deleted the add-with-resources branch July 11, 2019 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants