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
fix(elements): support output()-shaped outputs
Previously Elements was assuming that every output was an RxJS `Subject` and
supports `.pipe()`. This is not true for `output()`-based outputs which
have `.subscribe()` but not `.pipe()`. This commit fixes such outputs by
using a `new Observable` instead of `map` to forward outputs.
  • Loading branch information
alxhub committed Aug 26, 2024
commit d03eaf1aa98319bb6f3ee21c1b8b88bfc64ded9e
10 changes: 7 additions & 3 deletions packages/elements/src/component-factory-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import {
ɵChangeDetectionScheduler as ChangeDetectionScheduler,
ɵNotificationSource as NotificationSource,
ɵViewRef as ViewRef,
OutputRef,
} from '@angular/core';
import {merge, Observable, ReplaySubject} from 'rxjs';
import {map, switchMap} from 'rxjs/operators';
import {switchMap} from 'rxjs/operators';

import {
NgElementStrategy,
Expand Down Expand Up @@ -219,8 +220,11 @@ export class ComponentNgElementStrategy implements NgElementStrategy {
protected initializeOutputs(componentRef: ComponentRef<any>): void {
const eventEmitters: Observable<NgElementStrategyEvent>[] = this.componentFactory.outputs.map(
({propName, templateName}) => {
const emitter: EventEmitter<any> = componentRef.instance[propName];
return emitter.pipe(map((value) => ({name: templateName, value})));
const emitter: EventEmitter<any> | OutputRef<any> = componentRef.instance[propName];
return new Observable((observer) => {
const sub = emitter.subscribe((value) => observer.next({name: templateName, value}));
return () => sub.unsubscribe();
});
},
);

Expand Down
14 changes: 14 additions & 0 deletions packages/elements/test/component-factory-strategy_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
Input,
NgZone,
Output,
OutputEmitterRef,
SimpleChange,
SimpleChanges,
createComponent,
Expand Down Expand Up @@ -113,6 +114,18 @@ describe('ComponentFactoryNgElementStrategy', () => {
]);
});

it('should listen to output() emitters', () => {
const events: NgElementStrategyEvent[] = [];
strategy.events.subscribe((e) => events.push(e));

componentRef.instance.output3.emit('output-a');
componentRef.instance.output3.emit('output-b');
expect(events).toEqual([
{name: 'templateOutput3', value: 'output-a'},
{name: 'templateOutput3', value: 'output-b'},
]);
});

it('should initialize the component with initial values', () => {
expect(strategy.getInputValue('fooFoo')).toBe('fooFoo-1');
expect(componentRef.instance.fooFoo).toBe('fooFoo-1');
Expand Down Expand Up @@ -369,6 +382,7 @@ export class CdTrackerDir {
export class TestComponent {
@Output('templateOutput1') output1 = new Subject();
@Output('templateOutput2') output2 = new Subject();
@Output('templateOutput3') output3 = new OutputEmitterRef();

@Input() fooFoo: unknown;
@Input({alias: 'my-bar-bar'}) barBar: unknown;
Expand Down