Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0c80349

Browse files
alan-agius4dylhunn
authored andcommitted
refactor(http): replace zone.js macrotask creation with InitialRenderPendingTasks (#50425)
This commits refactors the HTTP client to use `InitialRenderPendingTasks` instead of Zone.js macrotask. This is another approach to #50406 which was revert due to a failure in G3. PR Close #50425
1 parent 28c68f7 commit 0c80349

File tree

2 files changed

+10
-39
lines changed

2 files changed

+10
-39
lines changed

‎packages/common/http/src/interceptor.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {EnvironmentInjector, inject, Injectable, InjectionToken} from '@angular/core';
9+
import {EnvironmentInjector, inject, Injectable, InjectionToken, ɵInitialRenderPendingTasks as InitialRenderPendingTasks} from '@angular/core';
1010
import {Observable} from 'rxjs';
11+
import {finalize} from 'rxjs/operators';
1112

1213
import {HttpBackend, HttpHandler} from './backend';
1314
import {HttpRequest} from './request';
@@ -178,13 +179,16 @@ export function legacyInterceptorFnFactory(): HttpInterceptorFn {
178179
adaptLegacyInterceptorToChain, interceptorChainEndFn as ChainedInterceptorFn<any>);
179180
}
180181

181-
return chain(req, handler);
182+
const pendingTasks = inject(InitialRenderPendingTasks);
183+
const taskId = pendingTasks.add();
184+
return chain(req, handler).pipe(finalize(() => pendingTasks.remove(taskId)));
182185
};
183186
}
184187

185188
@Injectable()
186189
export class HttpInterceptorHandler extends HttpHandler {
187190
private chain: ChainedInterceptorFn<unknown>|null = null;
191+
private readonly pendingTasks = inject(InitialRenderPendingTasks);
188192

189193
constructor(private backend: HttpBackend, private injector: EnvironmentInjector) {
190194
super();
@@ -206,6 +210,9 @@ export class HttpInterceptorHandler extends HttpHandler {
206210
chainedInterceptorFn(nextSequencedFn, interceptorFn, this.injector),
207211
interceptorChainEndFn as ChainedInterceptorFn<unknown>);
208212
}
209-
return this.chain(initialRequest, downstreamRequest => this.backend.handle(downstreamRequest));
213+
214+
const taskId = this.pendingTasks.add();
215+
return this.chain(initialRequest, downstreamRequest => this.backend.handle(downstreamRequest))
216+
.pipe(finalize(() => this.pendingTasks.remove(taskId)));
210217
}
211218
}

‎packages/common/http/src/xhr.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -309,36 +309,18 @@ export class HttpXhrBackend implements HttpBackend {
309309
}
310310
}
311311

312-
let macroTaskCanceller: VoidFunction|undefined;
313-
314-
/** Tear down logic to cancel the backround macrotask. */
315-
const onLoadStart = () => {
316-
macroTaskCanceller ??= createBackgroundMacroTask();
317-
};
318-
const onLoadEnd = () => {
319-
macroTaskCanceller?.();
320-
};
321-
322-
xhr.addEventListener('loadstart', onLoadStart);
323-
xhr.addEventListener('loadend', onLoadEnd);
324-
325312
// Fire the request, and notify the event stream that it was fired.
326313
xhr.send(reqBody!);
327314
observer.next({type: HttpEventType.Sent});
328315
// This is the return from the Observable function, which is the
329316
// request cancellation handler.
330317
return () => {
331318
// On a cancellation, remove all registered event listeners.
332-
xhr.removeEventListener('loadstart', onLoadStart);
333-
xhr.removeEventListener('loadend', onLoadEnd);
334319
xhr.removeEventListener('error', onError);
335320
xhr.removeEventListener('abort', onError);
336321
xhr.removeEventListener('load', onLoad);
337322
xhr.removeEventListener('timeout', onError);
338323

339-
// Cancel the background macrotask.
340-
macroTaskCanceller?.();
341-
342324
if (req.reportProgress) {
343325
xhr.removeEventListener('progress', onDownProgress);
344326
if (reqBody !== null && xhr.upload) {
@@ -356,21 +338,3 @@ export class HttpXhrBackend implements HttpBackend {
356338
);
357339
}
358340
}
359-
360-
// Cannot use `Number.MAX_VALUE` as it does not fit into a 32-bit signed integer.
361-
const MAX_INT = 2147483647;
362-
363-
/**
364-
* A method that creates a background macrotask of up to Number.MAX_VALUE.
365-
*
366-
* This is so that Zone.js can intercept HTTP calls, this is important for server rendering,
367-
* as the application is only rendered once the application is stabilized, meaning there are pending
368-
* macro and micro tasks.
369-
*
370-
* @returns a callback method to cancel the macrotask.
371-
*/
372-
function createBackgroundMacroTask(): VoidFunction {
373-
const timeout = setTimeout(() => void 0, MAX_INT);
374-
375-
return () => clearTimeout(timeout);
376-
}

0 commit comments

Comments
 (0)