-
Couldn't load subscription status.
- Fork 2
1014-memoize-conditional-branch-widgets #1018
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
Open
sagardspeed2
wants to merge
8
commits into
main
Choose a base branch
from
1014-memoize-conditional-branch-widgets
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−7
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
61b6474
1014-memoize-conditional-branch-widgets
sagardspeed2 279128d
fix: memoize the react node of conditional widget
sagardspeed2 cf64c88
fix: update the types
sagardspeed2 c348a01
fix: update the types
sagardspeed2 a7ffb3e
Merge branch '1014-memoize-conditional-branch-widgets' of ensemble:En…
sagardspeed2 77fca62
fix: lint issue
sagardspeed2 49520c6
fix: added ref
sagardspeed2 078c704
fix: update values through key in object
sagardspeed2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@ensembleui/react-runtime": patch | ||
| --- | ||
|
|
||
| Fix: memoize conditional branch widgets |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,10 @@ | ||
| import { render, screen } from "@testing-library/react"; | ||
| /* eslint import/first: 0 */ | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment | ||
| const framework = jest.requireActual("@ensembleui/react-framework"); | ||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access | ||
| const unwrapWidgetSpy = jest.fn().mockImplementation(framework.unwrapWidget); | ||
| import { fireEvent, render, screen } from "@testing-library/react"; | ||
| import { BrowserRouter } from "react-router-dom"; | ||
| import type { ConditionalProps, ConditionalElement } from "../Conditional"; | ||
| import { | ||
| Conditional, | ||
|
|
@@ -7,9 +13,20 @@ import { | |
| extractCondition, | ||
| } from "../Conditional"; | ||
| import "../index"; | ||
| import { EnsembleScreen } from "../../runtime/screen"; | ||
|
|
||
| jest.mock("react-markdown", jest.fn()); | ||
|
|
||
| // eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
| jest.mock("@ensembleui/react-framework", () => ({ | ||
| ...framework, | ||
| unwrapWidget: unwrapWidgetSpy, | ||
| })); | ||
|
|
||
| afterEach(() => { | ||
| jest.clearAllMocks(); | ||
| }); | ||
|
|
||
| describe("Conditional Component", () => { | ||
| test('renders the widget when "if" condition is met', () => { | ||
| const conditionalProps: ConditionalProps = { | ||
|
|
@@ -233,3 +250,91 @@ describe("extractCondition Function", () => { | |
| expect(extractedCondition).toBe("1 === 1"); | ||
| }); | ||
| }); | ||
|
|
||
| describe("conditional widget memoization", () => { | ||
| it("should memoize branch widgets and prevent unnecessary re-renders", () => { | ||
| render( | ||
| <EnsembleScreen | ||
| screen={{ | ||
| name: "test_conditional", | ||
| id: "test_conditional", | ||
| body: { | ||
| name: "Column", | ||
| properties: { | ||
| children: [ | ||
| { | ||
| name: "Conditional", | ||
| properties: { | ||
| conditions: [ | ||
| { | ||
| if: `\${ensemble.storage.get('number') < 0}`, | ||
| Text: { | ||
| text: "Less than 0", | ||
| }, | ||
| }, | ||
| { | ||
| if: `\${ensemble.storage.get('number') === 0}`, | ||
| Text: { | ||
| text: "Equals to 0", | ||
| }, | ||
| }, | ||
| { | ||
| if: `\${ensemble.storage.get('number') > 0}`, | ||
| Text: { | ||
| text: "Greater than 0", | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| { | ||
| name: "Button", | ||
| properties: { | ||
| label: "Increase", | ||
| onTap: { | ||
| executeCode: | ||
| "ensemble.storage.set('number', ensemble.storage.get('number') + 1)", | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| name: "Button", | ||
| properties: { | ||
| label: "Decrease", | ||
| onTap: { | ||
| executeCode: | ||
| "ensemble.storage.set('number', ensemble.storage.get('number') - 1)", | ||
| }, | ||
| }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| onLoad: { executeCode: 'ensemble.storage.set("number", -1)' }, | ||
| }} | ||
| />, | ||
| { | ||
| wrapper: BrowserRouter, | ||
| }, | ||
| ); | ||
|
|
||
| expect(unwrapWidgetSpy).toHaveBeenCalledTimes(1); | ||
| expect(screen.getByText("Less than 0")).not.toBeNull(); | ||
|
|
||
| fireEvent.click(screen.getByText("Increase")); | ||
| expect(unwrapWidgetSpy).toHaveBeenCalledTimes(2); | ||
| expect(screen.getByText("Equals to 0")).not.toBeNull(); | ||
|
|
||
| fireEvent.click(screen.getByText("Increase")); | ||
| expect(unwrapWidgetSpy).toHaveBeenCalledTimes(3); | ||
| expect(screen.getByText("Greater than 0")).not.toBeNull(); | ||
|
|
||
| fireEvent.click(screen.getByText("Decrease")); | ||
| expect(unwrapWidgetSpy).toHaveBeenCalledTimes(3); | ||
| expect(screen.getByText("Equals to 0")).not.toBeNull(); | ||
|
|
||
| fireEvent.click(screen.getByText("Decrease")); | ||
| expect(unwrapWidgetSpy).toHaveBeenCalledTimes(3); | ||
| expect(screen.getByText("Less than 0")).not.toBeNull(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you add assertion about how many times EnsembleRuntime.render was called? |
||
| }); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
Conditionalwidget fromReact.FC. is this fine ??There was a problem hiding this comment.
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?
widgetshould be aReactNodeUh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
EnsembleRuntime.renderfunction returnsReactNode[]. 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<>...</>.There was a problem hiding this comment.
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 validReactNode.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
];