npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ember-concurrency-ts

v0.3.1

Published

TypeScript utilities for ember-concurrency

Downloads

12,064

Readme

ember-concurrency-ts

TypeScript utilities for ember-concurrency.

This is how you would typically write ember-concurrency tasks in Octane:

import { action } from '@ember/object';
import Component from '@glimmer/component';
import { task, TaskGenerator, timeout } from 'ember-concurrency';

export default class extends Component {
  @task *myTask(ms: number): TaskGenerator<string> {
    yield timeout(ms);
    return 'done!';
  }

  @action performTask() {
    if (this.myTask.isRunning) {
      return;
    }

    this.myTask.perform(1000).then(value => {
      console.log(value.toUpperCase());
    });
  }
}

Since we are using native classes in Octane, TypeScript have an easier time understanding and following our code. Normally, this is a good thing, but in the case of ember-concurrency, it ends up getting a bit in the way.

ember-concurrency's API was designed with Class Ember in mind, where it could decorate a property or method and replace it with a different type in the .extend() hook.

This is not allowed using TypeScript's decorators. Since myTask is defined using as the generator method syntax, and since methods do not have a .perform() method on them, calling this.myTask.perform() will result in a type error, even though it will work at runtime.

We could work around this by type casting the method, such as (this.myTask as any as Task<string, number>), but doing this everywhere is quite verbose and error-prone.

Instead, this addon provides some TypeScript-specific utility functions to encapsulate the type cast transparently. See the Usage section for details.

Compatibility

Installation

ember install ember-concurrency-ts

Optionally, if using ember-concurrency-async, add the following to types/<app name>/index.d.ts:

import 'ember-concurrency-async';
import 'ember-concurrency-ts/async';

Usage

taskFor

The taskFor utility function allows the code example from above to type check:

import { action } from '@ember/object';
import Component from '@glimmer/component';
import { task, TaskGenerator, timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators';
import { taskFor } from 'ember-concurrency-ts';

export default class extends Component {
  @task *myTask(ms: number): TaskGenerator<string> {
    yield timeout(ms);
    return 'done!';
  }

  @action performTask() {
    if (taskFor(this.myTask).isRunning) {
      return;
    }

    taskFor(this.myTask).perform(1000).then(value => {
      console.log(value.toUpperCase());
    });
  }
}

Instead of accessing the task directly, wrapping it in the taskFor utility function will allow TypeScript to understand what we are trying to accomplish. If this becomes repetitive, you may extract it into a variable or getter, and the code will still type check:

import { action } from '@ember/object';
import Component from '@glimmer/component';
import { TaskGenerator, timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators';
import { taskFor } from 'ember-concurrency-ts';

export default class extends Component {
  @task *myTask(ms: number): TaskGenerator<string> {
    yield timeout(ms);
    return 'done!';
  }

  @action performTask() {
    let myTask = taskFor(this.myTask);

    if (myTask.isRunning) {
      return;
    }

    myTask.perform(1000).then(value => {
      console.log(value.toUpperCase());
    });
  }
}

Note that everything on the task is type-inferred from the method definition. Based on the return type of *myTask, TypeScript knows that myTask.value is string | undefined. Likewise, it knows that myTask.perform() takes the same arguments as *myTask. Passing the wrong arguments will be a type error. It also knows that the value promise callback parameter is a string.

Alternate usage of taskFor

The taskFor utility function can also be used at assignment:

import { action } from '@ember/object';
import Component from '@glimmer/component';
import { TaskGenerator, timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators';
import { taskFor } from 'ember-concurrency-ts';

export default class extends Component {
  @task myTask = taskFor(function*(ms: number): TaskGenerator<string> {
    yield timeout(ms);
    return 'done!';
  });

  @action performTask() {
    if (this.myTask.isRunning) {
      return;
    }

    this.myTask.perform(1000).then(value => {
      console.log(value.toUpperCase());
    });
  }
}

This allows you to access the task directly without using taskFor and perform. The one caveat here is that the this type must be asserted if you are referencing this in your task:

import { action } from '@ember/object';
import Component from '@glimmer/component';
import { TaskGenerator, timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators';
import { taskFor } from 'ember-concurrency-ts';

export default class MyComponent extends Component {
  returnVal = 'done';

  @task myTask = taskFor(function*(this: MyComponent, ms: number): TaskGenerator<string> {
    yield timeout(ms);
    return this.returnVal;
  });

  @action performTask() {
    if (this.myTask.isRunning) {
      return;
    }

    this.myTask.perform(1000).then(value => {
      console.log(value.toUpperCase());
    });
  }
}

perform

As a convenience, this addon also provide a perform utility function as a shorthand for myTask(...).perform(...):

import { action } from '@ember/object';
import Component from '@glimmer/component';
import { TaskGenerator, timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators';
import { perform } from 'ember-concurrency-ts';

export default class extends Component {
  @task *myTask(ms: number): TaskGenerator<string> {
    yield timeout(ms);
    return 'done!';
  }

  @action performTask() {
    perform(this.myTask, 1000).then(value => {
      console.log(value.toUpperCase());
    });
  }
}

Just like taskFor, it infers the type information from *myTask, type checks the arguments as has the right return type, etc.

ember-concurrency-async

This addon can be used together with ember-concurrency-async, see the Installation section for additional instructions.

import { action } from '@ember/object';
import Component from '@glimmer/component';
import { timeout } from 'ember-concurrency';
import { task } from 'ember-concurrency-decorators';
import { taskFor, perform } from 'ember-concurrency-ts';

export default class extends Component {
  @task async myTask(ms: number): Promise<string> {
    await timeout(ms);
    return 'done!';
  }

  @action performTask() {
    if (taskFor(this.myTask).isRunning) {
      return;
    }

    perform(this.myTask, 1000).then(value => {
      console.log(value.toUpperCase());
    });
  }
}

Type Safety

Under-the-hood, these utility functions are just implemented as unsafe type casts. For example, the examples will still type check if the @task decorator is omitted (so this.myTask is just a regular generator or async method), but you will get an error at runtime.

Contributing

See the Contributing guide for details.

License

This project is licensed under the MIT License.