Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/src/defer/idle_scheduler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export class IdleScheduler implements OnDestroy {

if (bucket.queue.size > 0) {
this.scheduleBucket(bucket, options);
} else {
} else if (this.buckets.get(key) === bucket) {
this.buckets.delete(key);
}
};
Expand Down
135 changes: 135 additions & 0 deletions packages/core/test/acceptance/defer_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
provideZoneChangeDetection,
QueryList,
ɵRuntimeError as RuntimeError,
signal,
Type,
ViewChild,
ViewChildren,
Expand Down Expand Up @@ -829,6 +830,83 @@ describe('@defer', () => {
globalThis.cancelIdleCallback = nativeCancelIdleCallback;
}
});

it('should cancel an `on idle` request registered while another idle callback was running', () => {
@Component({
selector: 'cmp-a',
template: 'A',
changeDetection: ChangeDetectionStrategy.Eager,
})
class CmpA {}

@Component({
selector: 'cmp-b',
template: 'B',
changeDetection: ChangeDetectionStrategy.Eager,
})
class CmpB {}

@Component({
selector: 'root-app',
imports: [CmpA, CmpB],
template: `
@defer (on idle) {
<cmp-a />
}
@if (showB()) {
@defer (on idle) {
<cmp-b />
}
}
`,
changeDetection: ChangeDetectionStrategy.Eager,
})
class RootCmp {
showB = signal(false);
}

@Injectable({providedIn: 'root'})
class CustomIdleService implements IdleService {
private nextId = 0;
readonly pending = new Map<number, (deadline?: IdleDeadline) => void>();

requestOnIdle(callback: (deadline?: IdleDeadline) => void): number {
const id = ++this.nextId;
this.pending.set(id, callback);
return id;
}

cancelOnIdle(id: number): void {
this.pending.delete(id);
}

trigger(): void {
const callbacks = [...this.pending.values()];
this.pending.clear();
callbacks.forEach((callback) => callback());
}
}

TestBed.configureTestingModule({
providers: [...COMMON_PROVIDERS, provideIdleServiceWith(CustomIdleService)],
});

const fixture = TestBed.createComponent(RootCmp);
// Attach the view to `ApplicationRef` so that the tick inside the idle callback refreshes it.
fixture.autoDetectChanges();

const idleService = TestBed.inject(CustomIdleService);
expect(idleService.pending.size).toBe(1);

// Make the `@if` pending so that the tick inside the idle callback renders it.
// Its block then requests a new idle callback while the first one is still running.
fixture.componentInstance.showB.set(true);
idleService.trigger();
expect(idleService.pending.size).toBe(1);

fixture.destroy();
expect(idleService.pending.size).toBe(0);
});
});

describe('directive matching', () => {
Expand Down Expand Up @@ -5649,4 +5727,61 @@ describe('IdleScheduler', () => {
expect(cbB).toHaveBeenCalledTimes(1);
expect(ricCount).toBe(1);
});

it('should cancel a bucket that was created while another bucket with the same key was draining', () => {
let capturedCbs: Array<(deadline: any) => void> = [];
let ricCount = 0;

customIdleService.requestOnIdleSpy.and.callFake((cb: any) => {
ricCount++;
capturedCbs.push(cb);
return 100 + ricCount;
});
const cancelOnIdleSpy = spyOn(customIdleService, 'cancelOnIdle');

const cbB = jasmine.createSpy('cbB');
const cbA = jasmine.createSpy('cbA').and.callFake(() => {
// Emulates a trigger cleanup emptying the current bucket, followed by
// a new idle-triggered block being registered within the same drain.
scheduler.remove(cbA);
scheduler.add(cbB);
});

scheduler.add(cbA);
capturedCbs[0]({didTimeout: false, timeRemaining: () => 10});

expect(cbA).toHaveBeenCalledTimes(1);
expect(cbB).toHaveBeenCalledTimes(0);
expect(ricCount).toBe(2);

cancelOnIdleSpy.calls.reset();
scheduler.remove(cbB);
expect(cancelOnIdleSpy).toHaveBeenCalledOnceWith(102);
});

it('should cancel a bucket created during another bucket drain on destroy', () => {
let capturedCbs: Array<(deadline: any) => void> = [];
let ricCount = 0;

customIdleService.requestOnIdleSpy.and.callFake((cb: any) => {
ricCount++;
capturedCbs.push(cb);
return 100 + ricCount;
});
const cancelOnIdleSpy = spyOn(customIdleService, 'cancelOnIdle');

const cbB = jasmine.createSpy('cbB');
const cbA = jasmine.createSpy('cbA').and.callFake(() => {
scheduler.remove(cbA);
scheduler.add(cbB);
});

scheduler.add(cbA);
capturedCbs[0]({didTimeout: false, timeRemaining: () => 10});
expect(ricCount).toBe(2);

cancelOnIdleSpy.calls.reset();
scheduler.ngOnDestroy();
expect(cancelOnIdleSpy).toHaveBeenCalledOnceWith(102);
});
});
Loading