Skip to content

Instantly share code, notes, and snippets.

@EWhite613
Last active October 10, 2018 18:33
Show Gist options
  • Save EWhite613/d34b2392faf7e8aa1fbb493340b8358c to your computer and use it in GitHub Desktop.
Save EWhite613/d34b2392faf7e8aa1fbb493340b8358c to your computer and use it in GitHub Desktop.
inner task
import Ember from 'ember';
import { task } from 'ember-concurrency';
import {pollTask} from '../polling'
export default Ember.Controller.extend({
count: 0,
appName: 'Ember Twiddle',
pollCountTask: pollTask(function * (context) {
this.set('count', this.count + 1)
}, {
interval: 100
}),
actions: {
tasker () {
this.pollCountTask.perform()
}
}
});
import Ember from 'ember'
import {task, timeout} from 'ember-concurrency'
import _ from 'lodash'
import {uuid} from 'ember-cli-uuid'
const {testing} = Ember
/**
* @typedef {Object} PollingOptions
* @property {Number} delay - initial delay in ms before polling begins (defaults to interval)
* @property {Boolean} disableInTest - diables polling during test
* @property {Number} interval - polling interval in ms
* @property {Number} maxDuration - will terminate poll after maxDuration in ms
*/
/**
* @const {PollingOptions}
*/
const DEFAULT_OPTIONS = {
disableInTest: true,
delay: undefined,
interval: 10000,
maxDuration: 0
}
/**
* Creates a task which performs the provided generator for every polling interval
* @param {Function} generator - user provided generator (returning false will end the polling)
* @param {PollingOptions} [options] - polling options
* @returns {Task} a ember-concurrency task
*/
export function pollTask (generator, options = {}) {
/* eslint complexity: [2, 10] */
_.defaults(options, DEFAULT_OPTIONS)
const {disableInTest, interval, maxDuration} = options
let delay = options.delay === undefined ? interval : options.delay
const id = uuid()
return task(function * () {
const foo = task(generator.bind(this))
const innerTask = { foo: foo }
if (testing && disableInTest) {
return
}
yield timeout(delay)
const startTime = Date.now()
let endTime = Date.now()
const genInstance = generator()
/* eslint-disable no-unmodified-loop-condition */
while (endTime - startTime < maxDuration || maxDuration === 0) {
const exitStatus = yield Ember.get(innerTask, 'foo').perform(this.context)
if (exitStatus === false) {
return
}
yield timeout(interval)
endTime = Date.now()
}
/* eslint-enable no-unmodified-loop-condition */
})
}
<h1>Welcome to {{appName}}</h1>
<br>
<br>
{{count}}
<br>
<br>
<button {{action "tasker"}}>Run</button>
{
"version": "0.15.1",
"EmberENV": {
"FEATURES": {}
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js",
"ember": "3.4.3",
"ember-template-compiler": "3.4.3",
"ember-testing": "3.4.3"
},
"addons": {
"ember-data": "3.4.2",
"ember-concurrency":"0.8.19",
"ember-lodash": "4.18.0",
"ember-cli-uuid": "0.5.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment