buschtoens / ember-concurrency-typescript Goto Github PK
View Code? Open in Web Editor NEWember-concurrency + ember-cli-typescript + native classes = type safe 🥳
License: MIT License
ember-concurrency + ember-cli-typescript + native classes = type safe 🥳
License: MIT License
Unfortunately we cannot support task modifiers as easily as we planned initially in machty/ember-concurrency-decorators#50.
class Foo {
doStuff = task(function*(this: Foo) {
// ...
}).restartable();
perform() {
this.doStuff.perform();
}
}
Our type definition currently basically looks like this:
declare module 'ember-concurrency' {
export function task<Args extends any[], R>(
task: GeneratorFn<Args, R>
): Task<Args, Exclude<R, Promise<any>>>;
export interface TaskProperty {
restartable: () => TaskProperty;
drop: () => TaskProperty;
keepLatest: () => TaskProperty;
enqueue: () => TaskProperty;
maxConcurrency: (n: number) => TaskProperty;
cancelOn: (eventName: string) => TaskProperty;
group: (groupName: string) => TaskProperty;
evented: () => TaskProperty;
debug: () => TaskProperty;
on: (eventName: string) => TaskProperty;
}
export interface Task<Args extends any[], T> {
readonly isIdle: boolean;
readonly isQueued: boolean;
readonly isRunning: boolean;
readonly last?: TaskInstance<T>;
readonly lastCanceled?: TaskInstance<T>;
readonly lastComplete?: TaskInstance<T>;
readonly lastErrored?: TaskInstance<T>;
readonly lastIncomplete?: TaskInstance<T>;
readonly lastPerformed?: TaskInstance<T>;
readonly lastRunning?: TaskInstance<T>;
readonly lastSuccessful?: TaskInstance<T>;
readonly performCount: number;
readonly state: TaskState;
perform(...args: Args): TaskInstance<T>;
cancelAll(): void;
}
export interface TaskInstance<T> extends PromiseLike<T> {
readonly error?: unknown;
readonly hasStarted: boolean;
readonly isCanceled: boolean;
readonly isDropped: boolean;
readonly isError: boolean;
readonly isFinished: boolean;
readonly isRunning: boolean;
readonly isSuccessful: boolean;
readonly state: TaskInstanceState;
readonly value?: T;
cancel(): void;
catch(): RSVP.Promise<unknown>;
finally(): RSVP.Promise<unknown>;
then<TResult1 = T, TResult2 = never>(
onfulfilled?:
| ((value: T) => TResult1 | RSVP.Promise<TResult1>)
| undefined
| null,
onrejected?:
| ((reason: any) => TResult2 | PromiseLike<TResult2>)
| undefined
| null
): RSVP.Promise<TResult1 | TResult2>;
}
}
The issue is that task
returns a Task
, so that can call perform
etc. on the class property.
However, this means that you can't call .restartable()
etc. (from TaskProperty
) in the class property assignment.
I see the following solutions:
task
to return a Task & TaskInstance
, which loosens the type safety. 😦@restartable
someTask = task(function*() { ... });
someTask = task({ restartable: true }, function*() { ... });
someTask = task(function*() { ... }).restartable().task;
regularTask = task(function*() { ... }).task;
someTask = task(function*() { ... }, t => t.restartable());
someTask = task(function*() { ... }, task.restartable());
We should copy the descriptions from the ember-concurrency API docs for nice inline IDE docs.
If used in a mixed project (like one transitioning to typescript), fails hard when tasks are present in classic classes.
For example
test('Classic class', async function(assert) {
const subject = EmberObject.create({
testTask: task(function*() {
yield timeout(1);
}),
async perform() {
await timeout(1);
return 'done';
}
})
const value = await subject.perform();
assert.equal(value, 'done');
});
results in
Build Error (broccoli-persistent-filter:Babel > [Babel: ember-concurrency-typescript]) in dummy/tests/unit/task-test.ts
/addons/ember-concurrency-typescript/dummy/tests/unit/task-test.ts: `task` has to be a direct child of a `ClassProperty`.
30 | test('Classic class', async function(assert) {
31 | const subject = EmberObject.create({
> 32 | testTask: task(function*() {
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.