Skip to content

Commit

Permalink
Add type tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Jun 7, 2020
1 parent 8bda148 commit 09a8e42
Show file tree
Hide file tree
Showing 6 changed files with 1,542 additions and 15 deletions.
35 changes: 21 additions & 14 deletions addon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ export type TaskCancelation = Error & { name: 'TaskCancelation' };

export type Yieldable<T = void> = PromiseLike<T>;

export type Evented = {
type Evented = {
on(event: string, callback: (...args: any[]) => void): void;
off(event: string, callback: (...args: any[]) => void): void;
} | {
Expand All @@ -486,6 +486,12 @@ export type Evented = {
removeEventListener(event: string, callback: (...args: any[]) => void): void;
};

type Resolved<T> = T extends PromiseLike<infer R> ? R : T;

type Settlement<T> = { state: 'fulfilled', value: T } | { state: 'rejected', reason: any };

type Settled<T> = Settlement<Resolved<T>>;

/**
* A Task is a cancelable, restartable, asynchronous operation that
* is driven by a generator function. Tasks are automatically canceled
Expand Down Expand Up @@ -572,9 +578,10 @@ export function taskGroup<T extends TaskFunction<any, any[]>>(taskFn: T):
*
* [Check out the "Awaiting Multiple Child Tasks example"](/docs/examples/joining-tasks)
*/
export function all<T>(
values: Iterable<T | TaskInstance<T> | PromiseLike<T>>
): Promise<T[]>;
export function all<T extends readonly unknown[] | readonly [unknown]>(
values: T
): Promise<{ -readonly [K in keyof T]: Resolved<T[K]> }>;
export function all<T>(values: Iterable<T>): Promise<Array<Resolved<T>>>;

/**
* A cancelation-aware variant of [RSVP.allSettled](http://emberjs.com/api/classes/RSVP.html#method_allSettled).
Expand All @@ -585,10 +592,10 @@ export function all<T>(
* - if the task that `yield`ed `allSettled()` is canceled, any of the
* {@linkcode TaskInstance}s passed in to `allSettled` will be canceled
*/
export function allSettled<T>(
values: Iterable<T | TaskInstance<T> | PromiseLike<T>>
): Promise<T[]>;

export function allSettled<T extends readonly unknown[] | readonly [unknown]>(
values: T
): Promise<{ -readonly [K in keyof T]: Settled<T[K]> }>;
export function allSettled<T>(values: Iterable<T>): Promise<Array<Settled<T>>>;

/**
* Returns true if the object passed to it is a TaskCancelation error.
Expand Down Expand Up @@ -621,9 +628,10 @@ export function didCancel(error: unknown): error is TaskCancelation;
* - if any of the items rejects/cancels, all other cancelable items
* (e.g. {@linkcode TaskInstance}s) will be canceled
*/
export function hash<T>(
values: Record<string, T | TaskInstance<T> | PromiseLike<T>>
): Promise<Record<string, T>>;
export function hash<T extends Record<string, unknown>>(
values: T
): Promise<{ [K in keyof T]: Resolved<T[K]> }>;
export function hash<T>(values: Record<string, T>): Promise<Record<string, Resolved<T>>>;

/**
* A cancelation-aware variant of [Promise.race](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race).
Expand All @@ -638,9 +646,8 @@ export function hash<T>(
*
* [Check out the "Awaiting Multiple Child Tasks example"](/docs/examples/joining-tasks)
*/
export function race<T>(
values: Iterable<T | TaskInstance<T> | PromiseLike<T>>
): Promise<T>;
export function race<T>(values: readonly T[]): Promise<Resolved<T>>;
export function race<T>(values: Iterable<T>): Promise<Resolved<T>>;

/**
* Yielding `timeout(ms)` will pause a task for the duration
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,13 @@
"eslint": "^6.5.1",
"eslint-plugin-ember": "^6.2.0",
"eslint-plugin-node": "^9.0.1",
"expect-type": "^0.7.4",
"jsdom": "^11.6.2",
"loader.js": "^4.7.0",
"prember": "0.3.0-alpha.3",
"qunit-dom": "^0.8.4",
"sass": "^1.22.12"
"sass": "^1.22.12",
"typescript": "~3.9.5"
},
"keywords": [
"ember-addon",
Expand Down
Loading

0 comments on commit 09a8e42

Please sign in to comment.