diff --git a/src/groups/list.js b/src/groups/list.js index bb7fc1bf36bf2a043d3f3a76c0ac2820792b51c1..02ca33c3cac726f5a8650c1ca8bc397c25c3f0cb 100644 --- a/src/groups/list.js +++ b/src/groups/list.js @@ -20,24 +20,7 @@ export default class GroupList { constructor() { this.ctrl = new DatalistController('groups', {}, ['name']); this.data = []; - - this.ctrl.getPageData(1).then((firstPage) => { - const pages = { 1: firstPage }; - // now fetch all the missing pages - console.log(this.ctrl.totalPages); - Array.from(new Array(this.ctrl.totalPages - 1), (x, i) => i + 2).forEach((pageNum) => { - this.ctrl.getPageData(pageNum).then((newPage) => { - pages[pageNum] = newPage; - // collect all the so-far loaded pages in order (sorted keys) - // and flatten them into 1 array - this.data = [].concat(...Object.keys(pages).sort().map(key => pages[key])); - m.redraw(); - }); - }); - // see above - this.data = [].concat(...Object.keys(pages).sort().map(key => pages[key])); - m.redraw(); - }); + this.ctrl.getFullList().then((list) => { this.data = list; m.redraw(); }); } view() { diff --git a/src/listcontroller.js b/src/listcontroller.js index a527ece5ed22bb0f6e86af050e199f838a9beb6f..ddcd3ec03069ca9a4283066d1611597d2bf09182 100644 --- a/src/listcontroller.js +++ b/src/listcontroller.js @@ -93,6 +93,42 @@ export default class DatalistController { }); } + /* + * Get all available pages + */ + getFullList() { + return new Promise((resolve) => { + // get first page to refresh total page count + this.getPageData(1).then((firstPage) => { + const pages = { 1: firstPage }; + // save totalPages as a constant to avoid race condition with pages added during this + // process + const { totalPages } = this; + console.log(totalPages); + + if (totalPages === 1) { + resolve(firstPage); + } + + // now fetch all the missing pages + Array.from(new Array(totalPages - 1), (x, i) => i + 2).forEach((pageNum) => { + this.getPageData(pageNum).then((newPage) => { + pages[pageNum] = newPage; + // look if all pages were collected + const missingPages = Array.from(new Array(totalPages), (x, i) => i + 1).filter(i => + !(i in pages)); + console.log('missingPages', missingPages); + if (missingPages.length === 0) { + // collect all the so-far loaded pages in order (sorted keys) + // and flatten them into 1 array + resolve([].concat(...Object.keys(pages).sort().map(key => pages[key]))); + } + }); + }); + }); + }); + } + setSearch(search) { if (this.onlineSearch) { this.search = search;