Tuesday, April 9, 2019

Angular learing important linlk (RxJS, Obserable, subscribe, subscription, ) switchmap, multiple time http call in Rxjs


how to use combine latest - you can pass as array or without array see the bellow example


combineLatest(
[this.FBD.iterateReferencesByChild('facilityData/' + this._currentFacility + '/tasks', 'parent/device', deviceId),
this.FBD.iterateReferencesByChild('facilityData/' + this._currentFacility + '/components', 'parent/device', deviceId),
this.FBD.iterateReferencesByChild('facilityData/' + this._currentFacility + '/attachments', 'parent/device', deviceId),
this.FBD.iterateReferencesByChild('facilityData/' + this._currentFacility + '/comments', 'parent/device', deviceId)]
).pipe(take(1)).subscribe((refArrays) => {
console.log("testing ",refArrays);
let referencePaths = [].concat.apply([], refArrays);
for (let i = referencePaths.length - 1; i >= 0; i--) {
references[referencePaths[i]] = null;
}
references['facilityData/' + this._currentFacility + '/devices/' + deviceId] = null;
this.logHelperService.markRemovedEvents(deviceId, '_deviceIndex');
Object.keys(references).forEach(element => {
// console.log(element);
const itemsRef = this.ngFireDb.list(element);
itemsRef.remove();
});
callback('done');
});


deleteDevice(deviceId, callback) {
let references = {};
combineLatest(
this.FBD.iterateReferencesByChild('facilityData/' + this._currentFacility + '/tasks', 'parent/device', deviceId),
this.FBD.iterateReferencesByChild('facilityData/' + this._currentFacility + '/components', 'parent/device', deviceId),
this.FBD.iterateReferencesByChild('facilityData/' + this._currentFacility + '/attachments', 'parent/device', deviceId),
this.FBD.iterateReferencesByChild('facilityData/' + this._currentFacility + '/comments', 'parent/device', deviceId)
).pipe(take(1)).subscribe((refArrays) => {
let referencePaths = [].concat.apply([], refArrays);
for (let i = referencePaths.length - 1; i >= 0; i--) {
references[referencePaths[i]] = null;
}
references['facilityData/' + this._currentFacility + '/devices/' + deviceId] = null;
this.logHelperService.markRemovedEvents(deviceId, '_deviceIndex');
Object.keys(references).forEach(element => {
// console.log(element);
const itemsRef = this.ngFireDb.list(element);
itemsRef.remove();
});
callback('done');
});
}




combine latest with another api call in pipe



getFacilitiesByUser(uid) {
return this.getFacilityRightsByUser(uid).snapshotChanges().pipe(
switchMap((data: any) => {
return combineLatest(
// of(data),
combineLatest(data.map(item => {
return this._getFacilityObject(item.key).snapshotChanges().pipe(map((action: any) => action))
}))
)
}),
map(([facData]) => {
return facData.map((item: any) => {
let obj: any = item.payload.toJSON();
return {
...obj,
'$id': item.key
};
});
})
)
}





debouncing
export class App {
  items: Array<string>;
  term = new FormControl();
  constructor(private wikipediaService: WikipediaService) {
    this.term.valueChanges
             .debounceTime(400)
             .subscribe(term => this.wikipediaService.search(term).then(items => this.items = items));
  }
}



toPromise()  // use obserable to promise() // I used in serverless async and await in s3 sdk
return this.jsonp
                .get('http://en.wikipedia.org/w/api.php?callback=JSONP_CALLBACK', { search })
                .toPromise()
                .then((response) => response.json()[1]);
fromPromise()  /// it use in obserable from promise

VVVI ( multiple time http call)

return new BehaviorSubject(null).pipe(
switchMap(size => this.ngFireDb.list(this.FBD.components(parent.facility), ref => ref.child(parent.component)).snapshotChanges()),
map((sn: any) => {
sn.map(item =>{
if(item.key == 'name'){
retString = item.payload.toJSON();
// console.log('name', item.payload.toJSON());
}
});
return retString;
}),
switchMap(retString => this.ngFireDb.list(this.FBD.devices(parent.facility), ref => ref.child(parent.device)).snapshotChanges()),
map((sn: any) => {
let devicename ='';
sn.map(item =>{
if(item.key == 'name'){
devicename = item.payload.toJSON();
}
});
return devicename+ ' - ' + retString;
})
);










No comments:

Post a Comment

Autoboxing and Unboxing

  Autoboxing  is the automatic conversion that the Java compiler makes between the primitive types and their corresponding object wrapper cl...