2017-04-12 14:34:59 +03:00
|
|
|
import Component from 'ember-component';
|
2017-05-29 21:50:03 +03:00
|
|
|
import Ember from 'ember';
|
2017-04-12 14:34:59 +03:00
|
|
|
import injectService from 'ember-service/inject';
|
|
|
|
import {task, timeout} from 'ember-concurrency';
|
|
|
|
|
|
|
|
const {testing} = Ember;
|
|
|
|
const INTERVAL = testing ? 20 : 2000;
|
|
|
|
|
|
|
|
export default Component.extend({
|
|
|
|
ajax: injectService(),
|
|
|
|
ghostPaths: injectService(),
|
|
|
|
|
2017-07-26 12:03:09 +03:00
|
|
|
tagName: '',
|
2017-04-12 14:34:59 +03:00
|
|
|
count: '',
|
|
|
|
|
|
|
|
_poll: task(function* () {
|
|
|
|
let url = this.get('ghostPaths.count');
|
|
|
|
let pattern = /(-?\d+)(\d{3})/;
|
|
|
|
|
|
|
|
try {
|
|
|
|
let data = yield this.get('ajax').request(url);
|
|
|
|
let count = data.count.toString();
|
|
|
|
|
|
|
|
while (pattern.test(count)) {
|
|
|
|
count = count.replace(pattern, '$1,$2');
|
|
|
|
}
|
|
|
|
|
|
|
|
this.set('count', count);
|
|
|
|
|
|
|
|
if (!testing) {
|
|
|
|
yield timeout(INTERVAL);
|
|
|
|
this.get('_poll').perform();
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
// no-op - we don't want to create noise for a failed download count
|
|
|
|
}
|
|
|
|
}),
|
|
|
|
|
|
|
|
didInsertElement() {
|
|
|
|
this.get('_poll').perform();
|
|
|
|
}
|
|
|
|
});
|