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

1014-memoize-conditional-branch-widgets #1018

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

sagardspeed2
Copy link
Contributor

@sagardspeed2 sagardspeed2 commented Feb 3, 2025

Describe your changes

Memoize conditional branch widgets

Screenshots [Optional]

Issue ticket number and link

Closes #1014

Checklist before requesting a review

  • I have performed a self-review of my code
  • I have added tests
  • I have added a changeset pnpm changeset add
  • I have added example usage in the kitchen sink app

@sagardspeed2 sagardspeed2 requested a review from evshi February 3, 2025 13:10
@sagardspeed2 sagardspeed2 self-assigned this Feb 3, 2025
Copy link

changeset-bot bot commented Feb 3, 2025

🦋 Changeset detected

Latest commit: 078c704

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@ensembleui/react-runtime Patch
@ensembleui/react-kitchen-sink Patch
@ensembleui/react-preview Patch
@ensembleui/react-starter Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link
Contributor

github-actions bot commented Feb 3, 2025

Visit the preview URL for this PR (updated for commit 078c704):

https://react-kitchen-sink-dev--pr1018-1014-memoize-conditi-fr68kirn.web.app

(expires Thu, 13 Feb 2025 06:23:36 GMT)

🔥 via Firebase Hosting GitHub Action 🌎

Sign: 6267897ade2ba783b6db70a53a60fc3946d625e9

}, [conditionStatements, conditions, trueIndex]);

if (!widget) {
return null;
}

return <>{EnsembleRuntime.render([widget])}</>;
return <>{widget}</>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't need a fragment here anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Then we also need to update the return type of the Conditional widget from React.FC. is this fine ??

Copy link
Contributor

Choose a reason for hiding this comment

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

Why would that be the case? widget should be a ReactNode

Copy link
Contributor Author

@sagardspeed2 sagardspeed2 Feb 6, 2025

Choose a reason for hiding this comment

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

The EnsembleRuntime.render function returns ReactNode[]. When we save this in our widget memo, it will contain either that array of nodes or null. so we need to wrap the output in a fragment <>...</>.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure why this matters since ReactNode[] is also a valid ReactNode.

type ReactNode =
| ReactElement
| string
| number
| Iterable
| ReactPortal
| boolean
| null
| undefined
| DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES[
keyof DO_NOT_USE_OR_YOU_WILL_BE_FIRED_EXPERIMENTAL_REACT_NODES
];


fireEvent.click(screen.getByText("Decrease"));
expect(unwrapWidgetSpy).toHaveBeenCalledTimes(3);
expect(screen.getByText("Less than 0")).not.toBeNull();
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you add assertion about how many times EnsembleRuntime.render was called?

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

Successfully merging this pull request may close these issues.

Improve conditional performance by memoizing branch widgets
2 participants