...
 
Commits (3)
......@@ -44,11 +44,31 @@ export default class FeedsService {
*/
feed = [];
/**
* @var {string}
*/
pagingToken: string = '';
/**
* @var {boolean}
*/
endReached = false;
/**
* @var {boolean}
*/
paginated = true;
/**
* Get entities from the current page
*/
async getEntities() {
const feedPage = this.feed.slice(this.offset, this.limit + this.offset);
const end = this.limit + this.offset;
if (this.paginated && end > this.feed.length && !this.endReached) {
await this.fetch(true);
}
const feedPage = this.feed.slice(this.offset, end);
return await entitiesService.getFromFeed(feedPage, this, this.asActivities);
}
......@@ -120,6 +140,10 @@ export default class FeedsService {
return this;
}
/**
* Set parameters
* @param {Object} params
*/
setParams(params): FeedsService {
this.params = params;
if (!params.sync) {
......@@ -138,6 +162,16 @@ export default class FeedsService {
return this;
}
/**
* Set paginated
* @param {boolean} paginated
* @returns {FeedsService}
*/
setPaginated(paginated: boolean): FeedsService {
this.paginated = paginated;
return this;
}
/**
* Abort pending fetch
*/
......@@ -147,12 +181,28 @@ export default class FeedsService {
/**
* Fetch
* @param {boolean} more
*/
async fetch() {
async fetch(more = false) {
abort(this);
const response = await apiService.get(this.endpoint, {...this.params, ...{ limit: 150, as_activities: this.asActivities ? 1 : 0 }}, this);
this.feed = response.entities;
const params = {...this.params, ...{ limit: 150, as_activities: this.asActivities ? 1 : 0 }};
if (this.paginated && more) params.from_timestamp = this.pagingToken;
const response = await apiService.get(this.endpoint, params, this);
if (response.entities.length) {
if (more) {
this.feed = this.feed.concat(response.entities);
} else {
this.feed = response.entities;
}
this.pagingToken = response['load-next'];
if (response.entities.length < 150) this.endReached = true;
} else {
this.endReached = true;
}
// save without wait
feedsStorage.save(this);
......@@ -165,7 +215,14 @@ export default class FeedsService {
try {
const feed = await feedsStorage.read(this);
if (feed) {
this.feed = feed;
// support old format
if (Array.isArray(feed)) {
this.feed = feed;
this.pagingToken = this.feed[this.feed.length - 1].timestamp - 1;
} else {
this.feed = feed.feed;
this.pagingToken = feed.next;
}
return true;
}
} catch (err) {
......@@ -238,6 +295,7 @@ export default class FeedsService {
clear(): FeedStore {
this.offset = 0;
this.limit = 12;
this.pagingToken = '';
this.params = {sync: 1};
this.feed = [];
return this;
......
......@@ -24,7 +24,9 @@ export class FeedsStorage {
try {
await this.getDb();
await this.db.executeSql('REPLACE INTO feeds (key, offset, data, updated) values (?,?,?,?)', [key, 0, JSON.stringify(this.map(feed.feed)), Math.floor(Date.now() / 1000)]);
const params = [key, 0, JSON.stringify({feed: this.map(feed.feed), next: feed.pagingToken}), Math.floor(Date.now() / 1000)];
await this.db.executeSql('REPLACE INTO feeds (key, offset, data, updated) values (?,?,?,?)', params);
} catch (err) {
logService.exception('[FeedsStorage]', err);
}
......
......@@ -181,6 +181,16 @@ export default class FeedStore {
return this;
}
/**
* Set paginated
* @param {boolean} paginated
* @returns {FeedStore}
*/
setPaginated(paginated: boolean): FeedStore {
this.feedsService.setPaginated(paginated);
return this;
}
/**
* Set the params for the feeds service
* @param {Object} params
......
......@@ -66,6 +66,9 @@ class DiscoveryStore {
this.listenChanges();
// we don't have feed pagination on discovery
this.listStore.setPaginated(false);
this.listStore.getMetadataService()
.setSource('feed/discovery')
.setMedium('feed');
......