machty / ember-concurrency-decorators

Task decorators for ember-concurrency 1.x (merged into ember-concurrency 2.0)
MIT License
70 stars 25 forks source link

Babel transform for assignment syntax (TypeScript support) #41

Open buschtoens opened 5 years ago

buschtoens commented 5 years ago

Related to https://github.com/machty/ember-concurrency-decorators/issues/30#issuecomment-455184343 and #35.

Create a babel transform that converts the following assignment syntax to decorator syntax:

import { task, restartableTask } from 'ember-concurrency-decorators';

class Foo {
  @restartableTask({ maxConcurrency: 5 })
  bar = task(function*(this: Foo) {
    // ...
  });
}
import { restartableTask} from 'ember-concurrency-decorators';

class Foo {
  @restartableTask({ maxConcurrency: 5 })
  bar = function*(this: Foo) {
    // ...
  };
}

Using task in assignment syntax allows us to provide valid types for TypeScript.

Since we're applying a Babel transform now anyway, we can also remove the decorator usage at runtime completely by using Ember.defineProperty.

import { task } from 'ember-concurrency';
import { defineProperty } from '@ember/object';

class Foo {}

defineProperty(
  Foo.prototype,
  'bar',
  task(function*(this: Foo) {
    // ...
  }).maxConcurrency(5).restartable()
);