MCPcopy Create free account
hub / github.com/angular/angular / processCleanups

Function processCleanups

packages/core/src/render3/node_manipulation.ts:386–435  ·  view source on GitHub ↗

Removes listeners and unsubscribes from output subscriptions

(tView: TView, lView: LView)

Source from the content-addressed store, hash-verified

384
385/** Removes listeners and unsubscribes from output subscriptions */
386function processCleanups(tView: TView, lView: LView): void {
387 ngDevMode && assertNotReactive(processCleanups.name);
388 const tCleanup = tView.cleanup;
389 const lCleanup = lView[CLEANUP]!;
390 if (tCleanup !== null) {
391 for (let i = 0; i < tCleanup.length - 1; i += 2) {
392 if (typeof tCleanup[i] === 'string') {
393 // This is a native DOM listener. It will occupy 4 entries in the TCleanup array (hence i +=
394 // 2 at the end of this block).
395 const targetIdx = tCleanup[i + 3];
396 ngDevMode && assertNumber(targetIdx, 'cleanup target must be a number');
397 if (targetIdx >= 0) {
398 // Destroy anything whose teardown is a function call (e.g. QueryList, ModelSignal).
399 lCleanup[targetIdx]();
400 } else {
401 // Subscription
402 lCleanup[-targetIdx].unsubscribe();
403 }
404 i += 2;
405 } else {
406 // This is a cleanup function that is grouped with the index of its context
407 const context = lCleanup[tCleanup[i + 1]];
408 tCleanup[i].call(context);
409 }
410 }
411 }
412 if (lCleanup !== null) {
413 lView[CLEANUP] = null;
414 }
415 const destroyHooks = lView[ON_DESTROY_HOOKS];
416 if (destroyHooks !== null) {
417 // Reset the ON_DESTROY_HOOKS array before iterating over it to prevent hooks that unregister
418 // themselves from mutating the array during iteration.
419 lView[ON_DESTROY_HOOKS] = null;
420 for (let i = 0; i < destroyHooks.length; i++) {
421 const destroyHooksFn = destroyHooks[i];
422 ngDevMode && assertFunction(destroyHooksFn, 'Expecting destroy hook to be a function.');
423 destroyHooksFn();
424 }
425 }
426
427 // Destroy effects registered to the view. Many of these will have been processed above.
428 const effects = lView[EFFECTS];
429 if (effects !== null) {
430 lView[EFFECTS] = null;
431 for (const effect of effects) {
432 effect.destroy();
433 }
434 }
435}
436
437/** Calls onDestroy hooks for this view */
438function executeOnDestroys(tView: TView, lView: LView): void {

Callers 1

cleanUpViewFunction · 0.85

Calls 5

assertNotReactiveFunction · 0.90
assertNumberFunction · 0.90
assertFunctionFunction · 0.90
unsubscribeMethod · 0.65
destroyMethod · 0.65

Tested by

no test coverage detected