Auto update logs

This commit is contained in:
CzBiX
2019-05-02 18:31:58 +08:00
parent 224adf0c6f
commit 1d0080b16d
3 changed files with 37 additions and 6 deletions

View File

@@ -63,8 +63,14 @@ class Api {
return this.axios.post('/app/setPreferences', data);
}
public getLogs() {
return this.axios.get('/log/main').then(this.handleResponse);
public getLogs(lastId?: number) {
const params = {
last_known_id: lastId,
};
return this.axios.get('/log/main', {
params,
}).then(this.handleResponse);
}
public toggleSpeedLimitsMode() {

View File

@@ -30,13 +30,16 @@
<script lang="ts">
import Vue from 'vue'
import { api } from '../Api';
import { sleep } from '../utils';
export default Vue.extend({
props: {
value: Boolean,
},
data() {
return {
logs: null,
logs: [],
task: 0,
destory: false,
};
},
filters: {
@@ -69,15 +72,33 @@ export default Vue.extend({
this.$emit('input', false);
},
async getLogs() {
this.logs = await api.getLogs();
const lastId = this.logs.length ? this.logs[this.logs.length - 1]['id'] : -1;
const logs = await api.getLogs(lastId);
await this.$nextTick();
this.$refs.end.scrollIntoView();
if (this.destory) {
return;
}
if (logs.length) {
this.logs = this.logs.concat(logs);
await this.$nextTick();
this.$refs.end.scrollIntoView();
}
this.task = setTimeout(this.getLogs, 2000);
},
},
async created() {
await this.getLogs();
},
beforeDestroy() {
this.destory = true;
if (this.task) {
clearTimeout(this.task);
}
},
});
</script>

View File

@@ -42,3 +42,7 @@ export function torrentIsState(type: StateType, state: string) {
return result;
}
export function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}