2016-07-22 3 views
0

컨텍스트를 반환 : 타이프에 Angular2로 작성된 응용 프로그램, + rxjs 5.RxJs 관측 가능한 <T>의 배열에 '지도',하지만 조건에게 T 매치 즉시

편집 : 나는 것을 precising 해요 상대적으로 Rx 라이브러리에 새롭고 일을 "관용적 인 방식으로"수행해야하는 방식입니다. 그리고 네, 그래서 게시하기 전에 문서에서 몇 가지 단서를 찾으려고 노력했다.

난이있다 :

class Result { constructor(public inError: boolean) { } } 

const checks : Array<() => Observable<Result>> = [...]; 

이 함수의 배열은을 관찰하는 의지를 돌려 각 함수 결과 개체를 포함한다. ...

  • 내가 차례로 각 함수를 호출하여 기본적으로 Array<Observable<Result>>에 를이 배열을 '지도'할
  • ...하지만 '는 휴식 할 :

    내가 원하는 것은 첫 번째 Result.inError이 사실이라면 즉시 '지도'로 표시됩니다.

은 내가 Observable 인의 이연 자연이 나에게 수수께끼하는 등 reduce, takeWith, contains ... 만지작, desesperalty 붙어있어.

도움이 될 것입니다. 당신의 관찰 가능한 동기식 작업을 수행하는 경우

+0

그리고 그는 내 질문을 downvoted 이유는 아마도 downvoter는 설명 할 수 있을까? –

답변

0

, 당신은 간단하게이 작업을 수행 할 수 있습니다 비동기 관찰자의 경우

const results = []; 
for (const i = 0; i < checks.length; i +=1) { 
    checks[i]().subscribe(result => if (result.inError) { 
     break; 
    } else { 
     results.push(checks[i]()); 
    }); 
} 
// Results observables accessible here. 

을 :

어떤 혜택을 제공하지 않는 중 하나를 수행
const results = []; 
function callObservable(index) { 
    checks[index]().subscribe(result => if (result.inError) { 
      // Results observables accessible here. 
     } else { 
      results.push(checks[i]()); 
      callObservable(index + 1); 
     }) 
} 
callObservable(0); 

, 그래도. 관측 값은 결과 배열에 도달하기 전에 이미 호출되었거나이 배열에서 다시 호출 된 경우 다른 값을 갖습니다.

0

은 점점 더 파고 후, 여기에 솔루션입니다 :

// Some class that will be contained in my observables 
class Result { constructor(public inError: boolean) { } } 

// An array of Observables that will emit those classes instances 
// the thunk is just here to lazy instantiate the Result classes 
// only when needed 
const oResults : Array<() => Observable<Result>> = [ 
    Rx.Observable.of(() => new Result(false)), 
    Rx.Observable.of(() => new Result(false)), 
    Rx.Observable.of(() => new Result(true)), 
    Rx.Observable.of(() => new Result(false)) 
]; 

// An home made (found on SO) INCLUSIVE 'takeWhile' version 
const takeWhileInclusive(source, predicate){ 
    return source.publish(co => co.takeWhile(predicate) 
     .merge(co.skipWhile(predicate).take(1))); 
} 

// Let's filter out things as expected 
const res = takeWhileInclusive(
    Rx.Observable.merge(oResults).map(x => x()), 
    x => !x.inError 
); 

// I can now subscribe to the resulting stream and will only get 
// all the first Results that are false AND the first following result that 
// is true 
res.subscribe(next => console.info("Next result", next)); 
관련 문제