1
0
Fork 0
mirror of https://github.com/TryGhost/Ghost-Admin.git synced 2023-12-14 02:33:04 +01:00
Ghost-Admin/app/components/gh-download-count.js
Kevin Ansfield d01469428c 💄 replace DownloadCountPoller with gh-download-count
closes https://github.com/TryGhost/Ghost/issues/8321
- adds `gh-download-count` component that uses ember-concurrency to poll the count endpoint
- removes the no-longer-needed `setup/one` route as ember-concurrency now handles the setInterval bookkeeping for us
2017-04-17 10:04:53 -04:00

41 lines
1 KiB
JavaScript

import Ember from 'ember';
import Component from 'ember-component';
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(),
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();
}
});