Skip to content

Instantly share code, notes, and snippets.

@tristanpendergrass
Created July 28, 2016 19:55
Show Gist options
  • Save tristanpendergrass/96a4681fe1876258a7b197989f3489ae to your computer and use it in GitHub Desktop.
Save tristanpendergrass/96a4681fe1876258a7b197989f3489ae to your computer and use it in GitHub Desktop.
class ContentCtrl {
constructor($scope, contentItems) {
this.readyIntervals = []; // $interval for each content item that we're checking "ready" status of
$scope.$on('$destroy', () => {
this.readyIntervals.forEach($interval.cancel.bind($interval));
});
this.contentItems = contentItems;
this.contentItems.forEach(this.getReadyStatus.bind(this));
}
getReadyStatus(item) {
const TIME_TO_WAIT = 10000;
const POLLING_INTERVAL = 1000;
const interval = $interval(() => {
$http.get(url)
.then((res) => {
if (res.data.status === 'Ready') {
$interval.cancel(interval);
this.readyIntervals.splice(this.readyIntervals.indexOf(interval));
item.status = 'Ready';
}
})
}, POLLING_INTERVAL);
this.readyIntervals.push(interval);
$timeout(() => {
$interval.cancel(interval);
this.readyIntervals.splice(this.readyIntervals.indexOf(interval));
}, TIME_TO_WAIT);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment