Skip to content

Commit

Permalink
Allow accessing encapsulated task state
Browse files Browse the repository at this point in the history
  • Loading branch information
chancancode committed Jun 19, 2020
1 parent ac75e47 commit 55fc088
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 11 deletions.
41 changes: 34 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import { Task, task, taskGroup } from 'ember-concurrency';
import {
Task,
TaskInstance,
EncapsulatedTask,
EncapsulatedTaskProperty,
EncapsulatedTaskState,
task,
taskGroup,
} from 'ember-concurrency';

declare module 'ember-concurrency' {
export type AsyncTaskFunction<T, Args extends any[]> = (...args: Args) => Promise<T>;
Expand All @@ -9,21 +17,40 @@ declare module 'ember-concurrency' {
export type AsyncTaskFunctionReturnType<T extends AsyncTaskFunction<any, any[]>> =
T extends (...args: any[]) => Promise<infer R> ? R : unknown;

export type TaskForAsyncTaskFunction<T extends AsyncTaskFunction<any, any[]>> =
Task<AsyncTaskFunctionReturnType<T>, AsyncTaskFunctionArgs<T>>;

export type TaskInstanceForAsyncTaskFunction<T extends AsyncTaskFunction<any, any[]>> =
TaskInstance<AsyncTaskFunctionReturnType<T>>;

export interface AsyncEncapsulatedTaskDescriptor<T, Args extends any[]> {
perform(...args: Args): Promise<T>;
}

export type AsyncEncapsulatedTaskDescriptorArgs<T extends AsyncEncapsulatedTaskDescriptor<any, any[]>> =
T extends (...args: infer A) => Promise<any> ? A : [];
T extends { perform(...args: infer A): Promise<any> } ? A : [];

export type AsyncEncapsulatedTaskDescriptorReturnType<T extends AsyncEncapsulatedTaskDescriptor<any, any[]>> =
T extends (...args: any[]) => Promise<infer R> ? R : unknown;
T extends { perform(...args: any[]): Promise<infer R> } ? R : unknown;

export type AsyncEncapsulatedTaskState<T extends object> = EncapsulatedTaskState<T>;

export type TaskForAsyncEncapsulatedTaskDescriptor<T extends AsyncEncapsulatedTaskDescriptor<any, any[]>> =
EncapsulatedTask<
AsyncEncapsulatedTaskDescriptorReturnType<T>,
AsyncEncapsulatedTaskDescriptorArgs<T>,
AsyncEncapsulatedTaskState<T>
>;

export type TaskInstanceForAsyncEncapsulatedTaskDescriptor<T extends AsyncEncapsulatedTaskDescriptor<any, any[]>> =
TaskInstance<AsyncEncapsulatedTaskDescriptorReturnType<T>> & AsyncEncapsulatedTaskDescriptorArgs<T>;

function task<T extends AsyncTaskFunction<any, any[]>>(taskFn: T):
TaskProperty<AsyncTaskFunctionReturnType<T>, AsyncTaskFunctionArgs<T>>;
function task<T extends AsyncEncapsulatedTaskDescriptor<any, any[]>>(taskFn: T):
TaskProperty<AsyncEncapsulatedTaskDescriptorReturnType<T>, AsyncEncapsulatedTaskDescriptorArgs<T>>;

function taskGroup<T extends AsyncTaskFunction<any, any[]>>(taskFn: T):
TaskGroup<AsyncTaskFunctionReturnType<T>>;
EncapsulatedTaskProperty<
AsyncEncapsulatedTaskDescriptorReturnType<T>,
AsyncEncapsulatedTaskDescriptorArgs<T>,
AsyncEncapsulatedTaskState<T>
>;
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
"ember-cli-babel-plugin-helpers": "^1.1.0",
"ember-cli-htmlbars": "^4.3.1"
},
"peerDependencies": {
"ember-concurrency": "^1.2.1"
},
"devDependencies": {
"@babel/core": "^7.10.2",
"@babel/plugin-syntax-class-properties": "^7.10.1",
Expand All @@ -49,7 +52,7 @@
"ember-cli-inject-live-reload": "^2.0.2",
"ember-cli-sri": "^2.1.1",
"ember-cli-uglify": "^3.0.0",
"ember-concurrency": "^1.2.0",
"ember-concurrency": "chancancode/ember-concurrency#encapsulated-task-v2",
"ember-concurrency-decorators": "^2.0.0",
"ember-disable-prototype-extensions": "^1.1.3",
"ember-export-application-global": "^2.0.1",
Expand Down
5 changes: 2 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5113,10 +5113,9 @@ ember-concurrency-decorators@^2.0.0:
ember-cli-htmlbars "^4.3.1"
ember-cli-typescript "^3.1.4"

ember-concurrency@^1.2.0:
ember-concurrency@chancancode/ember-concurrency#encapsulated-task-v2:
version "1.2.0"
resolved "https://registry.yarnpkg.com/ember-concurrency/-/ember-concurrency-1.2.0.tgz#f8d09ff035ba7652f01de198b9e16424ef2b8522"
integrity sha512-XEKleKlcJQOTbMHEbEtIYF/VXSJDVB8H3pRFY5qmGqGujvpLaJY0GCQ6xtnTDdmSDkBO77FJ9o36GL7iwyqn6g==
resolved "https://codeload.github.com/chancancode/ember-concurrency/tar.gz/6b9cc1691e5689fb102ccd175f68cbeee558a6bb"
dependencies:
ember-cli-babel "^7.7.3"
ember-compatibility-helpers "^1.2.0"
Expand Down

0 comments on commit 55fc088

Please sign in to comment.