I have a case in my angular application where I need to change the observable if the initial value was NEVER:
export class MyComponent {
@Input() isInitiallyOpen$?: Observable<boolean> = NEVER;
constructor(private myService: MyService) {
this.isOpen$ = this.isInitiallyOpen$ ?? this.myService.isOpen$; // NOT WORKING
// this.isOpen$ = NEVER at this point, and not this.myService.isOpen$
}
}
I know that rxjs's NEVER is basically new Observable<never>(noop()). But my question is, is there any way to know that isInitiallyOpen$ stream is actually holding a NEVER value?
If you check the NEVER Observable at the GitHub project you can see that its basically an empty Observable with a noop function:
export const NEVER = new Observable<never>(noop)
Therefore you can check if the variable equals NEVER. Below I made some tests for you to work with.
const { NEVER, Observable, noop } = rxjs;
const foo$ = NEVER
const bar$ = NEVER
console.log("foo$ === bar$: ", foo$ === bar$) // true
console.log("foo$ === NEVER: ", foo$ === NEVER) // true
console.log("foo$ === Observable: ", foo$ === Observable) // false
console.log("foo$ === new Observable(): ", foo$ === new Observable()) // false
console.log("foo$ === Observable(() => {}): ", foo$ === new Observable(() => {})) // false
console.log("foo$ === Observable(noop): ", foo$ === new Observable(noop)) // false
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/7.4.0/rxjs.umd.min.js"></script>