What is the correct way to share the result of an Angular Http network call in RxJs 5?

前端 未结 21 1280
广开言路
广开言路 2020-11-21 06:11

By using Http, we call a method that does a network call and returns an http observable:

getCustomer() {
    return          


        
相关标签:
21条回答
  • 2020-11-21 06:12

    rxjs 5.3.0

    I haven't been happy with .map(myFunction).publishReplay(1).refCount()

    With multiple subscribers, .map() executes myFunction twice in some cases (I expect it to only execute once). One fix seems to be publishReplay(1).refCount().take(1)

    Another thing you can do, is just not use refCount() and make the Observable hot right away:

    let obs = this.http.get('my/data.json').publishReplay(1);
    obs.connect();
    return obs;
    

    This will start the HTTP request regardless of subscribers. I'm not sure if unsubscribing before the HTTP GET finishes will cancel it or not.

    0 讨论(0)
  • 2020-11-21 06:13

    I starred the question, but i'll try and have a go at this.

    //this will be the shared observable that 
    //anyone can subscribe to, get the value, 
    //but not cause an api request
    let customer$ = new Rx.ReplaySubject(1);
    
    getCustomer().subscribe(customer$);
    
    //here's the first subscriber
    customer$.subscribe(val => console.log('subscriber 1: ' + val));
    
    //here's the second subscriber
    setTimeout(() => {
      customer$.subscribe(val => console.log('subscriber 2: ' + val));  
    }, 1000);
    
    function getCustomer() {
      return new Rx.Observable(observer => {
        console.log('api request');
        setTimeout(() => {
          console.log('api response');
          observer.next('customer object');
          observer.complete();
        }, 500);
      });
    }
    

    Here's the proof :)

    There is but one takeaway: getCustomer().subscribe(customer$)

    We are not subscribing to the api response of getCustomer(), we are subscribing to a ReplaySubject which is observable which is also able to subscribe to a different Observable and (and this is important) hold it's last emitted value and republish it to any of it's(ReplaySubject's) subscribers.

    0 讨论(0)
  • 2020-11-21 06:14

    rxjs version 5.4.0 (2017-05-09) adds support for shareReplay.

    Why use shareReplay?

    You generally want to use shareReplay when you have side-effects or taxing computations that you do not wish to be executed amongst multiple subscribers. It may also be valuable in situations where you know you will have late subscribers to a stream that need access to previously emitted values. This ability to replay values on subscription is what differentiates share and shareReplay.

    You could easily modify an angular service to use this and return an observable with a cached result that will only ever make the http call a single time (assuming the 1st call was successfull).

    Example Angular Service

    Here is a very simple customer service that uses shareReplay.

    customer.service.ts

    import { shareReplay } from 'rxjs/operators';
    import { Observable } from 'rxjs';
    import { HttpClient } from '@angular/common/http';
    
    @Injectable()
    export class CustomerService {
    
        private readonly _getCustomers: Observable<ICustomer[]>;
    
        constructor(private readonly http: HttpClient) {
            this._getCustomers = this.http.get<ICustomer[]>('/api/customers/').pipe(shareReplay());
        }
    
        getCustomers() : Observable<ICustomer[]> {
            return this._getCustomers;
        }
    }
    
    export interface ICustomer {
      /* ICustomer interface fields defined here */
    }
    

    Note that the assignment in the constructor could be moved to the method getCustomers but as observables returned from HttpClient are "cold" doing this in the constructor is acceptable as the http call will only every be made with the first call to subscribe.

    Also the assumption here is that the initial returned data does not get stale in the lifetime of the application instance.

    0 讨论(0)
  • 2020-11-21 06:14

    I wrote a cache class,

    /**
     * Caches results returned from given fetcher callback for given key,
     * up to maxItems results, deletes the oldest results when full (FIFO).
     */
    export class StaticCache
    {
        static cachedData: Map<string, any> = new Map<string, any>();
        static maxItems: number = 400;
    
        static get(key: string){
            return this.cachedData.get(key);
        }
    
        static getOrFetch(key: string, fetcher: (string) => any): any {
            let value = this.cachedData.get(key);
    
            if (value != null){
                console.log("Cache HIT! (fetcher)");
                return value;
            }
    
            console.log("Cache MISS... (fetcher)");
            value = fetcher(key);
            this.add(key, value);
            return value;
        }
    
        static add(key, value){
            this.cachedData.set(key, value);
            this.deleteOverflowing();
        }
    
        static deleteOverflowing(): void {
            if (this.cachedData.size > this.maxItems) {
                this.deleteOldest(this.cachedData.size - this.maxItems);
            }
        }
    
        /// A Map object iterates its elements in insertion order — a for...of loop returns an array of [key, value] for each iteration.
        /// However that seems not to work. Trying with forEach.
        static deleteOldest(howMany: number): void {
            //console.debug("Deleting oldest " + howMany + " of " + this.cachedData.size);
            let iterKeys = this.cachedData.keys();
            let item: IteratorResult<string>;
            while (howMany-- > 0 && (item = iterKeys.next(), !item.done)){
                //console.debug("    Deleting: " + item.value);
                this.cachedData.delete(item.value); // Deleting while iterating should be ok in JS.
            }
        }
    
        static clear(): void {
            this.cachedData = new Map<string, any>();
        }
    
    }
    

    It's all static because of how we use it, but feel free to make it a normal class and a service. I'm not sure if angular keeps a single instance for the whole time though (new to Angular2).

    And this is how I use it:

                let httpService: Http = this.http;
                function fetcher(url: string): Observable<any> {
                    console.log("    Fetching URL: " + url);
                    return httpService.get(url).map((response: Response) => {
                        if (!response) return null;
                        if (typeof response.json() !== "array")
                            throw new Error("Graph REST should return an array of vertices.");
                        let items: any[] = graphService.fromJSONarray(response.json(), httpService);
                        return array ? items : items[0];
                    });
                }
    
                // If data is a link, return a result of a service call.
                if (this.data[verticesLabel][name]["link"] || this.data[verticesLabel][name]["_type"] == "link")
                {
                    // Make an HTTP call.
                    let url = this.data[verticesLabel][name]["link"];
                    let cachedObservable: Observable<any> = StaticCache.getOrFetch(url, fetcher);
                    if (!cachedObservable)
                        throw new Error("Failed loading link: " + url);
                    return cachedObservable;
                }
    

    I assume there could be a more clever way, which would use some Observable tricks but this was just fine for my purposes.

    0 讨论(0)
  • 2020-11-21 06:15

    You could simply use ngx-cacheable! It better suits your scenario.

    The benefit of using this

    • It calls rest API only once, caches the response & returns the same for following requests.
    • Can call API as required after create/ update/ delete operation.

    So, Your service class would be something like this -

    import { Injectable } from '@angular/core';
    import { Cacheable, CacheBuster } from 'ngx-cacheable';
    
    const customerNotifier = new Subject();
    
    @Injectable()
    export class customersService {
    
        // relieves all its caches when any new value is emitted in the stream using notifier
        @Cacheable({
            cacheBusterObserver: customerNotifier,
            async: true
        })
        getCustomer() {
            return this.http.get('/someUrl').map(res => res.json());
        }
    
        // notifies the observer to refresh the data
        @CacheBuster({
            cacheBusterNotifier: customerNotifier
        })
        addCustomer() {
            // some code
        }
    
        // notifies the observer to refresh the data
        @CacheBuster({
            cacheBusterNotifier: customerNotifier
        })
        updateCustomer() {
            // some code
        }
    }
    

    Here's the link for more reference.

    0 讨论(0)
  • 2020-11-21 06:16

    Cacheable HTTP Response Data using Rxjs Observer/Observable + Caching + Subscription

    See Code Below

    *disclaimer: I am new to rxjs, so bear in mind that I may be misusing the observable/observer approach. My solution is purely a conglomeration of other solutions I found, and is the consequence of having failed to find a simple well-documented solution. Thus I am providing my complete code solution (as I would liked to have found) in hopes that it helps others.

    *note, this approach is loosely based on GoogleFirebaseObservables. Unfortunately I lack the proper experience/time to replicate what they did under the hood. But the following is a simplistic way of providing asynchronous access to some cache-able data.

    Situation: A 'product-list' component is tasked with displaying a list of products. The site is a single-page web app with some menu buttons that will 'filter' the products displayed on the page.

    Solution: The component "subscribes" to a service method. The service method returns an array of product objects, which the component accesses through the subscription callback. The service method wraps its activity in a newly created Observer and returns the observer. Inside this observer, it searches for cached data and passes it back to the subscriber (the component) and returns. Otherwise it issues an http call to retrieve the data, subscribes to the response, where you can process that data (e.g. map the data to your own model) and then pass the data back to the subscriber.

    The Code

    product-list.component.ts

    import { Component, OnInit, Input } from '@angular/core';
    import { ProductService } from '../../../services/product.service';
    import { Product, ProductResponse } from '../../../models/Product';
    
    @Component({
      selector: 'app-product-list',
      templateUrl: './product-list.component.html',
      styleUrls: ['./product-list.component.scss']
    })
    export class ProductListComponent implements OnInit {
      products: Product[];
    
      constructor(
        private productService: ProductService
      ) { }
    
      ngOnInit() {
        console.log('product-list init...');
        this.productService.getProducts().subscribe(products => {
          console.log('product-list received updated products');
          this.products = products;
        });
      }
    }
    

    product.service.ts

    import { Injectable } from '@angular/core';
    import { Http, Headers } from '@angular/http';
    import { Observable, Observer } from 'rxjs';
    import 'rxjs/add/operator/map';
    import { Product, ProductResponse } from '../models/Product';
    
    @Injectable()
    export class ProductService {
      products: Product[];
    
      constructor(
        private http:Http
      ) {
        console.log('product service init.  calling http to get products...');
    
      }
    
      getProducts():Observable<Product[]>{
        //wrap getProducts around an Observable to make it async.
        let productsObservable$ = Observable.create((observer: Observer<Product[]>) => {
          //return products if it was previously fetched
          if(this.products){
            console.log('## returning existing products');
            observer.next(this.products);
            return observer.complete();
    
          }
          //Fetch products from REST API
          console.log('** products do not yet exist; fetching from rest api...');
          let headers = new Headers();
          this.http.get('http://localhost:3000/products/',  {headers: headers})
          .map(res => res.json()).subscribe((response:ProductResponse) => {
            console.log('productResponse: ', response);
            let productlist = Product.fromJsonList(response.products); //convert service observable to product[]
            this.products = productlist;
            observer.next(productlist);
          });
        }); 
        return productsObservable$;
      }
    }
    

    product.ts (the model)

    export interface ProductResponse {
      success: boolean;
      msg: string;
      products: Product[];
    }
    
    export class Product {
      product_id: number;
      sku: string;
      product_title: string;
      ..etc...
    
      constructor(product_id: number,
        sku: string,
        product_title: string,
        ...etc...
      ){
        //typescript will not autoassign the formal parameters to related properties for exported classes.
        this.product_id = product_id;
        this.sku = sku;
        this.product_title = product_title;
        ...etc...
      }
    
    
    
      //Class method to convert products within http response to pure array of Product objects.
      //Caller: product.service:getProducts()
      static fromJsonList(products:any): Product[] {
        let mappedArray = products.map(Product.fromJson);
        return mappedArray;
      }
    
      //add more parameters depending on your database entries and constructor
      static fromJson({ 
          product_id,
          sku,
          product_title,
          ...etc...
      }): Product {
        return new Product(
          product_id,
          sku,
          product_title,
          ...etc...
        );
      }
    }
    

    Here is a sample of the output I see when I load the page in Chrome. Note that on the initial load, the products are fetched from http (call to my node rest service, which is running locally on port 3000). When I then click to navigate to a 'filtered' view of the products, the products are found in cache.

    My Chrome Log (console):

    core.es5.js:2925 Angular is running in the development mode. Call enableProdMode() to enable the production mode.
    app.component.ts:19 app.component url: /products
    product.service.ts:15 product service init.  calling http to get products...
    product-list.component.ts:18 product-list init...
    product.service.ts:29 ** products do not yet exist; fetching from rest api...
    product.service.ts:33 productResponse:  {success: true, msg: "Products found", products: Array(23)}
    product-list.component.ts:20 product-list received updated products
    

    ...[clicked a menu button to filter the products]...

    app.component.ts:19 app.component url: /products/chocolatechip
    product-list.component.ts:18 product-list init...
    product.service.ts:24 ## returning existing products
    product-list.component.ts:20 product-list received updated products
    

    Conclusion: This is the simplest way I've found (so far) to implement cacheable http response data. In my angular app, each time I navigate to a different view of the products, the product-list component reloads. ProductService seems to be a shared instance, so the local cache of 'products: Product[]' in the ProductService is retained during navigation, and subsequent calls to "GetProducts()" returns the cached value. One final note, I've read comments about how observables/subscriptions need to be closed when you're finished to prevent 'memory leaks'. I've not included this here, but it's something to keep in mind.

    0 讨论(0)
提交回复
热议问题