如果条件不匹配,如何忽略Observable中的事件?

时间:2019-05-14 19:38:59

标签: observable rxjs6

我想放置一个条件,以便在嵌套管道条件中不满足该条件的情况下忽略操作事件。

如果我只返回export const getDataEpic: (action$, state$, { apolloClient, services }: { apolloClient: ApolloClient<any>, services: any }) => any = (action$, state$, { apolloClient, services }) => action$.pipe(ofType('TEST)).pipe(map(x => x)) .pipe( switchMap((action) => of([true, false, true])) .pipe( map(settings => ({ action, config: settings})), // true or false switchMap(value=> { if(value.config) return of([1]) } else { // if condition not met then don't emit anything return ignoreElements(); } }), ), ), groupBy(value => value.action.payload.model.resource), mergeMap(ngroup => ngroup ,则会导致返回值不流的错误。

const int pingPin = 11; // Trigger Pin of Ultrasonic Sensor
const int echoPin = 12; // Echo Pin of Ultrasonic Sensor
const int ml1=8;
const int ml2=7;
const int mr1=10;
const int mr2=9;
void setup() {
  pinMode(ml1,OUTPUT);
  pinMode(ml2,OUTPUT);
  pinMode(mr1,OUTPUT);
  pinMode(mr2,OUTPUT);
  pinMode(pingPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600); // Starting Serial Terminal
 }

 void loop() {
   long duration;
   long distance;
   digitalWrite(pingPin, LOW);
   delayMicroseconds(2);
   digitalWrite(pingPin, HIGH);
   delayMicroseconds(10);
   digitalWrite(pingPin, LOW);
   duration = pulseIn(echoPin, HIGH);
   distance=duration*0.034/2;
   Serial.print(distance);
   Serial.print("cm");
   Serial.println();
  if(distance>8)
   {
    digitalWrite(ml1,LOW);
    digitalWrite(ml2,HIGH);
    digitalWrite(mr1,LOW);
    digitalWrite(mr2,HIGH);
    }
  else{
   digitalWrite(ml1,HIGH);
   digitalWrite(ml2,LOW);
   digitalWrite(mr1,LOW);
   digitalWrite(mr2,HIGH);
   delay(1000);
   digitalWrite(ml1,LOW);
   digitalWrite(ml2,HIGH);
   digitalWrite(mr1,LOW);
   digitalWrite(mr2,HIGH);

   }


   }

1 个答案:

答案 0 :(得分:0)

错误是因为您返回的是 operator函数,而不是可观察的函数。如果您需要一个不发射任何物品的可观察物,则应该返回EMPTY

import { EMPTY } from 'rxjs'

...
switchMap(value => {
  if (value.config)
    return of([1])
  } else {
    // if condition not met then don't emit anything
    return EMPTY
  }
})
...