machty / ember-concurrency

ember-concurrency is an Ember Addon that enables you to write concise, worry-free, cancelable, restartable, asynchronous tasks.
http://ember-concurrency.com
MIT License
689 stars 157 forks source link

Typescript question for creating custom task modifiers #513

Open LanceStasinski opened 1 year ago

LanceStasinski commented 1 year ago

Hello, I've added a custom task modifier to restore the functionality of waitFor from @ember/test-waiters. Do you have any suggestions for improving the types for this custom modifier? Thank you in advance!

import { TaskFactory, registerModifier } from 'ember-concurrency'
import { waitFor } from '@ember/test-waiters'

declare module 'ember-concurrency' {
  interface TaskFactory<T, Args> {
    taskDefinition: Function & TaskDefinition<T, Args>
  }
}

function waitForTask<T, Args extends unknown[]>(taskFactory: TaskFactory<T, Args>, option: boolean) {
  if (!option) {
    return
  }

  const { taskDefinition } = taskFactory
  const waitForTaskDefinition = waitFor(function* (this: unknown, ...args: Args) {
    yield* taskDefinition.apply(this, args)
  })

  taskFactory.setTaskDefinition(waitForTaskDefinition)
}

export function initialize() {
  registerModifier('waitFor', waitForTask)
}

export default {
  initialize,
}
SergeAstapov commented 1 year ago

that will be great to bring this functionality back. I wonder if it's worth to built into ember-concurrency itself