Warning... Very long message...
So I changed one of the observables to a promise because it could work as a
promise as well and all is working well now, as far as I can tell.
Do you see anything wrong with my approach?
// single http get call turning into a promise
getDataP(): Promise<Array<string>> {
return this.http.get(*url*).toPromise();
}
// one observable that reads a configFile
getConfigurationSettings(url) : Observable<string[]> {
return this.http.get(url)
.map((res:Response) => {
return res.json()
})
.catch((error:any) => Observable.throw(error.json().error ||
'Failed to get flowsheet configuration settings'));
}
// another http get call as an observable...
// this also can be made into a promise but it is fine for now
// just one http get call
askForPatientContextWSAddress() : Observable<string[]> {
return this.http.get(this.patientContextProviderURL)
.map((res:Response) => {
return ['ws://localhost:3006'];
})
.catch((error: Response | any) => {
if (error instanceof Response && error.status !== 200) {
console.log('failed to get valid response code:' + error.status
+ 'error text: ' + error.text());
}
return Observable.throw(error);
})
}
//code to call the http get call every 15 sec
startGettingData () {
setInterval(
console.log('startGettingData');
this.getDataP().then(function (stringArray) {
console.log(stringArray);
return stringArray;
}), 15000);
}
// observable to listen for websocket messages
// and process them
processWebscoketMessages(): Observable<any>{
console.log('listenForContextChanges');
return Observable.fromEvent(this.contextWS,'message')
.map(res=>"From WS: " + res.data);
}
// just a test observable code to generate a value every second
var numbers = Observable.interval(1000);
// the final combination of promises and observables...
this.getConfigurationSettings()
.do(config => {
// nothing async
console.log('we got the configuration settings.');
this.configSettings = config;
})
.switchMap(res => this.askForPatientContextWSAddress())
.do(x => {
// nothing async
// construct the webscoket...
this.contextWS = new WebSocket(wsAdress);
this.contextWS.onopen = (evt) => {
this.contextWS.send("Hello World");
};
})
.switchMap(r => this. processWebscoketMessages())
// in the following do block/side effect, I start the timer based call to
the
// http get promise.
.do (() => this.startGettingData())
.merge(numbers)
.subscribe(
x => {
console.log(*'got data');*
* // process the data, synchronous.* },
err => {
// Log errors if any
console.log(err);
},
() => console.log('app exiting'));
How does the following structure look like?
On Thursday, April 20, 2017 at 10:25:28 PM UTC-7, Sander Elias wrote:
>
> Hi Reza,
>
> Well, as usual, it depends on what you need to do. But if you have indeed
> 2 different observables, that result in 2 different things, that need
> different handling, then you have a reason to use multiple subscribers.
> But be careful with side-effects. Observables work great if you use pure
> functions. But if you mix in side-effect, debugging them becomes a
> nightmare. If you need side-effect, make sure they are putting things out,
> not pulling things in. Does that make sense to you?
>
> Regards
> Sander
>
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.