2017-06-22 7 views
0

Immutable.jsのobservableとMap構造を持つ同期メカニズムを実装しようとしています。Observableとして不変のマップ構造を使用する

Mapは観測可能にすることができないため、または私が間違った方法で行う可能性があります。

私はRxドキュメントを調べようとしましたが、ちょうど、戻り値、から、...のいずれもマップには適していないようです。

私が必要とするのは、自分がMapを購読コールバックに入れる前に(http.GETから取得した値で)待つことです。


import {List, Map} from 'immutable'; 
import {Observable} from 'rxjs/Observable'; 
... 

processNewTopology(topology: List<Endpoint>): Observable<Map<string, any>> { 

    let ip: string = JSON.stringify(topology.get(0).ueIpAddress); 

    //this function is just a http GET that returns an osbervable string (imsi) 
    this.apiService.getImsiFromAAA(ip).subscribe(
      imsi => myMap = this.reduceEndpointsToMap(imsi, topology), 
      error => this.errorMessage = <any>error 
     ); 

    return myMap; // I need in some way to convert my map into an obervable 

    } 


private reduceEndpointsToMap(imsi: string, topology: List<Endpoint>): Map<string, any> { 
    // this function take the imsi and a list point and build the map and return it 
    // the imsi is provided by http.GET 
} 

だから、別のクラスでは、私は地図を取得するためにprocessNewTopologyを呼び出します。 は、私が表示アクション

this.topologyService.processNewTopology(endpoints).subscribe(
      myMap => { 
      // here I want to access the content of myMap to display the new topo 
      } 
      ... 
     ); 

答えて

0

あなたはES6プロミスとしてObserableを使用していることを実行する前に、私のマップを持っている必要があります。最善の解決策は、apiサービスからのhttp要求をPromiseにラップすることです。 http要求が完了したら、結果を簡単に解決できます。

サービス:

class TopologyService { 

    private apiService: any; 

    ... 

    public processNewTopology(topology: List<Endpoint>): Promise<Map<string, any>> { 
     let ip = JSON.stringify(topology.get(0).ueIpAddress); 

     return new Promise((resolve, reject) => { 
      this.apiService.getImsiFromAAA(ip).subscribe(
       response => resolve(this.reduceEndpointsToMap(response, topology)), 
       error => reject(error) 
      ); 
     }); 
    } 

    private reduceEndpointsToMap(imsi: string, topology: List<Endpoint>): Map<string, any> { 
     ... 
    } 

    ... 
} 

用法:

topologyService.processNewTopology(endpoints) 
    .then(value => { 
     // do something 
    }) 
    .catch(err => { 
     // error occured 
    }); 
+0

ありがとうございました!出来た。 – Charles

関連する問題