Skip to content

Commit 30f6db3

Browse files
JeanMecheAndrewKushnir
authored andcommitted
refactor(core): log a warning instead of throwing error on OutputRef.emit when the directive is destroyed. (#60293)
This should not be a hard error, as nothing bad happens but the users should be warned that no event will be emitted. fixes #60110 PR Close #60293
1 parent 442ef13 commit 30f6db3

File tree

3 files changed

+16
-8
lines changed

3 files changed

+16
-8
lines changed

packages/core/src/authoring/output/output_emitter_ref.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {setActiveConsumer} from '@angular/core/primitives/signals';
1010

1111
import {inject} from '../../di/injector_compatibility';
1212
import {ErrorHandler} from '../../error_handler';
13-
import {RuntimeError, RuntimeErrorCode} from '../../errors';
13+
import {formatRuntimeError, RuntimeError, RuntimeErrorCode} from '../../errors';
1414
import {DestroyRef} from '../../linker/destroy_ref';
1515

1616
import {OutputRef, OutputRefSubscription} from './output_ref';
@@ -69,12 +69,15 @@ export class OutputEmitterRef<T> implements OutputRef<T> {
6969
/** Emits a new value to the output. */
7070
emit(value: T): void {
7171
if (this.destroyed) {
72-
throw new RuntimeError(
73-
RuntimeErrorCode.OUTPUT_REF_DESTROYED,
74-
ngDevMode &&
75-
'Unexpected emit for destroyed `OutputRef`. ' +
76-
'The owning directive/component is destroyed.',
72+
console.warn(
73+
formatRuntimeError(
74+
RuntimeErrorCode.OUTPUT_REF_DESTROYED,
75+
ngDevMode &&
76+
'Unexpected emit for destroyed `OutputRef`. ' +
77+
'The owning directive/component is destroyed.',
78+
),
7779
);
80+
return;
7881
}
7982

8083
if (this.listeners === null) {

packages/core/test/acceptance/authoring/model_inputs_spec.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,9 @@ describe('model inputs', () => {
447447
expect(emittedEvents).toBe(1);
448448

449449
fixture.destroy();
450-
expect(() => modelRef.set(2)).toThrowError(/Unexpected emit for destroyed `OutputRef`/);
450+
const warnSpy = spyOn(console, 'warn');
451+
modelRef.set(2);
452+
expect(warnSpy.calls.mostRecent().args[0]).toMatch(/Unexpected emit for destroyed `OutputRef`/);
451453
expect(emittedEvents).toBe(1);
452454
});
453455

packages/core/test/acceptance/authoring/output_function_spec.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ describe('output() function', () => {
122122
fixture.componentInstance.show = false;
123123
fixture.detectChanges();
124124

125-
expect(() => dir.onBla.emit(3)).toThrowError(/Unexpected emit for destroyed `OutputRef`/);
125+
fixture.destroy();
126+
const warnSpy = spyOn(console, 'warn');
127+
dir.onBla.emit(3);
128+
expect(warnSpy.calls.mostRecent().args[0]).toMatch(/Unexpected emit for destroyed `OutputRef`/);
126129
});
127130

128131
it('should error when subscribing to a destroyed output', () => {

0 commit comments

Comments
 (0)