forked from rc/aircox
work on player
This commit is contained in:
50
assets/public/live.js
Normal file
50
assets/public/live.js
Normal file
@ -0,0 +1,50 @@
|
||||
import {setEcoTimeout} from 'public/utils';
|
||||
|
||||
export default class Live {
|
||||
constructor({url,timeout=10,src=""}={}) {
|
||||
this.url = url;
|
||||
this.timeout = timeout;
|
||||
this.src = src;
|
||||
|
||||
this.promise = null;
|
||||
this.items = [];
|
||||
}
|
||||
|
||||
get current() {
|
||||
let items = this.logs && this.logs.items;
|
||||
let item = items && items[items.length-1];
|
||||
if(item)
|
||||
item.src = this.src;
|
||||
return item;
|
||||
}
|
||||
|
||||
//-- data refreshing
|
||||
drop() {
|
||||
this.promise = null;
|
||||
}
|
||||
|
||||
fetch() {
|
||||
const promise = fetch(this.url).then(response =>
|
||||
response.ok ? response.json()
|
||||
: Promise.reject(response)
|
||||
).then(data => {
|
||||
this.items = data;
|
||||
return this.items
|
||||
})
|
||||
|
||||
this.promise = promise;
|
||||
return promise;
|
||||
}
|
||||
|
||||
refresh() {
|
||||
const promise = this.fetch();
|
||||
promise.then(data => {
|
||||
if(promise != this.promise)
|
||||
return [];
|
||||
|
||||
setEcoTimeout(() => this.refresh(), this.timeout*1000)
|
||||
})
|
||||
return promise
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user