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

Parent task might get canceled before the child task #13

Open MartinMalinda opened 4 years ago

MartinMalinda commented 4 years ago

When a task calls another tasks, it might do something like this, perhaps when doing an optimistic update:

export function useFollowTask(user) {
  const saveTask = useSaveUserTask();
  const followTask = useTask(function* (signal) {
    const newState = store.toggleSomeState(user);
    try {
      yield saveTask.perform({
       user
      });
    } catch (e) {
      store.toggleSomeState(user); // toggle back
      notifyError({
        title: 'Action was unsuccessful',
        text: e.message,
      });
    }
  }).drop();
}

On unMount the followTask might get cancelled before the saveTask and therefore, upon unMount cancelation the catch is not invoked. In this example, the optimistic update then does not get reverted.

Since saveTask is declared before followTask it should be cancelled first, but probably the order is not reliable enough.

Workaround for this particular case:

    signal.pr.catch(() => {
      store.toggleProfileFollowState(profile);
    });

But it shouldn't be needed.