Add nested page cache

This commit is contained in:
gconsidine
2018-01-22 15:01:11 -05:00
committed by Jake McDermott
parent 4b81d8d494
commit 745e547e34
2 changed files with 47 additions and 26 deletions

View File

@@ -110,8 +110,6 @@ function next () {
console.log('below:', getRowsBelow()); console.log('below:', getRowsBelow());
append(cursor); append(cursor);
shift(); shift();
debugger;
}); });
} }

View File

@@ -107,9 +107,15 @@ function httpGet (config = {}) {
if (config.params.page_size) { if (config.params.page_size) {
this.page.size = config.params.page_size; this.page.size = config.params.page_size;
this.page.limit = config.pageLimit || false;
this.page.cache = config.pageCache || false;
this.page.current = 1; this.page.current = 1;
if (config.pageCache) {
this.page.cache = {
root: {}
};
this.page.limit = config.pageLimit || false;
}
} }
} }
@@ -121,11 +127,14 @@ function httpGet (config = {}) {
req.url = `${this.path}${config.resource}/`; req.url = `${this.path}${config.resource}/`;
} }
console.log(req, this.path);
return $http(req) return $http(req)
.then(res => { .then(res => {
this.model.GET = res.data; this.model.GET = res.data;
if (config.pageCache) {
this.page.cache[this.page.current] = res.data.results;
}
return res; return res;
}); });
} }
@@ -340,10 +349,12 @@ function extend (related, config) {
if (config.params.page_size) { if (config.params.page_size) {
this.page.size = config.params.page_size; this.page.size = config.params.page_size;
this.page.limit = config.pageLimit || false;
this.page.cache = config.pageCache || false;
this.page.current = 1; this.page.current = 1;
this.page.cursor = 0;
if (config.pageCache) {
this.page.cache = this.page.cache || {};
this.page.limit = config.pageLimit || false;
}
} }
if (this.has(req.method, `related.${related}`)) { if (this.has(req.method, `related.${related}`)) {
@@ -355,6 +366,12 @@ function extend (related, config) {
.then(({ data }) => { .then(({ data }) => {
this.set(req.method, `related.${related}`, data); this.set(req.method, `related.${related}`, data);
if (config.pageCache) {
const key = `related.${related}.${this.page.current}`;
_.set(this.page.cache, key, data.results);
}
return this; return this;
}); });
} }
@@ -366,40 +383,42 @@ function goToPage (config, page) {
const params = config.params || {}; const params = config.params || {};
let url; let url;
// let results; let count;
let cursor; let key;
let pageNumber; let pageNumber;
if (config.related) { if (config.related) {
// results = this.get(`related.${config.related}.results`) || []; count = this.get(`related.${config.related}.count`);
url = `${this.endpoint}${config.related}/`; url = `${this.endpoint}${config.related}/`;
key = `related.${config.related}`;
} else { } else {
// results = this.get('results'); count = this.get('count');
url = this.endpoint; url = this.endpoint;
key = 'root';
} }
params.page_size = this.page.size; params.page_size = this.page.size;
if (page === 'next') { if (page === 'next') {
// if (at max) // if (at max)
pageNumber = this.page.current + 1; pageNumber = this.page.current + 1;
cursor = this.page.cursor + this.page.size; } else if (page === 'previous') {
} else if (page === 'prev') { // if (at min)
pageNumber = this.page.current - 1;
} else if (page === 'first') {
// if (at min) // if (at min)
pageNumber = this.page.current - 1; pageNumber = 1;
cursor = this.page.cursor - this.page.size; } else if (page === 'last') {
} else { // if (at min)
pageNumber = Math.floor(count / this.page.size);
} else if (typeof pageNumber === 'number') {
pageNumber = page; pageNumber = page;
cursor = this.page.size * (pageNumber - 1);
} }
if (cursor !== 0 && cursor !== (this.page.limit * this.page.size)) { if (this.page.cache && this.page.cache[pageNumber]) {
this.page.cursor = cursor; return Promise.resolve(this.page.cache[pageNumber]);
this.page.current = pageNumber;
return Promise.resolve(cursor);
} }
params.page_size = this.page.size; params.page_size = this.page.size;
@@ -413,7 +432,11 @@ function goToPage (config, page) {
return $http(req) return $http(req)
.then(({ data }) => { .then(({ data }) => {
console.log(data); if (this.page.cache) {
_.set(this.page.cache, `${key}.${pageNumber}`, data.results);
}
console.log('cache', this.page.cache);
}); });
} }
@@ -450,7 +473,7 @@ function next (config = {}) {
} }
function prev (config = {}) { function prev (config = {}) {
return this.goToPage(config, 'next'); return this.goToPage(config, 'prev');
} }
/* /*
* let url; * let url;