Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

rxjs - Discard future returning observables

So here is my observable code:

  var suggestions =
        Rx.Observable.fromEvent(textInput, 'keyup')
          .pluck('target','value')
          .filter( (text) => {
              text = text.trim();
              if (!text.length) // empty input field
              {
                  this.username_validation_display("empty");
              }
              else if (!/^\w{1,20}$/.test(text))
              {
                  this.username_validation_display("invalid");
                  return false;
              }
              return text.length > 0;
          })
          .debounceTime(300)
          .distinctUntilChanged()
          .switchMap(term => {
              return $.ajax({
                type: "post",
                url: "src/php/search.php",
                data: {
                  username: term,
                  type: "username"
                }
              }).promise();
            }
          );
  suggestions.subscribe(
    (r) =>
    {
        let j = JSON.parse(r);
        if (j.length)
        {
            this.username_validation_display("taken");
        }
        else
        {
            this.username_validation_display("valid");
        }
    },
    function (e)
    {
        console.log(e);
    }
  );

The problem I have is when the input is empty I have a another piece of code that basically returns a 'error: empty input' but it gets overridden by the returning observable. So I was wondering if there was a way to disregard all observables if the text.length is 0, but also re-subscribe when the text length isn't zero.

I've thought about unsubscribe but don't know where to fit it in to try.

like image 599
A. L Avatar asked Aug 22 '17 06:08

A. L


People also ask

When to use Observable vs Subject?

In comparison to a regular Observable, a Subject allows values to be multicasted to many Observers. A Subject and its subscribers have a one-to-many relationship. A Subject can be an Observable as well as an Observer. They hold a registry of many listeners to multiple Observables.

Can you describe Observables and their purpose?

Observables provide support for passing messages between parts of your application. They are used frequently in Angular and are a technique for event handling, asynchronous programming, and handling multiple values.

Is Observable is async?

An observable produces values over time. An array is created as a static set of values. In a sense, observables are asynchronous where arrays are synchronous. In the following examples, → implies asynchronous value delivery.


1 Answers

Maybe this would work? The idea is to incorporate empty input in the switchmap so the current observable is switched off i.e. discarded also in that case.

  var suggestions =
        Rx.Observable.fromEvent(textInput, 'keyup')
          .pluck('target','value')
          .filter( (text) => {
              text = text.trim();
              if (!text.length) // empty input field
              {
                  this.username_validation_display("empty");
              }
              else if (!/^\w{1,20}$/.test(text))
              {
                  this.username_validation_display("invalid");
                  return false;
              }
          })
          .debounceTime(300)
          .distinctUntilChanged()
          .switchMap(text=> {
              if (text.length > 0) {
              return $.ajax({
                type: "post",
                url: "src/php/search.php",
                data: {
                  username: text,
                  type: "username"
                }
              }).promise()}
              return Rx.Observable.empty();
            }
          );
like image 69
user3743222 Avatar answered Sep 17 '22 20:09

user3743222