MartinMalinda / vue-concurrency

A library for encapsulating asynchronous operations and managing concurrency for Vue and Composition API.
https://vue-concurrency.netlify.app/
MIT License
352 stars 15 forks source link

Default concurrency policy config #106

Closed Lionad-Morotar closed 4 months ago

Lionad-Morotar commented 4 months ago

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 commented 4 months ago

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 https://github.com/MartinMalinda/vue-concurrency/issues/50 but so far it's missing.

Lionad-Morotar commented 4 months ago

Thank you! My problem is solved now.

glad to see if theres a global config in new version