From ff2425bfd5d6de4bd0acba67c967261a83dcd610 Mon Sep 17 00:00:00 2001 From: Hermann <blumh@ethz.ch> Date: Sat, 1 Sep 2018 12:16:43 +0200 Subject: [PATCH] move getFullList function into controller --- src/groups/list.js | 19 +------------------ src/listcontroller.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/src/groups/list.js b/src/groups/list.js index bb7fc1b..02ca33c 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 a527ece..ddcd3ec 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; -- GitLab