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

Default concurrency policy config #106

Closed
Lionad-Morotar opened this issue Jun 21, 2024 · 2 comments
Closed

Default concurrency policy config #106

Lionad-Morotar opened this issue Jun 21, 2024 · 2 comments

Comments

@Lionad-Morotar
Copy link

Lionad-Morotar commented Jun 21, 2024

Hello,how can I use drop as default concurrency policy? so that I can ignore .drop() on each useTask call.

const xTask = useTask(function*(){
  /* xxx */
})
// no need for this little tail
.drop()

Also,thanks for this great tool

@MartinMalinda
Copy link
Owner

MartinMalinda commented Jun 21, 2024

Hey!

You can create a wrapper like this:

type Generator = Parameters<typeof useTask>[0];
export function useDropTask(cb: Generator) {
  return useTask(cb).drop();
}

I'd calll it useDropTask but since you likely want to save typing you could call it useTask and import the original as something else:

import { useTask as useTaskOriginal } from 'vue-concurrency';
type Generator = Parameters<typeof useTaskOriginal>[0];
export function useTask(cb: Generator) {
  return useTaskOriginal(cb).drop();
}

But the big issue here is that useTask().restartable() will not turn off the drop() so you'll end up with both which does not make that much sense.

So at the end of the day you could go for this setting:

import { useTask as useTaskOriginal } from 'vue-concurrency';
type Generator = Parameters<typeof useTaskOriginal>[0];
export function useTask(cb: Generator, concurrency: 'drop' | 'restartable' | 'keepLatest' | null = 'drop') {
  const task =  useTaskOriginal(cb);
  if (concurrency) {
    task[concurrency]();
  }
}

const myTask = useTask(function*(){
 //...
}); // drop
const myOtherTask = useTask(function*() {
 // ...
}, 'restartable').

const myOtherTask = useTask(function*() {
 // ...
}, null). // this one allows parallel runs

I'm not sure if any of these are ideal :D There could totally be global config as that could also solve #50 but so far it's missing.

@Lionad-Morotar
Copy link
Author

Lionad-Morotar commented Jun 25, 2024

Thank you! My problem is solved now.

glad to see if theres a global config in new version

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

No branches or pull requests

2 participants