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

Function processCleanups

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

Removes listeners and unsubscribes from output subscriptions

(tView: TView, lView: LView)

Source from the content-addressed store, hash-verified

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