Skip to content

Commit

Permalink
fix(core): error when invoking callbacks registered via ViewRef.onDes…
Browse files Browse the repository at this point in the history
…troy (#37543)

Invoking a callback registered through `ViewRef.onDestroy` throws an error, because we weren't registering it correctly in the internal data structure. These changes also remove the `storeCleanupFn` function, because it was mostly identical to `storeCleanupWithContext` and was only used in one place.

Fixes #36213.

PR Close #37543
  • Loading branch information
crisbeto authored and AndrewKushnir committed Jun 26, 2020
1 parent 64b0ae9 commit 75b119e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
16 changes: 0 additions & 16 deletions packages/core/src/render3/instructions/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -795,22 +795,6 @@ export function storeCleanupWithContext(
}
}

/**
* Saves the cleanup function itself in LView.cleanupInstances.
*
* This is necessary for functions that are wrapped with their contexts, like in renderer2
* listeners.
*
* On the first template pass, the index of the cleanup function is saved in TView.
*/
export function storeCleanupFn(tView: TView, lView: LView, cleanupFn: Function): void {
getLCleanup(lView).push(cleanupFn);

if (tView.firstCreatePass) {
getTViewCleanup(tView).push(lView[CLEANUP]!.length - 1, null);
}
}

/**
* Constructs a TNode object from the arguments.
*
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/render3/view_ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {ChangeDetectorRef as viewEngine_ChangeDetectorRef} from '../change_detec
import {ViewContainerRef as viewEngine_ViewContainerRef} from '../linker/view_container_ref';
import {EmbeddedViewRef as viewEngine_EmbeddedViewRef, InternalViewRef as viewEngine_InternalViewRef} from '../linker/view_ref';
import {assertDefined} from '../util/assert';
import {checkNoChangesInRootView, checkNoChangesInternal, detectChangesInRootView, detectChangesInternal, markViewDirty, storeCleanupFn} from './instructions/shared';
import {checkNoChangesInRootView, checkNoChangesInternal, detectChangesInRootView, detectChangesInternal, markViewDirty, storeCleanupWithContext} from './instructions/shared';
import {CONTAINER_HEADER_OFFSET} from './interfaces/container';
import {TElementNode, TNode, TNodeType, TViewNode} from './interfaces/node';
import {isLContainer} from './interfaces/type_checks';
Expand Down Expand Up @@ -88,7 +88,7 @@ export class ViewRef<T> implements viewEngine_EmbeddedViewRef<T>, viewEngine_Int
}

onDestroy(callback: Function) {
storeCleanupFn(this._lView[TVIEW], this._lView, callback);
storeCleanupWithContext(this._lView[TVIEW], this._lView, null, callback);
}

/**
Expand Down
20 changes: 19 additions & 1 deletion packages/core/test/acceptance/view_ref_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* found in the LICENSE file at https://angular.io/license
*/

import {ApplicationRef, Component, ComponentFactoryResolver, ComponentRef, ElementRef, Injector, NgModule} from '@angular/core';
import {ApplicationRef, ChangeDetectorRef, Component, ComponentFactoryResolver, ComponentRef, ElementRef, Injector, NgModule} from '@angular/core';
import {InternalViewRef} from '@angular/core/src/linker/view_ref';
import {TestBed} from '@angular/core/testing';

Expand Down Expand Up @@ -54,4 +54,22 @@ describe('ViewRef', () => {
fixture.detectChanges();
expect(document.body.querySelector('dynamic-cpt')).toBeFalsy();
});

it('should invoke the onDestroy callback of a view ref', () => {
let called = false;

@Component({template: ''})
class App {
constructor(changeDetectorRef: ChangeDetectorRef) {
(changeDetectorRef as InternalViewRef).onDestroy(() => called = true);
}
}

TestBed.configureTestingModule({declarations: [App]});
const fixture = TestBed.createComponent(App);
fixture.detectChanges();
fixture.destroy();

expect(called).toBe(true);
});
});

0 comments on commit 75b119e

Please sign in to comment.