Skip to content

Instantly share code, notes, and snippets.

@chrismllr
Last active May 9, 2019 23:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrismllr/3c695bc45653db869eba5b08c9dd9c49 to your computer and use it in GitHub Desktop.
Save chrismllr/3c695bc45653db869eba5b08c9dd9c49 to your computer and use it in GitHub Desktop.
ember-concurrency composable task for long poll functionality
import { task, timeout } from 'ember-concurrency';
import Config from 'app/config/environment';
const DEFAULT_POLL_MS = 20000; // 20 sec
export function pollTask(_requestTask, pollMs = DEFAULT_POLL_MS) {
const taskProperty = task(_requestTask).restartable();
const ogTaskFn = taskProperty.taskFn;
taskProperty.taskFn = function*(...args) {
while (true) {
try {
yield* ogTaskFn.apply(this, args);
if (Config.environment === 'test') {
break;
}
yield timeout(pollMs);
} catch (err) {
console.error('tasks#poll: Polling Task failed. Stopping poll.', err);
break;
}
}
};
return taskProperty;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment