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
691 stars 155 forks source link

2.3.0 arrow syntax not working #471

Closed urbany closed 1 year ago

urbany commented 1 year ago

Hi, first of all thank you all for the work on this addon!

Ember: 3.28.9

❯ npm ls ember-concurrency
root
└─┬ myapp
  ├── ember-concurrency@2.3.0
  ├─┬ ember-power-calendar@0.18.0
  │ └── ember-concurrency@2.3.0 deduped
  └─┬ ember-power-select@6.0.1
    └── ember-concurrency@2.3.0 deduped
import { task } from 'ember-concurrency';

export default class MyService extends Service {
  async setup() {
    await this.myTask.perform();
  }

  myTask = task(this, async () => {
    // ...
  });
  //...
}

Error

Uncaught (in promise) TypeError: this.myTask.perform is not a function

If I check myTask it says it is this function taskComputed:

ember-concurrency/-private/task-properties.js Screenshot 2022-08-24 at 09 43 13

Previous @task *myTask syntax is still working.

EDIT: also checked how the task is being defined, and there seems to be something wrong here, my fn is being ignored, and my service is being used to buildClassicTaskProperty

ember-concurrency/-private/task-public-api.js Screenshot 2022-08-24 at 09 54 02

machty commented 1 year ago

@urbany can you find the compiled JS code for myTask and paste it here? e.g. find what myTask = task(this, async () => { compiles to.

urbany commented 1 year ago

After talking to @machty on discord it seems to be a problema with a babel transform not running. We're not sure how to debug, and a small reproduction is needed.

urbany commented 1 year ago

Figured it out. Was using tasks in an in-app addon without declaring ember-concurrency as a dependency of the addon. Added id to my in-app addon package.json and now it is working @machty you can close this if you want.

olyckne commented 1 year ago

I seem to have a similar issue but have no idea how to debug this. The transpilation doesn't run.

Reproduction: https://ember-twiddle.com/df50d8a9f24a72674ada3f32ab263023

Error: Assertion Failed: It appears you're attempting to use the new task(async () => { ... }) syntax, but the async arrow task function you've provided is not being properly compiled by Babel.

1. You must pass the async function expression directly to the task() function (it is not currently supported to pass in a variable containing the async arrow fn, or any other kind of indirection)

My code: test = task(async () => {}); Transpiled code: _defineProperty(this, "test", (cov_1uxrpjaiu0().s[14]++, (0, _emberConcurrency.task)(async () => { cov_1uxrpjaiu0().f[4]++; })));

2. If this code is in an addon, please ensure the addon specificies ember-concurrency "2.3.0" or higher in "dependencies" (not "devDependencies") It is not an addon

3. Ensure that there is only one version of ember-concurrency v2.3.0+ being used in your project (including nested dependencies) and consider using npm/yarn/pnpm resolutions to enforce a single version is used npm ls ember-concurrency shows only one ember-concurrency@2.3.4

machty commented 1 year ago

@olyckne I don't know if ember-twiddle is going to support the new transpiled Task syntax or not, but I think maybe your issue is that you're still using @task as a decorator, but the decorator isn't necessary anymore.

Instead of

  @task
  foo = task(async () => {
    // ...
  });

just do

  foo = task(async () => {
    // ...
  });
olyckne commented 1 year ago

@machty After some investigating, I found the problem. It's something with coverage mentioned in https://github.com/machty/ember-concurrency/issues/483

I always run npm start which in my application translates to cross-env COVERAGE=true ember serve. Took a while to connect the dots :)