Programing

여러 키를 구별할 때까지 KeyChanged와 비교하는 방법?

c10106 2022. 3. 11. 22:18
반응형

여러 키를 구별할 때까지 KeyChanged와 비교하는 방법?

나는 아직 rxjs를 배우고 있고, 운영자 별개의 EllowChanged를 위한 사용자 정의 비교 함수를 쓰는 방법에 약간 집착하고 있다.

단일 키에 적합한 DifferentLayKeyChanged를 사용하는 방법을 알아보았다.하지만 비교해야 할 열쇠가 두 개 있어.

현재 방출된 값을 마지막 방출된 값과 비교하기 위해 스캔 연산자를 구제해야 할 것 같아...?

좋아, 여기 내 암호야.구글 지도에서 지도 센터 변경 사항을 스트리밍하고 있어.지도 센터 GeoLocation이 매우 정확할 필요는 없어, 그래서 나는 구글 지도에서 반환되는 십진법 대부분을 반올림하고 있어.

searchStream$
  .map((value)=>{
  return {
    lat: round(value[1].lat, 1),
    lng: round(value[1].lng, 1)
  }
}).distinctUntilKeyChanged('lat')
  .do((position)=>{console.log(position)})
  .subscribe((position)=>{ this._store.dispatch(new QueryUpdateGeoPositionAPIAction({latitude: position.lat, longitude: position.lng})) });

그렇다면 제 질문으로 돌아가서 어떻게 두 속성(lat & lng)을 비교해서 값 중 하나가 바뀌었을값만 방출하도록 할 수 있을까?

도와줘서 고마워!

나는 같은 문제를 겪고 있었고 이 상황은 의사들이 다루지 않는다.

여기에 더해질 것이다.distinctUntilKeysChanged교환원의그냥 "감시"할 수 있는 아무 키나 건네줘.

const {
  Observable,
  Subject
} = Rx;

Observable.prototype.distinctUntilKeysChanged = function(...keys) {
  return this.distinctUntilChanged((old, current) =>
    // if no value changed,
    // the array will only have true values,
    // includes(false) will be false,
    // convert to oposite (!),
    // returns true;
    // => true = do nothing

    // if any value changed,
    // the array will include some false,
    // includes(false) will be true,
    // convert to oposite (!),
    // returns false;
    // => false = emit

    !keys
    .map(key => old[key] === current[key]) // converts to array of boolean
    .includes(false) // if any value changed
  );
};

const stream = new Subject();

stream
  .distinctUntilKeysChanged('prop', 'prop2')
  .subscribe(obj => console.log(obj));

// should log
stream.next({
  prop: 42,
  prop2: 54,
});

// should log
stream.next({
  prop: 12346,
  prop2: 54,
});

// shouldn't log because neither prop nor prop2 changed
stream.next({
  prop: 12346,
  prop2: 54,
});

// should log
stream.next({
  prop: 12346,
  prop2: 5454665654645,
});
<script src="https://unpkg.com/@reactivex/rxjs@5.0.3/dist/global/Rx.js"></script>

이것의 유일한 단점은 사용자 정의 비교 함수를 지정할 수 없다는 것이다.만약 당신이 하나를 지정하기를 원한다면, 당신은 이것을 대신 사용할 수 있다 (이것은 보다 구현과 유사하다..distinctUntilKeyChanged여기서 첫 번째 주장이 핵심이고 두 번째 논거는 비교 함수)이다.이 키의 배열은 첫 번째 키가 별도의 인수로서 키를 가져간다는 점에 유의하십시오.

const {
  Observable,
  Subject
} = Rx;

Observable.prototype.distinctUntilKeysChanged = function(keys, compare) {
  return this.distinctUntilChanged((old, current) =>
    // if no value changed,
    // the array will only have true values,
    // includes(false) will be false,
    // convert to oposite (!),
    // returns true;
    // => true = do nothing

    // if any value changed,
    // the array will include some false,
    // includes(false) will be true,
    // convert to oposite (!),
    // returns false;
    // => false = emit

    !keys
    .map(key => compare ? compare(old[key], current[key]) : old[key] === current[key]) // converts to array of boolean
    .includes(false) // if any value changed
  );
};

const stream = new Subject();

stream
  .distinctUntilKeysChanged(['prop', 'prop2'])
  .subscribe(obj => console.log(obj));

// should log
stream.next({
  prop: 42,
  prop2: 54,
});

// should log
stream.next({
  prop: 12346,
  prop2: 54,
});

// shouldn't log because neither prop nor prop2 changed
stream.next({
  prop: 12346,
  prop2: 54,
});

// should log
stream.next({
  prop: 12346,
  prop2: 5454665654645,
});
<script src="https://unpkg.com/@reactivex/rxjs@5.0.3/dist/global/Rx.js"></script>

그것이 유용하기를 바라다.

설명서의 RxJS 섹션에서:

RxJS에서 구별되는 연산자에는 두 가지 선택적 파라미터가 있다.

  1. 소스에서 방출되는 품목을 수신하고 두 품목을 구별하기 위해 비교할 때 품목 자체 대신 사용할 키를 반환하는 함수
  2. 두 개의 항목(또는 두 개의 키)을 수용하여 구별성을 비교하여 반환하는 기능false구별되는 경우(여기에서 자신의 기능을 제공하지 않을 경우 동등함수가 기본값임)

그래서 내가 보기엔 (시험 없이) 그냥 통과하면 되는 것 같다.

(a, b) => a.lat === b.lat && a.lon === b.lon 

RxJS 규약에 대해 잘 모르겠는데 어떻게 이 (두 번째 선택사항) 매개 변수를 통과시켜야 하는지.

참조URL: https://stackoverflow.com/questions/44660184/how-to-compare-multiple-keys-with-distinctuntilkeychanged

반응형