Skip to content

Commit

Permalink
Fixed an issue where lifecycleDone could be emitted before the last r…
Browse files Browse the repository at this point in the history
…ender is done
  • Loading branch information
bonkalol committed Feb 7, 2024
1 parent a1f8b0f commit 57b1e51
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,11 @@ export default class bVirtualScrollNew extends iVirtualScrollHandlers implements
});
});

this.componentInternalState.setIsDomInsertInProgress(true);

this.async.requestAnimationFrame(() => {
this.$refs.container.appendChild(fragment);
this.componentInternalState.setIsDomInsertInProgress(false);

this.onDomInsertDone();
this.onRenderDone();
Expand Down
24 changes: 20 additions & 4 deletions src/components/base/b-virtual-scroll-new/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* https://github.com/V4Fire/Client/blob/master/LICENSE
*/

import symbolGenerator from 'core/symbol';

import iVirtualScrollProps from 'components/base/b-virtual-scroll-new/props';

import type bVirtualScrollNew from 'components/base/b-virtual-scroll-new/b-virtual-scroll-new';
Expand All @@ -16,6 +18,8 @@ import { isAsyncReplaceError } from 'components/base/b-virtual-scroll-new/module

import iData, { component } from 'components/super/i-data/i-data';

const $$ = symbolGenerator();

/**
* A class that provides an API to handle events emitted by the {@link bVirtualScrollNew} component.
* This class is designed to work in conjunction with {@link bVirtualScrollNew}.
Expand Down Expand Up @@ -97,15 +101,27 @@ export abstract class iVirtualScrollHandlers extends iVirtualScrollProps {
*/
protected onLifecycleDone(this: bVirtualScrollNew): void {
const
state = this.getVirtualScrollState();
state = this.getVirtualScrollState(),
isDomInsertInProgress = this.componentInternalState.getIsDomInsertInProgress();

if (state.isLifecycleDone) {
return;
}

this.slotsStateController.doneState();
this.componentInternalState.setIsLifecycleDone(true);
this.componentEmitter.emit(componentEvents.lifecycleDone);
const handler = () => {
this.slotsStateController.doneState();
this.componentInternalState.setIsLifecycleDone(true);
this.componentEmitter.emit(componentEvents.lifecycleDone);
};

if (isDomInsertInProgress) {
return this.componentEmitter.once(componentEvents.renderDone, handler, {
group: bVirtualScrollNewAsyncGroup,
label: $$.waitUntilRenderDone
});
}

return handler();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ export interface PrivateComponentState {
* Pointer to the index of the data element that was last rendered.
*/
dataOffset: number;

/**
* If true, it means that the process of inserting components into the DOM tree is currently in progress.
*/
isDomInsertInProgress: boolean;
}

/**
Expand Down
16 changes: 16 additions & 0 deletions src/components/base/b-virtual-scroll-new/modules/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,14 @@ export class ComponentInternalState extends Friend {
this.state.isInitialRender = value;
}

/**
* Sets the flag indicating that the process of inserting components into the DOM tree is currently in progress
* @param value
*/
setIsDomInsertInProgress(value: boolean): void {
this.privateState.isDomInsertInProgress = value;
}

/**
* Sets the flag indicating if requests are stopped and the component won't make any more requests
* until the lifecycle is refreshed.
Expand Down Expand Up @@ -190,6 +198,14 @@ export class ComponentInternalState extends Friend {
return this.privateState.dataOffset;
}

/**
* Returns the value of the flag indicating whether the process
* of inserting components into the DOM tree is currently in progress
*/
getIsDomInsertInProgress(): boolean {
return this.privateState.isDomInsertInProgress;
}

/**
* Updates the cursor indicating the last index of the last rendered data element
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ test.describe('<b-virtual-scroll-new>', () => {
['renderEngineStart'],
['renderEngineDone'],
['domInsertStart'],
['lifecycleDone'],
['domInsertDone'],
['renderDone']
['renderDone'],
['lifecycleDone']
]);
});
});
Expand Down

0 comments on commit 57b1e51

Please sign in to comment.