Skip to content

Commit 9501a70

Browse files
committed
refactor: Support Explicit Resource Management on PendingTasks
Support Explicit Resource Management on PendingTasks fixes angular#64742
1 parent f80b51a commit 9501a70

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

packages/core/src/pending_tasks.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export class PendingTasks {
4646
*/
4747
add(): () => void {
4848
const taskId = this.internalPendingTasks.add();
49-
return () => {
49+
const cleanup = () => {
5050
if (!this.internalPendingTasks.has(taskId)) {
5151
// This pending task has already been cleared.
5252
return;
@@ -55,6 +55,8 @@ export class PendingTasks {
5555
this.scheduler.notify(NotificationSource.PendingTaskRemoved);
5656
this.internalPendingTasks.remove(taskId);
5757
};
58+
cleanup[Symbol.dispose] = cleanup;
59+
return cleanup;
5860
}
5961

6062
/**

packages/core/test/acceptance/pending_tasks_spec.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,21 @@ describe('public PendingTasks', () => {
113113
await expectAsync(appRef.whenStable()).toBeResolved();
114114
expect(spy).toHaveBeenCalled();
115115
});
116+
117+
it('should allow calling cleanup function multiple times', async () => {
118+
const appRef = TestBed.inject(ApplicationRef);
119+
const pendingTasks = TestBed.inject(PendingTasks);
120+
121+
const removeTask = pendingTasks.add();
122+
await expectAsync(applicationRefIsStable(appRef)).toBeResolvedTo(false);
123+
124+
removeTask();
125+
removeTask();
126+
removeTask();
127+
128+
TestBed.inject(ApplicationRef).tick();
129+
await expectAsync(applicationRefIsStable(appRef)).toBeResolvedTo(true);
130+
});
116131
});
117132

118133
function applicationRefIsStable(applicationRef: ApplicationRef) {

packages/tsconfig-build.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"moduleResolution": "bundler",
2020
"module": "esnext",
2121
"target": "es2022",
22-
"lib": ["es2020", "es2022", "dom", "dom.iterable"],
22+
"lib": ["es2020", "es2022", "esnext", "dom", "dom.iterable"],
2323
"skipLibCheck": true,
2424
// don't auto-discover @types/node, it results in a ///<reference in the .d.ts output
2525
"types": [],

0 commit comments

Comments
 (0)