2018-08-30 00:15:55 +00:00
|
|
|
import Mixin from '@ember/object/mixin';
|
|
|
|
import { assert } from '@ember/debug';
|
2018-09-17 22:58:28 +00:00
|
|
|
import { task, timeout } from 'ember-concurrency';
|
2018-08-30 00:15:55 +00:00
|
|
|
|
|
|
|
export default Mixin.create({
|
|
|
|
url: '',
|
|
|
|
|
2018-09-14 15:57:26 +00:00
|
|
|
bufferSize: 500,
|
|
|
|
|
2018-08-30 00:15:55 +00:00
|
|
|
fetch() {
|
2018-08-31 02:57:28 +00:00
|
|
|
assert('StatsTrackers need a fetch method, which should have an interface like window.fetch');
|
2018-08-30 00:15:55 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
append(/* frame */) {
|
|
|
|
assert(
|
2018-08-31 02:57:28 +00:00
|
|
|
'StatsTrackers need an append method, which takes the JSON response from a request to url as an argument'
|
2018-08-30 00:15:55 +00:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2018-09-17 22:58:28 +00:00
|
|
|
// Uses EC as a form of debounce to prevent multiple
|
|
|
|
// references to the same tracker from flooding the tracker,
|
|
|
|
// but also avoiding the issue where different places where the
|
|
|
|
// same tracker is used needs to coordinate.
|
|
|
|
poll: task(function*() {
|
2018-08-30 00:15:55 +00:00
|
|
|
const url = this.get('url');
|
|
|
|
assert('Url must be defined', url);
|
|
|
|
|
2018-09-17 22:58:28 +00:00
|
|
|
yield this.get('fetch')(url)
|
2018-09-13 23:32:29 +00:00
|
|
|
.then(res => res.json())
|
2018-08-30 00:15:55 +00:00
|
|
|
.then(frame => this.append(frame));
|
2018-09-17 22:58:28 +00:00
|
|
|
|
|
|
|
yield timeout(2000);
|
|
|
|
}).drop(),
|
2018-08-30 00:15:55 +00:00
|
|
|
});
|