Angular: RxJS Observable only updating on timeout
Date : March 29 2020, 07:55 AM
With these it helps Updates coming from the startScanWithOptions() method are likely running outside the Angular zone, meaning that the asynchronous event is not monkey-patched by Zone.js, like setTimeout() is. In startSelection(), manually trigger change detection inside your subscribe() callback: export class MyComponent {
constructor(private _cdRef:ChangeDetectorRef) {}
private startSelection() {
console.log('Scan Starting');
this.connectionSource = this.connection.start();
this.connectionSub = this.connectionSource.subscribe((result) => {
this.test.push(result.rssi);
console.log(this.test);
this._cdRef.detectChanges(); // <---------
});
}
|
Rxjs using Observable.from() string and prevent Observable being created many times
Date : March 29 2020, 07:55 AM
should help you out If you want to use observables, but don't want to create one from an event, you can create a Subject and can call its next method from within your search: import { Subject } from 'rxjs/Subject';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/debounceTime';
import 'rxjs/add/operator/distinctUntilChanged';
import 'rxjs/add/operator/switchMap';
var searchSubject = new Subject();
.filter(value => value.length > 3)
.debounceTime(300)
.distinctUntilChanged()
.switchMap(searchValue => {
// ajax call
return Promise.resolve(data)
})
.subscribe(data => {
// Do something with the data
});
function search(input) {
searchSubject.next(input);
}
|
How to set a timeout on a RxJS Observable with a cache throught `.publishReplay()`?
Date : March 29 2020, 07:55 AM
I hope this helps . I think I should update the example and add this use-case because this is a pretty common situation (anyway, I'm glad you find it useful!). When the Observable returned from mockDataFetch() sends error/complete notification the Subject inside marks itself as stopped (see explanation Rx.Subject loses events) so it won't reemit any items. You can ideally catch all errors with catch() operator inside mockDataFetch(): function mockDataFetch() {
return Observable.of(counter++)
.delay(Math.floor((Math.random() * 100) + 1))
.timeout(50)
.catch(err => Observable.of('This request is broken.'));
}
Response 0: This request is broken.
Response 50: This request is broken.
Response 200: This request is broken.
Response 1200: 2
Response 1500: 2
Response 3500: This request is broken.
|
why does rxjs observable event fire twice after timeout?
Date : March 29 2020, 07:55 AM
seems to work fine I see in your stackblitz that it only fired once. The only way it could fire twice is if for some reason you had called the method twice or you are using a hot observable to emit every 2000 ms.
|
Errors after migrated Angular and RxJS 5 to 6 - Type 'Observable<{}>' is not assignable to type 'Observable<...
Date : March 29 2020, 07:55 AM
hop of those help? I can probably change the interface {count:number, next:string, previous:string, results: any[]} to simply any` import { throwError, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
// Create source Observable<string> that emits an error
const source : Observable<string> = throwError('This is an error!');
// Gracefully handle error, returning observable with error message
// Notice that the type contract of the source is mantained
const example : Observable<string> = source.pipe(catchError(val => of(`I caught: ${val}`)));
// Output: 'I caught: This is an error'
// Notice that the next, and not the error callback, is invoked
const subscribe = example.subscribe(
val => console.log(val),
error => console.log("Something exploded: ", error));
import { throwError, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
// Create source Observable<string> that emits an error
const source : Observable<string> = throwError('This is an error!');
// Gracefully handle error, returning observable with error message
// Notice that by mapping the error to a fake notification of another type, the new
// stream extends the type contract of the source
const example : Observable<string | number> = source.pipe(catchError(val => of(1)));
// Output: 'I caught: This is an error'
// Notice that the next, and not the error callback, is invoked
const subscribe = example.subscribe(
val => console.log(val),
error => console.log("Something exploded: ", error));
import { throwError, of } from 'rxjs';
import { catchError } from 'rxjs/operators';
// Create source Observable<string> that emits an error
const source : Observable<string> = throwError('This is an error!');
// Ungracefully handle error, re-throwing an object
const example : Observable<string> = source.pipe(catchError(error => throwError({message: 'Error caught', error})));
// Output: 'Something exploded: '
// Notice that the error, and not the next callback, is invoked
const subscribe = example.subscribe(
val => console.log(val),
error => console.log("Something exploded: ", error));
list(params?): Observable<PaginatedList | any>;
|