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

    1. if(request.status === 200) {
    2. observer.next( request.response ) // emit data
    3. }

    Handle potential errors

    1. else {
    2. observer.error('error happened');
    3. }

    and

    1. if(request.status === 200) {
    2. observer.next( request.response )
    3. observer.complete() // close stream, as we don't expect more data
    4. }
    1. console.clear();
    2. const { Observable } = Rx;
    3. const speechRecognition$ = new Observable(observer => {
    4. const speech = new webkitSpeechRecognition();
    5. observer.next(event);
    6. observer.complete();
    7. };
    8. speech.start();
    9. return () => {
    10. speech.stop();
    11. }
    12. });
    13. const say = (text) => new Observable(observer => {
    14. const utterance = new SpeechSynthesisUtterance(text);
    15. utterance.onend = (e) => {
    16. observer.next(e);
    17. observer.complete();
    18. };
    19. speechSynthesis.speak(utterance);
    20. });
    21. const button = document.querySelector("button");
    22. const heyClick$ = Observable.fromEvent(button, 'click');
    23. heyClick$
    24. .switchMap(e => speechRecognition$)
    25. .map(e => e.results[0][0].transcript)
    26. switch (text) {
    27. case 'I want':
    28. return 'candy';
    29. case 'hi':
    30. case 'ice ice':
    31. return 'baby';
    32. case 'hello':
    33. case 'make me a sandwich':
    34. case 'get me a sandwich':
    35. return 'do it yo damn self';
    36. case 'why are you being so sexist':
    37. return 'you made me that way';
    38. default:
    39. return `I don't understand: "${text}"`;
    40. }
    41. })
    42. .concatMap(say)
    43. .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

    1. return () => {
    2. speech.stop();
    3. }

    so that we can call speechRecognition.unsubscribe() to clean up resources

    1. const say = (text) => new Observable(observer => {
    2. const utterance = new SpeechSynthesisUtterance(text);
    3. utterance.onend = (e) => {
    4. observer.next(e);
    5. observer.complete();
    6. };
    7. });

    main stream hey$

    Logic should be read as follows
    heyClick$ 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)