Skip to content

Commit

Permalink
Merge pull request #691 from aeternity/feature/refactor-tips-pagination
Browse files Browse the repository at this point in the history
TipsPagination: Combine data fetching, fetch simultaneously
  • Loading branch information
mradkov authored Jul 10, 2020
2 parents ee1a79b + d19d08f commit ac146dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 38 deletions.
29 changes: 10 additions & 19 deletions src/components/TipsPagination.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
</template>

<script>
import { times } from 'lodash-es';
import Loading from './Loading.vue';
import Backend from '../utils/backend';
import { MIDDLEWARE_URL } from '../config/constants';
import TipRecord from './tipRecords/TipRecord.vue';
import Util from '../utils/util';
import { EventBus } from '../utils/eventBus';
export default {
Expand Down Expand Up @@ -67,7 +67,7 @@ export default {
},
},
async created() {
this.loadData();
await this.reloadData();
},
mounted() {
const scrollHandler = () => {
Expand All @@ -81,9 +81,7 @@ export default {
this.$on('hook:deactivated', () => window.removeEventListener('scroll', scrollHandler));
this.$once('hook:destroyed', () => window.removeEventListener('scroll', scrollHandler));
EventBus.$on('reloadData', () => {
this.reloadData();
});
EventBus.$on('reloadData', () => this.reloadData());
const interval = setInterval(() => this.reloadData(), 120 * 1000);
this.$once('hook:destroyed', () => clearInterval(interval));
Expand All @@ -94,21 +92,15 @@ export default {
this.endReached = false;
this.page = 1;
this.tips = null;
this.loadData();
await this.reloadData();
},
async loadData() {
this.loadingTips = true;
this.tips = await Backend.getCacheTips(this.tipSortBy, this.page,
this.address, this.search, this.blacklist);
if (this.tips) {
this.loadingTips = false;
}
getCacheTips(page) {
return Backend.getCacheTips(this.tipSortBy, page, this.address, this.search, this.blacklist);
},
async loadMoreTips() {
if (!this.endReached && !this.loadingMoreTips) {
this.loadingMoreTips = true;
const tips = await Backend
.getCacheTips(this.tipSortBy, this.page + 1, this.address, this.search, this.blacklist);
const tips = await this.getCacheTips(this.page + 1);
this.tips = this.tips.concat(tips);
if (tips.length > 0) {
this.page += 1;
Expand All @@ -120,10 +112,9 @@ export default {
},
async reloadData() {
this.loadingTips = true;
this.tips = await Util.range(1, this.page)
.asyncMap(async (page) => Backend
.getCacheTips(this.tipSortBy, page, this.address, this.search, this.blacklist));
this.tips = (await Promise.all(
times(this.page, (page) => this.getCacheTips(page + 1)),
)).flat();
this.loadingTips = false;
},
openExplorer(address) {
Expand Down
19 changes: 0 additions & 19 deletions src/utils/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,6 @@ export const wrapTry = async (promise) => {
}
};

const range = (start, end) => (new Array(end - start + 1)).fill(undefined).map((_, i) => i + start);

// eslint-disable-next-line no-extend-native, func-names
Array.prototype.asyncMap = async function (asyncF) {
return this.reduce(async (promiseAcc, cur) => {
const acc = await promiseAcc;
const res = await asyncF(cur).catch((e) => {
console.error('asyncMap asyncF', e.message);
return null;
});
if (Array.isArray(res)) {
return acc.concat(res);
}
if (res) acc.push(res);
return acc;
}, Promise.resolve([]));
};

export const supportedBrowsers = [
'chrome', 'firefox', 'opera', 'vivaldi', 'brave', 'edge-chromium',
];
Expand Down Expand Up @@ -122,7 +104,6 @@ export const getI18nPath = (index, page) => (isTitle(index, page)
export default {
atomsToAe,
aeToAtoms,
range,
wrapTry,
currencySigns,
createDeepLinkUrl,
Expand Down

1 comment on commit ac146dd

@mradkov
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.