Observable wrapping
Wrapping something in an observable means we take something that is NOT an Observable and turn it into one, so it can play nice with other Observables. It also means that it can now use Operators.
Three things we need to do here emit data
, handle errors
and close the stream
if(request.status === 200) {
observer.next( request.response ) // emit data
}
Handle potential errors
else {
observer.error('error happened');
}
and
if(request.status === 200) {
observer.next( request.response )
observer.complete() // close stream, as we don't expect more data
}
console.clear();
const { Observable } = Rx;
const speechRecognition$ = new Observable(observer => {
const speech = new webkitSpeechRecognition();
observer.next(event);
observer.complete();
};
speech.start();
return () => {
speech.stop();
}
});
const say = (text) => new Observable(observer => {
const utterance = new SpeechSynthesisUtterance(text);
utterance.onend = (e) => {
observer.next(e);
observer.complete();
};
speechSynthesis.speak(utterance);
});
const button = document.querySelector("button");
const heyClick$ = Observable.fromEvent(button, 'click');
heyClick$
.switchMap(e => speechRecognition$)
.map(e => e.results[0][0].transcript)
switch (text) {
case 'I want':
return 'candy';
case 'hi':
case 'ice ice':
return 'baby';
case 'hello':
case 'make me a sandwich':
case 'get me a sandwich':
return 'do it yo damn self';
case 'why are you being so sexist':
return 'you made me that way';
default:
return `I don't understand: "${text}"`;
}
})
.concatMap(say)
.subscribe(e => console.log(e));
Speech recognition stream
This essentially sets up the speech recognition API. We wait for one response and after that we complete the stream, much like the first example with AJAX.
Note also that a function is defined for cleanup
return () => {
speech.stop();
}
so that we can call speechRecognition.unsubscribe()
to clean up resources
const say = (text) => new Observable(observer => {
const utterance = new SpeechSynthesisUtterance(text);
utterance.onend = (e) => {
observer.next(e);
observer.complete();
};
});
main stream hey$
Logic should be read as followsheyClick$
is activated on a click on a button.speechRecognition
is listening for what we say and sends that result into heyClick$
where the switching logic determines an appropriate response that is uttered by say
Observable.
all credit due to @ladyleet and
One easier Ajax wrapping and one a little more advanced Speech API has been wrapped into an Observable. The mechanics are still the same though:
1) where data is emitted, add a call to next()
2) if there is NO more data to emit call complete
3) if there is a need for it, define a function that can be called upon unsubscribe()
4) Handle errors through calling in the appropriate place. (only done in the first example)