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

feat(dynamicWidgets): prevent calling of "render" when widgets are not yet added #4997

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,7 @@ describe('connectDynamicWidgets', () => {
expect(
dynamicWidgets.getRenderState(existingRenderState, createInitOptions())
).toEqual({
PREVENT_RENDER: true,
dynamicWidgets: {
attributesToRender: [],
widgetParams,
Expand Down Expand Up @@ -750,6 +751,7 @@ describe('connectDynamicWidgets', () => {
createRenderOptions()
)
).toEqual({
PREVENT_RENDER: true,
dynamicWidgets: {
attributesToRender: ['test1'],
widgetParams,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,10 +212,22 @@ const connectDynamicWidgets: DynamicWidgetsConnector =
})
);
},
rendersOnPrevented: true,
getRenderState(renderState, renderOptions) {
const { PREVENT_RENDER = true } = renderState;
const dynamicWidgets = this.getWidgetRenderState(renderOptions);

// if we are in a "has results, but only just mounted widgets" state, add a flag
// in other cases the flag isn't set and we *do* render the other widgets
// TODO: improve this condition, reordering is allowed, we just want to make sure it's the same items
const willChangeWidgets =
renderState.dynamicWidgets?.attributesToRender.join('__') !==
dynamicWidgets?.attributesToRender.join('__');

return {
...renderState,
dynamicWidgets: this.getWidgetRenderState(renderOptions),
PREVENT_RENDER: PREVENT_RENDER !== false && willChangeWidgets,
dynamicWidgets,
};
},
getWidgetRenderState({ results, state }) {
Expand Down
4 changes: 3 additions & 1 deletion packages/instantsearch.js/src/types/render-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,10 @@ type ConnectorRenderStates = AnswersWidgetDescription['indexRenderState'] &
type WidgetRenderStates = AnalyticsWidgetDescription['indexRenderState'] &
PlacesWidgetDescription['indexRenderState'];

type GlobalRenderStates = { PREVENT_RENDER: boolean };

export type IndexRenderState = Partial<
ConnectorRenderStates & WidgetRenderStates
ConnectorRenderStates & WidgetRenderStates & GlobalRenderStates
>;

export type RenderState = {
Expand Down
6 changes: 6 additions & 0 deletions packages/instantsearch.js/src/types/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,12 @@ type RequiredRenderStateLifeCycle<
>,
renderOptions: InitOptions | RenderOptions
) => IndexRenderState & TWidgetDescription['indexRenderState'];

/**
* If PREVENT_RENDER is set in the render state, still render this widget.
* This is only useful for cases where this widget is the one setting PREVENT_RENDER
*/
rendersOnPrevented?: boolean;
};

type RenderStateLifeCycle<
Expand Down
9 changes: 8 additions & 1 deletion packages/instantsearch.js/src/widgets/index/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,12 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
}
});

// a widget that causes the current state to be incomplete (dynamic widgets)
// can prevent following widgets from rendering this pass by setting the
// PREVENT_RENDER flag to "true"
const { PREVENT_RENDER = false } =
instantSearchInstance.renderState?.[this.getIndexId()] ?? {};

widgetsToRender.forEach((widget) => {
// At this point, all the variables used below are set. Both `helper`
// and `derivedHelper` have been created at the `init` step. The attribute
Expand All @@ -652,7 +658,8 @@ const index = (widgetParams: IndexWidgetParams): IndexWidget => {
// be delayed. The render is triggered for the complete tree but some parts do
// not have results yet.

if (widget.render) {
// the widget can make itself rendering, even if the rest is prevented
if (widget.render && (!PREVENT_RENDER || widget.rendersOnPrevented)) {
widget.render(createRenderArgs(instantSearchInstance, this));
}
});
Expand Down