Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add types definitions that also support ember-concurrency-decorators #355

Closed

Conversation

NullVoxPopuli
Copy link

@NullVoxPopuli NullVoxPopuli commented Apr 29, 2020

After initial review

  • review Add Type Definitions #319, and address feedback from that PR
  • add some tests
    • Octane App with ember-concurrency-decorators
    • Classic App with classic function wrappers
  • de-namespace, as we can just export straight from this file, as TS already handles the namespacing because the index.d.ts will be in node-modules

Huge thanks to @buschtoens for the work done in #319

these types enable the following, given these helper functions:

import Task from 'ember-concurrency/task';

type ECTask<Args extends Array<any>, Return> = (
  ...args: Args
) => Generator<any /* potentially yielded types */, Return, unknown>;

export function taskFor<Args extends any[], Return = void>(generatorFn: ECTask<Args, Return>) {
  return (generatorFn as any) as Task<Args, Return>;
}

we can have inferred types from ember-concurrency-decorators!
image

image

@NullVoxPopuli NullVoxPopuli changed the title add types for ember-concurrency add types definitions that also support ember-concurrency-decorators Apr 29, 2020
Copy link
Collaborator

@maxfierke maxfierke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a TypeScript user, so I don't have a very sophisticated understanding of TypeScript type definitions, so apologies for some fairly basic questions.

I also have some sort of broad questions:

  • Could you provide some details on what's different here versus Add Type Definitions #319?
    • Does it address feedback raised there?
  • Are there TypeScript version considerations?
  • Is there a way to test/validate these (something that could be thrown in CI)?

The latter would make this more likely to be merged.

restartable(): TaskDecorator;
maxConcurrency(num: number): TaskDecorator;
enqueue(): TaskDecorator;
withTestWaiter(): PropertyDecorator;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is defined by ember-test-waiters. perhaps this should live there? (is that a possible thing?)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe? but should https://github.com/bendemboski/ember-concurrency-test-waiter get pulled in to this addon since it's a useful tool to help in testing with tasks?

restartable(): TaskDecorator;
maxConcurrency(num: number): TaskDecorator;
enqueue(): TaskDecorator;
withTestWaiter(): PropertyDecorator;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same q

Comment on lines +31 to +35
export enum TaskState {
RUNNING = 'running',
QUEUED = 'queued',
IDLE = 'idle',
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My understanding based on some feedback in #319, is that we should not export this in a type definition, since there is no such enum available to import.

Comment on lines +139 to +154
export enum TaskInstanceState {
/** task instance was canceled before it started */
DROPPED = 'dopped',

/** task instance was canceled before it could finish */
CANCELED = 'canceled',

/** task instance ran to completion (even if an exception was thrown) */
FINISHED = 'finished',

/** task instance is currently running (returns true even if is paused on a yielded promise) */
RUNNING = 'running',

/** task instance hasn't begun running yet (usually because the task is using the `TaskProperty#enqueue` task modifier) */
WAITING = 'waiting',
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same deal here, re: exporting of an enum

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, these probably don't need to be exported -- this is mostly copy paste from long ago

};

export function task(
generator: Function
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be a GeneratorFunction (or w/e the TypeScript-equivalent type name is)?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

depends on if any type inference could be gained from doing so, like how I'm getting inference with decorators

/**
* The most recently started task instance.
*/
readonly last: TaskInstance<PerformArgs, PerformReturn>;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this (and the other last* properties) can also be null. Do these need to be denoted as nullable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh! yes, you're absolutely right. (though is it null or undefined?)

@NullVoxPopuli
Copy link
Author

Could you provide some details on what's different here versus #319?

the main difference is that I've added some generics that help with type inference with decorators (see screenshots in original comment)

Does it address feedback raised there?

nope, I need to look at that

Are there TypeScript version considerations?

no clue, I'm only using 3.7.x in my apps

Is there a way to test/validate these (something that could be thrown in CI)?

yea - we could build this in to the repo with full ember apps that aren't the dummy app, and try to use all of the APIs and make sure there are no type errors?

};
export function timeout(wait: number): Promise<void>;

export function didCancel(error: Error): boolean;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be export function didCancel(reason: unknown): reason is Error;. In a promise callback, I don't think you necessarily know the reject reason object is an Error, and so if you require this parameter to be Error TypeScript might not let you pass it.

@maxfierke
Copy link
Collaborator

Thanks so much for your work on this! However, we're closing this in favor of #357, which has been merged and will be part of 1.2.0

@maxfierke maxfierke closed this Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants