Skip to content

Commit b611367

Browse files
DaSchTourbrandonroberts
authored andcommitted
fix(component): transform to Observable if Input is Promise
1 parent 17f076c commit b611367

File tree

4 files changed

+56
-15
lines changed

4 files changed

+56
-15
lines changed

modules/component/spec/core/cd-aware/cd-aware_creator.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ describe('CdAware', () => {
108108
expect(cdAwareImplementation.renderedValue).toBe(42);
109109
});
110110

111+
it('should render_creator emitted value from passed promise without changing it', () => {
112+
cdAwareImplementation.cdAware.nextPotentialObservable(
113+
Promise.resolve(42)
114+
);
115+
expect(cdAwareImplementation.renderedValue).toBe(42);
116+
});
117+
111118
it('should render_creator undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)', () => {
112119
cdAwareImplementation.cdAware.nextPotentialObservable(of(42));
113120
expect(cdAwareImplementation.renderedValue).toBe(42);

modules/component/spec/let/let.directive.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ import {
55
ViewContainerRef,
66
} from '@angular/core';
77
import { fakeAsync, TestBed, tick, waitForAsync } from '@angular/core/testing';
8-
import { EMPTY, interval, NEVER, Observable, of, throwError } from 'rxjs';
8+
import {
9+
EMPTY,
10+
interval,
11+
NEVER,
12+
Observable,
13+
ObservableInput,
14+
of,
15+
throwError,
16+
} from 'rxjs';
917
import { take } from 'rxjs/operators';
1018

1119
import { LetDirective } from '../../src/let/let.directive';
@@ -65,7 +73,7 @@ class LetDirectiveTestCompleteComponent {
6573

6674
let fixtureLetDirectiveTestComponent: any;
6775
let letDirectiveTestComponent: {
68-
value$: Observable<any> | undefined | null;
76+
value$: ObservableInput<any> | undefined | null;
6977
};
7078
let componentNativeElement: any;
7179

@@ -188,6 +196,12 @@ describe('LetDirective', () => {
188196
expect(componentNativeElement.textContent).toBe('42');
189197
});
190198

199+
it('should render_creator emitted value from passed promise without changing it', () => {
200+
letDirectiveTestComponent.value$ = Promise.resolve(42);
201+
fixtureLetDirectiveTestComponent.detectChanges();
202+
expect(componentNativeElement.textContent).toBe('42');
203+
});
204+
191205
it('should render_creator undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)', () => {
192206
letDirectiveTestComponent.value$ = of(42);
193207
fixtureLetDirectiveTestComponent.detectChanges();

modules/component/spec/push/push.pipe.spec.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChangeDetectorRef, Component } from '@angular/core';
22
import { TestBed, waitForAsync } from '@angular/core/testing';
3-
import { EMPTY, NEVER, Observable, of } from 'rxjs';
3+
import { EMPTY, NEVER, Observable, ObservableInput, of } from 'rxjs';
44

55
import { PushPipe } from '../../src/push/push.pipe';
66
import { MockChangeDetectorRef } from '../fixtures/fixtures';
@@ -20,7 +20,7 @@ class PushPipeTestComponent {
2020

2121
let fixturePushPipeTestComponent: any;
2222
let pushPipeTestComponent: {
23-
value$: Observable<any> | undefined | null;
23+
value$: ObservableInput<any> | undefined | null;
2424
};
2525
let componentNativeElement: any;
2626

@@ -71,6 +71,10 @@ describe('PushPipe', () => {
7171
expect(pushPipe.transform(of(42))).toBe(42);
7272
});
7373

74+
it('should return emitted value from passed promise without changing it', () => {
75+
expect(pushPipe.transform(Promise.resolve(42))).toBe(42);
76+
});
77+
7478
it('should return undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)', () => {
7579
expect(pushPipe.transform(of(42))).toBe(42);
7680
expect(pushPipe.transform(NEVER)).toBe(undefined);
@@ -148,6 +152,12 @@ describe('PushPipe', () => {
148152
expect(componentNativeElement.textContent).toBe(wrapWithSpace('42'));
149153
});
150154

155+
it('should emitted value from passed promise without changing it', () => {
156+
pushPipeTestComponent.value$ = Promise.resolve(42);
157+
fixturePushPipeTestComponent.detectChanges();
158+
expect(componentNativeElement.textContent).toBe(wrapWithSpace('42'));
159+
});
160+
151161
it('should return undefined as value when a new observable NEVER was passed (as no value ever was emitted from new observable)', () => {
152162
pushPipeTestComponent.value$ = of(42);
153163
fixturePushPipeTestComponent.detectChanges();

modules/component/src/core/cd-aware/cd-aware_creator.ts

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
import {
2+
asapScheduler,
23
EMPTY,
4+
isObservable,
35
NextObserver,
46
Observable,
7+
ObservableInput,
8+
scheduled,
59
Subject,
610
Subscribable,
711
Subscription,
812
} from 'rxjs';
913
import {
1014
catchError,
1115
distinctUntilChanged,
12-
filter,
1316
switchMap,
1417
tap,
1518
} from 'rxjs/operators';
1619

1720
export interface CdAware<U> extends Subscribable<U> {
18-
nextPotentialObservable: (value: any) => void;
21+
nextPotentialObservable: (
22+
value: ObservableInput<any> | null | undefined
23+
) => void;
1924
}
2025

2126
/**
@@ -32,12 +37,12 @@ export function createCdAware<U>(cfg: {
3237
resetContextObserver: NextObserver<void>;
3338
updateViewContextObserver: NextObserver<U | undefined | null>;
3439
}): CdAware<U | undefined | null> {
35-
const potentialObservablesSubject = new Subject<
36-
Observable<U> | undefined | null
37-
>();
38-
const observablesFromTemplate$ = potentialObservablesSubject.pipe(
39-
distinctUntilChanged()
40-
);
40+
const potentialObservablesSubject: Subject<
41+
ObservableInput<U> | undefined | null
42+
> = new Subject();
43+
const observablesFromTemplate$: Observable<
44+
ObservableInput<U> | undefined | null
45+
> = potentialObservablesSubject.pipe(distinctUntilChanged());
4146

4247
const rendering$ = observablesFromTemplate$.pipe(
4348
// Compose the observables from the template and the strategy
@@ -54,25 +59,30 @@ export function createCdAware<U>(cfg: {
5459
return EMPTY;
5560
}
5661

62+
const ob$: Observable<U> = isObservable(observable$)
63+
? (observable$ as Observable<U>)
64+
: scheduled<U>(observable$, asapScheduler);
65+
5766
// If a new Observable arrives, reset the value to render_creator
5867
// We do this because we don't know when the next value arrives and want to get rid of the old value
5968
cfg.resetContextObserver.next();
6069
cfg.render();
6170

62-
return observable$.pipe(
71+
return (ob$ as Observable<U>).pipe(
6372
distinctUntilChanged(),
6473
tap(cfg.updateViewContextObserver),
6574
tap(() => cfg.render()),
6675
catchError((e) => {
67-
console.error(e);
6876
return EMPTY;
6977
})
7078
);
7179
})
7280
);
7381

7482
return {
75-
nextPotentialObservable(value: Observable<U> | undefined | null): void {
83+
nextPotentialObservable(
84+
value: ObservableInput<U> | undefined | null
85+
): void {
7686
potentialObservablesSubject.next(value);
7787
},
7888
subscribe(): Subscription {

0 commit comments

Comments
 (0)