Skip to content

Parallel Tasks #108

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

Open
mybigman opened this issue Apr 29, 2025 · 1 comment
Open

Parallel Tasks #108

mybigman opened this issue Apr 29, 2025 · 1 comment

Comments

@mybigman
Copy link

Hopefully this project isnt dead as its quite nice.

With useParallelTask using your example.

const parallelTask = useParallelTask(taskA, taskB);

It appears I cant access the tasks by names which seems problematic as it means I have to check the order providing they go in and come out the same.

Would be nice if you can access them by name?

const tasksA = ref([])
const tasksB = ref([])
parallelTask.perform().then((data) => {
  tasksA.value = data[0] // data.taskA
  tasksB.value = data[1] // data.taskB
})
@MartinMalinda
Copy link
Owner

MartinMalinda commented May 8, 2025

Hi @mybigman the reason it works like this is because it's analogous to Promise.all which works with arrays and outputs arrays.

on the other hand I think it might be possible to create a different higher order function that would accept by name and also output by name

ai is giving me this adjustment:

export function useParallelTask<RecordType extends Record<string, any>>(...tasks: Task<any, any>[]): Task<RecordType, any> {
  return useTask(function*(signal, ...args) {
    // Perform each task and attach cancellation signal
    const instances = tasks.map((task) => {
      const instance = task.perform(...args).canceledOn(signal);
      return { name: task.name, instance };
    });

    // Wait for all tasks to complete
    const results = yield Promise.all(instances.map(item => item.instance));

    // Map results back to object with task names as keys
    const namedResults = {} as RecordType;
    instances.forEach((item, index) => {
      (namedResults as any)[item.name] = results[index];
    });

    return namedResults;
  });
}

Perhaps this version might work for you? (hope it actually works 🙏️ I haven't given ran it )

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