machty / ember-concurrency

ember-concurrency is an Ember Addon that enables you to write concise, worry-free, cancelable, restartable, asynchronous tasks.
http://ember-concurrency.com
MIT License
689 stars 157 forks source link

Using task helper disconnects original task #327

Open bobisjan opened 5 years ago

bobisjan commented 5 years ago

Hi,

when having task defined like this

import Controller from '@ember/controller';
import { task, timeout } from 'ember-concurrency'

export default Controller.extend({
  hi: task(function*() {
    console.log('this.hi.isRunning', this.hi.isRunning);
    yield timeout(1500);
  })
});

with button

<button
  {{action (perform this.hi) on="click"}}
>
  Hi
</button>

then it logs this.hi.isRunning true,


but with button using (task helper

<button
  {{action (perform (task this.hi)) on="click"}}
>
  Hi
</button>

then it logs this.hi.isRunning false.

I'm not sure if this is a bug or expected behaviour, but currently this disallows using task.isRunning property with (task helper?

ember-source: 3.13.3
ember-concurrency: 1.1.1

Thanks

maxfierke commented 4 years ago

Based on the implementation, I believe this is intentional, as it does clone the original task. However, you should be able to use the let helper to access the isRunning for a curried task within the template:

e.g.

{{#let (task this.hi) as |newTask|}}
  {{#if newTask.isRunning}}
    Trying to say hi...
  {{else}}
    <button onclick={{action (perform newTask)}}>
      Say Hi
    </button>
  {{/if}}
{{/let}}

Caveat: other task state may be shared with the original task (See https://github.com/machty/ember-concurrency/issues/258)