Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions goldens/public-api/core/index.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ export function afterRenderEffect<E = never, W = never, M = never>(spec: {
// @public
export interface AfterRenderOptions {
injector?: Injector;
manualCleanup?: boolean;
// @deprecated
phase?: AfterRenderPhase;
}
Expand Down
11 changes: 10 additions & 1 deletion packages/core/src/render3/after_render/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ export interface AfterRenderOptions {
*/
injector?: Injector;

/**
* Whether the hook should require manual cleanup.
*
* If this is `false` (the default) the hook will automatically register itself to be cleaned up
* with the current `DestroyRef`.
*/
manualCleanup?: boolean;

/**
* The phase the callback should be invoked in.
*
Expand Down Expand Up @@ -448,11 +456,12 @@ function afterRenderImpl(
manager.impl ??= injector.get(AfterRenderImpl);

const hooks = options?.phase ?? AfterRenderPhase.MixedReadWrite;
const destroyRef = options?.manualCleanup !== true ? injector.get(DestroyRef) : null;
const sequence = new AfterRenderSequence(
manager.impl,
getHooks(callbackOrSpec, hooks),
once,
injector.get(DestroyRef),
destroyRef,
);
manager.impl.register(sequence);
return sequence;
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/render3/after_render/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,15 +149,15 @@ export class AfterRenderSequence implements AfterRenderRef {
*/
pipelinedValue: unknown = undefined;

private unregisterOnDestroy: () => void;
private unregisterOnDestroy: (() => void) | undefined;

constructor(
readonly impl: AfterRenderImpl,
readonly hooks: AfterRenderHooks,
public once: boolean,
destroyRef: DestroyRef,
destroyRef: DestroyRef | null,
) {
this.unregisterOnDestroy = destroyRef.onDestroy(() => this.destroy());
this.unregisterOnDestroy = destroyRef?.onDestroy(() => this.destroy());
}

afterRun(): void {
Expand All @@ -167,6 +167,6 @@ export class AfterRenderSequence implements AfterRenderRef {

destroy(): void {
this.impl.unregister(this);
this.unregisterOnDestroy();
this.unregisterOnDestroy?.();
}
}
47 changes: 47 additions & 0 deletions packages/core/test/acceptance/after_render_hook_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,53 @@ describe('after render hooks', () => {
);
});
});

it('should not destroy automatically if manualCleanup is set', () => {
let afterRenderRef: AfterRenderRef | null = null;
let count = 0;

@Component({selector: 'comp', template: ''})
class Comp {
constructor() {
afterRenderRef = afterRender(() => count++, {manualCleanup: true});
}
}

@Component({
imports: [Comp],
template: `
@if (shouldShow) {
<comp/>
}
`,
})
class App {
shouldShow = true;
}

TestBed.configureTestingModule({
declarations: [App, Comp],
...COMMON_CONFIGURATION,
});
const component = createAndAttachComponent(App);
const appRef = TestBed.inject(ApplicationRef);
expect(count).toBe(0);

appRef.tick();
expect(count).toBe(1);

component.instance.shouldShow = false;
component.changeDetectorRef.detectChanges();
appRef.tick();
expect(count).toBe(2);
appRef.tick();
expect(count).toBe(3);

// Ensure that manual destruction still works.
afterRenderRef!.destroy();
appRef.tick();
expect(count).toBe(3);
});
});

describe('afterNextRender', () => {
Expand Down