Skip to content

Instantly share code, notes, and snippets.

@validkeys
Last active April 12, 2021 15:03
Show Gist options
  • Select an option

  • Save validkeys/f7641548ebaec397d94c07be4166f236 to your computer and use it in GitHub Desktop.

Select an option

Save validkeys/f7641548ebaec397d94c07be4166f236 to your computer and use it in GitHub Desktop.
encapsulated-tasks
import Controller from '@ember/controller';
import { task, timeout, waitForProperty } from "ember-concurrency"
import { alias } from '@ember/object/computed'
import { computed, action, set, get } from '@ember/object'
import { tracked } from '@glimmer/tracking'
export default class ApplicationController extends Controller {
appName = 'Ember Twiddle';
@task({ enqueue: true}) subTask = {
called: tracked({ value: false }),
respond() {
set(this, "called", true)
},
*perform(){
while(!this.called) {
console.log("not called")
yield timeout(0)
}
// yield waitForProperty(this, "called", v => {
// console.log("here")
// })
}
}
@task({ enqueue: true }) *clickTask() {
yield this.subTask.perform()
}
@alias('subTask.last') lastTask
@computed('lastTask.isRunning')
get currentTask() {
const { lastTask } = this
console.log({ lastTask })
return !lastTask || !lastTask.isRunning ? null : lastTask
}
@action
respond() {
return this.currentTask && this.currentTask.respond()
}
}
<h1>Welcome to {{this.appName}}</h1>
<br>
<br>
{{outlet}}
<br>
<br>
{{if this.clickTask.isRunning "Running" "Idle"}}
<button onclick={{perform this.clickTask}}>Click</button>
{{#if this.lastTask}}
{{if this.lastTask.called "Called" "Not Called"}}
{{#if this.lastTask.isRunning}}
<div>
Last Task
<button onclick={{fn this.respond}}>Respond</button>
</div>
{{/if}}
{{/if}}
{
"version": "0.17.1",
"EmberENV": {
"FEATURES": {},
"_TEMPLATE_ONLY_GLIMMER_COMPONENTS": false,
"_APPLICATION_TEMPLATE_WRAPPER": true,
"_JQUERY_INTEGRATION": true
},
"options": {
"use_pods": false,
"enable-testing": false
},
"dependencies": {
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.js",
"ember": "3.18.1",
"ember-template-compiler": "3.18.1",
"ember-testing": "3.18.1"
},
"addons": {
"@glimmer/component": "1.0.0",
"ember-concurrency": "2.0.3"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment