Stopping a pipeline being triggered again until it has completed

18 views
Skip to first unread message

tom....@gmail.com

unread,
Jul 13, 2015, 5:44:55 AM7/13/15
to bac...@googlegroups.com
I'm building an infinite scroll thing and down want it to be possible for a get request to be made to the server if one is already in process. My current solution looks like this:

let loading = false;

const pageUrl = page => '/backend.php?action=load_feed_page&page=' + page;

const pageNumber = loadEvents
.filter(() => !loading)
.doAction(() => {
loading = true;
showSpinner();
})
.scan(0, page => page + 1);

const nextPageUrl = pageNumber.map(pageUrl);

const loadedItems = nextPageUrl
.flatMapLatest(url => Bacon.fromPromise(request.get(url)))
.scan([], (newItems, current) => current.concat(newItems));

loadedItems
.onValue((items) => {
loading = false;
hideSpinner();
showItems(items);
});

What I don't like is the mutable state stored in the loading variable. I'm sure there's a much better way to do this. Can anyone suggest any improvements?

Many thanks!
Reply all
Reply to author
Forward
0 new messages