Last active
May 9, 2019 23:20
-
-
Save chrismllr/3c695bc45653db869eba5b08c9dd9c49 to your computer and use it in GitHub Desktop.
ember-concurrency composable task for long poll functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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