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 waitFor support to async arrow function API #531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions addon/-private/async-arrow-runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ export function buildTask(contextFn, options, taskName, bufferPolicyName) {

const result = contextFn();

if (optionsWithBufferPolicy && optionsWithBufferPolicy.waitFor) {
result.generator = optionsWithBufferPolicy.waitFor(result.generator);

optionsWithBufferPolicy = Object.assign({}, optionsWithBufferPolicy);
delete optionsWithBufferPolicy.waitFor;
}

Comment on lines +19 to +25
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this probably should be moved into a modifier (a la #513), so that it works regardless of whether a task is running through the AyncArrowRuntime or not

const taskFactory = new TaskFactory(
taskName || '<unknown>',
result.generator,
Expand Down
22 changes: 16 additions & 6 deletions addon/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,6 +796,16 @@ type OptionTypeFor<T, F> = F extends (...args: infer Args) => T
type TaskOptions = OptionsFor<TaskProperty<unknown, unknown[]>>;
type TaskGroupOptions = OptionsFor<TaskGroupProperty<unknown>>;

type AsyncArrowFunctionTaskOptions<
HostObject,
T,
Args extends any[]
> = TaskOptions & {
waitFor?: (
fn: AsyncArrowTaskFunction<HostObject, T, Args>
) => AsyncArrowTaskFunction<HostObject, T, Args>;
};

Comment on lines +799 to +808
Copy link
Collaborator

Choose a reason for hiding this comment

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

This might not be needed to be special-cased if moved to a waitFor modifier

type MethodOrPropertyDecoratorWithParams<Params extends unknown[]> =
MethodDecorator &
PropertyDecorator &
Expand Down Expand Up @@ -919,7 +929,7 @@ export function task<

export function task<
HostObject,
O extends TaskOptions,
O extends AsyncArrowFunctionTaskOptions<HostObject, any, any[]>,
T extends AsyncArrowTaskFunction<HostObject, any, any[]>
>(
hostObject: HostObject,
Expand All @@ -929,7 +939,7 @@ export function task<

export function task<
HostObject,
O extends TaskOptions,
O extends AsyncArrowFunctionTaskOptions<HostObject, any, any[]>,
T extends AsyncArrowTaskFunction<HostObject, any, any[]>
>(baseOptions: O, asyncArrowTaskFn: T): TaskForAsyncTaskFunction<HostObject, T>;

Expand Down Expand Up @@ -981,7 +991,7 @@ export function dropTask<
>(asyncArrowTaskFn: T): TaskForAsyncTaskFunction<HostObject, T>;
export function dropTask<
HostObject,
O extends TaskOptions,
O extends AsyncArrowFunctionTaskOptions<HostObject, any, any[]>,
T extends AsyncArrowTaskFunction<HostObject, any, any[]>
>(baseOptions: O, asyncArrowTaskFn: T): TaskForAsyncTaskFunction<HostObject, T>;

Expand Down Expand Up @@ -1030,7 +1040,7 @@ export function enqueueTask<

export function enqueueTask<
HostObject,
O extends TaskOptions,
O extends AsyncArrowFunctionTaskOptions<HostObject, any, any[]>,
T extends AsyncArrowTaskFunction<HostObject, any, any[]>
>(baseOptions: O, asyncArrowTaskFn: T): TaskForAsyncTaskFunction<HostObject, T>;

Expand Down Expand Up @@ -1079,7 +1089,7 @@ export function keepLatestTask<

export function keepLatestTask<
HostObject,
O extends TaskOptions,
O extends AsyncArrowFunctionTaskOptions<HostObject, any, any[]>,
T extends AsyncArrowTaskFunction<HostObject, any, any[]>
>(baseOptions: O, asyncArrowTaskFn: T): TaskForAsyncTaskFunction<HostObject, T>;

Expand Down Expand Up @@ -1128,7 +1138,7 @@ export function restartableTask<

export function restartableTask<
HostObject,
O extends TaskOptions,
O extends AsyncArrowFunctionTaskOptions<HostObject, any, any[]>,
T extends AsyncArrowTaskFunction<HostObject, any, any[]>
>(baseOptions: O, asyncArrowTaskFn: T): TaskForAsyncTaskFunction<HostObject, T>;

Expand Down
39 changes: 38 additions & 1 deletion tests/integration/async-arrow-task-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,14 @@ import { module, test } from 'qunit';
import { setupRenderingTest } from 'ember-qunit';
// eslint-disable-next-line ember/no-computed-properties-in-native-classes
import { computed, set } from '@ember/object';
import { click, render, settled } from '@ember/test-helpers';
import {
click,
getSettledState,
render,
settled,
waitUntil,
} from '@ember/test-helpers';
import { waitFor } from '@ember/test-waiters';
import { hbs } from 'ember-cli-htmlbars';
import {
task,
Expand Down Expand Up @@ -142,6 +149,36 @@ module('Integration | async-arrow-task', function (hooks) {
await finishTest(assert);
});

test('waitFor option', async function (assert) {
assert.expect(9);

let { promise, resolve } = defer();

this.owner.register(
'component:test-async-arrow-task',
class extends TestComponent {
myTask = task(this, { waitFor }, async (arg) => {
set(this, 'resolved', await promise);
assert.strictEqual(this.myTask.name, 'myTask');
return arg;
});
}
);

await render(hbs`<TestAsyncArrowTask />`);
click('button#start');
await waitUntil(() => this.element.textContent.includes('Running!'));

assert.true(getSettledState().hasPendingWaiters);

resolve('Wow!');
await settled();

assert.false(getSettledState().hasPendingWaiters);

await finishTest(assert);
});

test('dropTask and other shorthand tasks (with `this` arg)', async function (assert) {
assert.expect(13);

Expand Down
4 changes: 4 additions & 0 deletions tests/types/ember-concurrency-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import {
} from 'ember-concurrency';
import { expectTypeOf as expect } from 'expect-type';
import { taskFor } from 'ember-concurrency-ts';
import { waitFor } from '@ember/test-waiters';

declare type TestCallback = () => void | Promise<void>;
declare function module(description: string, callback: TestCallback): void;
Expand Down Expand Up @@ -3128,6 +3129,7 @@ module('integration tests', () => {
debug = task(this, { debug: true }, async () => {});
onState = task(this, { onState: () => {} }, async () => {});
onStateNull = task(this, { onState: null }, async () => {});
waitFor = task({ waitFor }, async () => {});
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is missing the this (otherwise, it's the same test case as the one further down):

Suggested change
waitFor = task({ waitFor }, async () => {});
waitFor = task(this, { waitFor }, async () => {});


// Note: these options work even when strictFunctionTypes is enabled, but
// turning it on in this repo breaks other things in addon/index.d.ts
Expand Down Expand Up @@ -3237,6 +3239,7 @@ module('integration tests', () => {
debug = task({ debug: true }, async () => {});
onState = task({ onState: () => {} }, async () => {});
onStateNull = task({ onState: null }, async () => {});
waitFor = task({ waitFor }, async () => {});

// Note: these options work even when strictFunctionTypes is enabled, but
// turning it on in this repo breaks other things in addon/index.d.ts
Expand Down Expand Up @@ -3409,6 +3412,7 @@ module('integration tests', () => {
debug = task({ debug: true }, async () => {});
onState = task({ onState: () => {} }, async () => {});
onStateNull = task({ onState: null }, async () => {});
waitFor = task({ waitFor }, async () => {});

// Note: these options work even when strictFunctionTypes is enabled, but
// turning it on in this repo breaks other things in addon/index.d.ts
Expand Down