Merge pull request #406 from YunoHost/enh-optimization

Optimization for waiting modal messages and AppCatalog cards rendering
This commit is contained in:
Alexandre Aubin 2021-10-01 17:16:12 +02:00 committed by GitHub
commit a10aaae7c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 195 additions and 60 deletions

View file

@ -0,0 +1,59 @@
<template>
<div class="lazy-renderer" :style="`min-height: ${fixedMinHeight}px`">
<slot v-if="render" />
</div>
</template>
<script>
export default {
name: 'LazyRenderer',
props: {
unrender: { type: Boolean, default: true },
minHeight: { type: Number, default: 0 },
renderDelay: { type: Number, default: 100 },
unrenderDelay: { type: Number, default: 2000 },
rootMargin: { type: String, default: '300px' }
},
data () {
return {
observer: null,
render: false,
fixedMinHeight: this.minHeight
}
},
mounted () {
let unrenderTimer
let renderTimer
this.observer = new IntersectionObserver(entries => {
if (entries[0].isIntersecting) {
clearTimeout(unrenderTimer)
// Show the component after a delay (to avoid rendering while scrolling fast)
renderTimer = setTimeout(() => {
this.render = true
}, this.unrender ? this.renderDelay : 0)
if (!this.unrender) {
// Stop listening to intersections after first appearance if unrendering is not activated
this.observer.disconnect()
}
} else if (this.unrender) {
clearTimeout(renderTimer)
// Hide the component after a delay if it's no longer in the viewport
unrenderTimer = setTimeout(() => {
this.fixedMinHeight = this.$el.clientHeight
this.render = false
}, this.unrenderDelay)
}
}, { rootMargin: this.rootMargin })
this.observer.observe(this.$el)
},
beforeDestroy () {
this.observer.disconnect()
}
}
</script>

View file

@ -1,9 +1,15 @@
<template> <template>
<b-list-group <b-list-group
v-bind="$attrs" ref="self" v-bind="$attrs" flush
flush :class="{ 'fixed-height': fixedHeight, 'bordered': bordered }" :class="{ 'fixed-height': fixedHeight, 'bordered': bordered }"
@scroll="onScroll"
> >
<b-list-group-item v-for="({ color, text }, i) in messages" :key="i"> <b-list-group-item
v-if="limit && messages.length > limit"
variant="info" v-t="'api.partial_logs'"
/>
<b-list-group-item v-for="({ color, text }, i) in reducedMessages" :key="i">
<span class="status" :class="'bg-' + color" /> <span class="status" :class="'bg-' + color" />
<span v-html="text" /> <span v-html="text" />
</b-list-group-item> </b-list-group-item>
@ -18,15 +24,36 @@ export default {
messages: { type: Array, required: true }, messages: { type: Array, required: true },
fixedHeight: { type: Boolean, default: false }, fixedHeight: { type: Boolean, default: false },
bordered: { type: Boolean, default: false }, bordered: { type: Boolean, default: false },
autoScroll: { type: Boolean, default: false } autoScroll: { type: Boolean, default: false },
limit: { type: Number, default: null }
},
data () {
return {
auto: true
}
},
computed: {
reducedMessages () {
const len = this.messages.length
if (!this.limit || len <= this.limit) {
return this.messages
}
return this.messages.slice(len - this.limit)
}
}, },
methods: { methods: {
scrollToEnd () { scrollToEnd () {
if (!this.auto) return
this.$nextTick(() => { this.$nextTick(() => {
const container = this.$refs.self this.$el.scrollTo(0, this.$el.lastElementChild.offsetTop)
container.scrollTo(0, container.lastElementChild.offsetTop)
}) })
},
onScroll ({ target }) {
this.auto = target.scrollHeight === target.scrollTop + target.clientHeight
} }
}, },

View file

@ -14,6 +14,7 @@
"administration_password": "Administration password", "administration_password": "Administration password",
"all": "All", "all": "All",
"api": { "api": {
"partial_logs": "[...] (check in history for full logs)",
"processing": "The server is processing the action...", "processing": "The server is processing the action...",
"query_status": { "query_status": {
"error": "Unsuccessful", "error": "Unsuccessful",

View file

@ -12,7 +12,9 @@ export default {
waiting: false, // Boolean waiting: false, // Boolean
history: [], // Array of `request` history: [], // Array of `request`
requests: [], // Array of `request` requests: [], // Array of `request`
error: null // null || request error: null, // null || request
historyTimer: null, // null || setTimeout id
tempMessages: [] // array of messages
}, },
mutations: { mutations: {
@ -52,12 +54,26 @@ export default {
state.history.push(request) state.history.push(request)
}, },
'ADD_MESSAGE' (state, { message, type }) { 'ADD_TEMP_MESSAGE' (state, { request, message, type }) {
const request = state.history[state.history.length - 1] state.tempMessages.push([message, type])
request.messages.push(message) },
if (['error', 'warning'].includes(type)) {
request[type + 's']++ 'UPDATE_DISPLAYED_MESSAGES' (state, { request }) {
if (!state.tempMessages.length) {
state.historyTimer = null
return
} }
const { messages, warnings, errors } = state.tempMessages.reduce((acc, [message, type]) => {
acc.messages.push(message)
if (['error', 'warning'].includes(type)) acc[type + 's']++
return acc
}, { messages: [], warnings: 0, errors: 0 })
state.tempMessages = []
state.historyTimer = null
request.messages = request.messages.concat(messages)
request.warnings += warnings
request.errors += errors
}, },
'SET_ERROR' (state, request) { 'SET_ERROR' (state, request) {
@ -147,7 +163,11 @@ export default {
return request return request
}, },
'END_REQUEST' ({ commit }, { request, success, wait }) { 'END_REQUEST' ({ state, commit }, { request, success, wait }) {
// Update last messages before finishing this request
clearTimeout(state.historyTimer)
commit('UPDATE_DISPLAYED_MESSAGES', { request })
let status = success ? 'success' : 'error' let status = success ? 'success' : 'error'
if (success && (request.warnings || request.errors)) { if (success && (request.warnings || request.errors)) {
const messages = request.messages const messages = request.messages
@ -166,7 +186,7 @@ export default {
} }
}, },
'DISPATCH_MESSAGE' ({ commit }, { request, messages }) { 'DISPATCH_MESSAGE' ({ state, commit, dispatch }, { request, messages }) {
for (const type in messages) { for (const type in messages) {
const message = { const message = {
text: messages[type].replace('\n', '<br>'), text: messages[type].replace('\n', '<br>'),
@ -183,7 +203,13 @@ export default {
commit('UPDATE_REQUEST', { request, key: 'progress', value: Object.values(progress) }) commit('UPDATE_REQUEST', { request, key: 'progress', value: Object.values(progress) })
} }
if (message.text) { if (message.text) {
commit('ADD_MESSAGE', { request, message, type }) // To avoid rendering lag issues, limit the flow of websocket messages to batches of 50ms.
if (state.historyTimer === null) {
state.historyTimer = setTimeout(() => {
commit('UPDATE_DISPLAYED_MESSAGES', { request })
}, 50)
}
commit('ADD_TEMP_MESSAGE', { request, message, type })
} }
} }
}, },

View file

@ -59,7 +59,7 @@
@shown="scrollToAction(i)" @shown="scrollToAction(i)"
@hide="scrollToAction(i)" @hide="scrollToAction(i)"
> >
<message-list-group :messages="action.messages" flush /> <message-list-group :messages="action.messages" />
</b-collapse> </b-collapse>
</b-card> </b-card>
</div> </div>

View file

@ -18,6 +18,7 @@
<message-list-group <message-list-group
v-if="hasMessages" :messages="request.messages" v-if="hasMessages" :messages="request.messages"
bordered fixed-height auto-scroll bordered fixed-height auto-scroll
:limit="100"
/> />
</b-card-body> </b-card-body>
</template> </template>

View file

@ -64,10 +64,12 @@
<!-- APPS CARDS --> <!-- APPS CARDS -->
<b-card-group v-else deck> <b-card-group v-else deck>
<b-card no-body v-for="app in filteredApps" :key="app.id"> <lazy-renderer v-for="app in filteredApps" :key="app.id" :min-height="120">
<b-card no-body>
<b-card-body class="d-flex flex-column"> <b-card-body class="d-flex flex-column">
<b-card-title class="d-flex mb-2"> <b-card-title class="d-flex mb-2">
{{ app.manifest.name }} {{ app.manifest.name }}
<small v-if="app.state !== 'working'" class="d-flex align-items-center ml-2"> <small v-if="app.state !== 'working'" class="d-flex align-items-center ml-2">
<b-badge <b-badge
v-if="app.state !== 'highquality'" v-if="app.state !== 'highquality'"
@ -76,6 +78,7 @@
> >
{{ $t('app_state_' + app.state) }} {{ $t('app_state_' + app.state) }}
</b-badge> </b-badge>
<icon <icon
v-else iname="star" class="star" v-else iname="star" class="star"
v-b-popover.hover.bottom="$t(`app_state_${app.state}_explanation`)" v-b-popover.hover.bottom="$t(`app_state_${app.state}_explanation`)"
@ -110,6 +113,7 @@
</b-button> </b-button>
</b-button-group> </b-button-group>
</b-card> </b-card>
</lazy-renderer>
</b-card-group> </b-card-group>
<template #bot> <template #bot>
@ -155,12 +159,17 @@
<script> <script>
import { validationMixin } from 'vuelidate' import { validationMixin } from 'vuelidate'
import LazyRenderer from '@/components/LazyRenderer'
import { required, githubLink } from '@/helpers/validators' import { required, githubLink } from '@/helpers/validators'
import { randint } from '@/helpers/commons' import { randint } from '@/helpers/commons'
export default { export default {
name: 'AppCatalog', name: 'AppCatalog',
components: {
LazyRenderer
},
data () { data () {
return { return {
queries: [ queries: [
@ -375,10 +384,11 @@ export default {
} }
.card-deck { .card-deck {
.card { > * {
border-color: $gray-400; margin-left: 15px;
margin-right: 15px;
margin-bottom: 2rem; margin-bottom: 2rem;
flex-basis: 90%; flex-basis: 100%;
@include media-breakpoint-up(md) { @include media-breakpoint-up(md) {
flex-basis: 50%; flex-basis: 50%;
max-width: calc(50% - 30px); max-width: calc(50% - 30px);
@ -388,6 +398,15 @@ export default {
max-width: calc(33.3% - 30px); max-width: calc(33.3% - 30px);
} }
.card {
margin: 0;
height: 100%;
}
}
.card {
border-color: $gray-400;
// not maintained info // not maintained info
.alert-warning { .alert-warning {
font-size: .75em; font-size: .75em;
@ -402,6 +421,8 @@ export default {
@include media-breakpoint-up(sm) { @include media-breakpoint-up(sm) {
min-height: 10rem; min-height: 10rem;
} }
flex-basis: 90%;
border: 0; border: 0;
.btn { .btn {