Search code examples
htmlangularangular2-services

Need to get large chunk of data from service to component in Angular 4


I need to pass a very large object to my component from service. Since the object is very large i am intending to divide the object into chunks. How can i send the service 'getData()' request at intervals to get the data chunk by chunk? Basically i don't want the data to come from service at once, but in chunks. For instance if data is like this - {'x':'1','y':2,'z':3}, then data to to come for x first, then y and so on. Thanks in advance for any heads up.


Solution

  • In this case RxJs partition operator might help. Assuming 'large object' is nested object with keys you can implement some logic to return in a sequence you want.

    const obj = {fist: {name: 'joe', age: 30}, second: {name: 'doe', age: 40}};
    //Turn it into iterable
    const array = Object.entries(obj);
    // make an observable
    const source = from(array);
    // Logic for how to split, depends on your obj structure
    const [under, over] = source.pipe(partition(val => val[1].age < 40));
    // result
    under.subscribe(a => console.log("Under 30: " + a));
    over.subscribe(a => console.log("Over 30: " + a));