Skip to content

Commit

Permalink
fixup! fixup! refactor(core): add batching for defer blocks with `on …
Browse files Browse the repository at this point in the history
…idle` conditions
  • Loading branch information
AndrewKushnir committed Sep 25, 2023
1 parent f5d5147 commit 8d0fa51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
3 changes: 3 additions & 0 deletions packages/core/src/render3/instructions/defer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,16 @@ export function ɵɵdeferWhen(rawValue: unknown) {
const tNode = getSelectedTNode();
const lDetails = getLDeferBlockDetails(lView, tNode);
const renderedState = lDetails[DEFER_BLOCK_STATE];
console.log('Entering ɵɵdeferWhen with values: ', value, ' and ', renderedState);
if (value === false && renderedState === DeferBlockInternalState.Initial) {
// If nothing is rendered yet, render a placeholder (if defined).
console.log('Rendering placeholder');
renderPlaceholder(lView, tNode);
} else if (
value === true &&
(renderedState === DeferBlockInternalState.Initial ||
renderedState === DeferBlockState.Placeholder)) {
console.log('Triggering defer block');
// The `when` condition has changed to `true`, trigger defer block loading
// if the block is either in initial (nothing is rendered) or a placeholder
// state.
Expand Down
24 changes: 17 additions & 7 deletions packages/core/test/acceptance/defer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ function clearDirectiveDefs(type: Type<unknown>): void {
*/
function dynamicImportOf<T>(type: T, timeout = 0): Promise<T> {
return new Promise<T>(resolve => {
setTimeout(() => resolve(type), timeout);
console.log(`Scheduling ${timeout === 0 ? 'main import' : 'await all pending imports'}.`);
setTimeout(() => {
console.log(`Resolving ${timeout === 0 ? 'main import' : 'await all pending imports'}.`);
return resolve(type);
}, timeout);
});
}

Expand All @@ -54,7 +58,7 @@ function failedDynamicImport(): Promise<void> {
* emulated using `dynamicImportOf` function.
*/
function allPendingDynamicImports() {
return dynamicImportOf(null, 10);
return dynamicImportOf(null, 100);
}

// Set `PLATFORM_ID` to a browser platform value to trigger defer loading
Expand All @@ -66,8 +70,10 @@ describe('#defer', () => {
afterEach(() => setEnabledBlockTypes([]));

beforeEach(() => {
TestBed.configureTestingModule(
{providers: COMMON_PROVIDERS, deferBlockBehavior: DeferBlockBehavior.Playthrough});
TestBed.configureTestingModule({
providers: COMMON_PROVIDERS,
deferBlockBehavior: DeferBlockBehavior.Playthrough,
});
});

it('should transition between placeholder, loading and loaded states', async () => {
Expand Down Expand Up @@ -899,7 +905,7 @@ describe('#defer', () => {
expect(fixture.nativeElement.outerHTML).toContain('Rendering primary block');
});

it('should support `prefetch on idle` condition', async () => {
it('should support `prefetch on idle` condition #2', async () => {
@Component({
selector: 'nested-cmp',
standalone: true,
Expand All @@ -916,6 +922,8 @@ describe('#defer', () => {
template: `
{#defer when deferCond; prefetch on idle}
<nested-cmp [block]="'primary'" />
{:loading}
Loading
{:placeholder}
Placeholder
{/defer}
Expand Down Expand Up @@ -979,7 +987,7 @@ describe('#defer', () => {
expect(loadingFnInvokedTimes).toBe(1);
});

it('should trigger prefetching based on `on idle` only once', async () => {
it('should trigger prefetching based on `on idle` only once #2', async () => {
@Component({
selector: 'nested-cmp',
standalone: true,
Expand All @@ -997,6 +1005,8 @@ describe('#defer', () => {
{#for item of items; track item}
{#defer when deferCond; prefetch on idle}
<nested-cmp [block]="'primary for \`' + item + '\`'" />
{:loading}
Loading \`{{ item }}\`
{:placeholder}
Placeholder \`{{ item }}\`
{/defer}
Expand Down Expand Up @@ -1063,7 +1073,7 @@ describe('#defer', () => {
expect(loadingFnInvokedTimes).toBe(1);
});

it('should trigger fetching based on `on idle` only once', async () => {
it('should trigger fetching based on `on idle` only once #2', async () => {
@Component({
selector: 'nested-cmp',
standalone: true,
Expand Down

0 comments on commit 8d0fa51

Please sign in to comment.