rlemaigre / vue3-promise-dialog

Dialogs meet promises in Vue 3 !
MIT License
69 stars 14 forks source link

Feat: resolve / reject dialogs #14

Open bpolaszek opened 12 months ago

bpolaszek commented 12 months ago

I suggest an API change to be able to reject the underlying promise of a dialog, by calling either resolveDialog() or rejectDialog() instead of closeDialog().

When rejectDialog() is invoked (when clicking on a cancel button, for instance), it rejects the underlying promise with a DismissedDialog error.

Useful when chaining dialogs, this allows turning this:

async function addBook() {
  let book = await openBookNameDialog(BookNameDialog, {});
  if (null === book) {
    return;
  }

  book = await openBookISBNDialog(BookISBNDialog, {book});

  if (null === book) {
    return;
  }

  book = await openBookAuthorDialog(BookAuthorDialog, {book});

  if (null === book) {
    return;
  }

  books.add(book)
}

into this:

async function addBook() {
  try {
    let book = await openBookNameDialog(BookNameDialog, {});
    book = await openBookISBNDialog(BookISBNDialog, {book});
    book = await openBookAuthorDialog(BookAuthorDialog, {book});
    books.add(book);
  } catch (e) {
    if (!(e instanceof DismissedDialog)) {
      throw e;
    }
  }
}

I kept closeDialog() for BC - when you pass an Error into closeDialog(), it will internally call rejectDialog().