简体   繁体   中英

Reactive javascript - convert ajax calls to Bacon.js stream with pagination

How can I convert calls to server API, with pagination support, to a Bacon.js / RxJs stream?

For the pagination I want to be able to store the last requested item-index, and ask for the next page_size items from that index to fill the stream.

But I need the 'load next page_size items' method to be called only when all items in stream already been read.

Here is a test that I wrote:

var PAGE_SIZE = 20;
var LAST_ITEM = 100;
var FIRST_ITEM = 0;

function getItemsFromServer(fromIndex) {
    if (fromIndex > LAST_ITEM) { 
        return [];
    }

    var remainingItemsCount = LAST_ITEM-fromIndex;
    if (remainingItemsCount <= PAGE_SIZE) {
        return _.range(fromIndex, fromIndex + remainingItemsCount);
    }
    return _.range(fromIndex, fromIndex + PAGE_SIZE);
}


function makeStream() {
    return Bacon.fromBinder(function(sink) {
        var fromIndex = FIRST_ITEM;

        function loadMoreItems() {
            var items = getItemsFromServer(fromIndex);
            fromIndex = fromIndex + items.length;
            return items;
        }

        var hasMoreItems = true;

        while (hasMoreItems) {
            var items = loadMoreItems();
            if (items.length < PAGE_SIZE) { hasMoreItems = false; }
            _.forEach(items, function(item) { sink(new Bacon.Next(item)); });
        }        

        return function() { console.log('done'); };
    });
}

makeStream().onValue(function(value) {
    $("#events").append($("<li>").text(value))
});

http://jsfiddle.net/Lc2oua5x/10/

Currently the 'getItemsFromServer' method is only a dummy and generate items locally. How to combine it with ajax call or a promise that return array of items? and can be execute unknown number of times (depends on the number of items on the server and the page size).

I read the documentation regarding Bacon.fromPromise() but couldn't manage to use it along with the pagination.

You need to use flatMap to map pages to streams created with Bacon.fromPromise . Here is a working example, where I use the jsfiddle echo endpoint ( it sends the same data back )

Bacon.sequentially(0, _.range(0,5))
.map(toIndex)
  .flatMap(loadFromServer)
  .onValue(render)

function toIndex(page) {
    return page * PAGE_SIZE
}

function loadFromServer(index) {
    var response = getItemsFromServer(index)
    return Bacon.fromPromise($.ajax({
      type: 'POST',
      dataType: 'json',
      url: '/echo/json/',
      data : { json: JSON.stringify( response ) }
    }))
}

function render(items) {
    items.forEach(function(item) {
        $("#events").append($("<li>").text(item))
    })
}

http://jsfiddle.net/1eqec9g3/2/

Note: this code relies on responses coming from the server in the same order they were sent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM