Skip to content

Commit

Permalink
[duoyun-ui] Ignore modal reject toast message
Browse files Browse the repository at this point in the history
  • Loading branch information
mantou132 committed Feb 22, 2024
1 parent fb8f799 commit a6664a6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/duoyun-ui/src/elements/modal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { mediaQuery } from '@mantou/gem/helper/mediaquery';
import { theme } from '../lib/theme';
import { locale } from '../lib/locale';
import { hotkeys } from '../lib/hotkeys';
import { DyPromise } from '../lib/utils';
import { DyPromise, ignoredPromiseReasonSet } from '../lib/utils';
import { setBodyInert } from '../lib/element';
import { commonAnimationOptions, fadeIn, fadeOut, slideInUp } from '../lib/animations';

Expand Down Expand Up @@ -183,7 +183,6 @@ export class DuoyunModalElement extends GemElement {
@state closing: boolean;

// Cannot be used for dynamic forms
// 错误必须处理,不然会被默认通过 Toast 显示
static open<T = Element>(options: ModalOptions & ModalOpenOptions<T>) {
const modal = new this({ ...options, open: true });
const restoreInert = setBodyInert(modal);
Expand All @@ -198,6 +197,7 @@ export class DuoyunModalElement extends GemElement {
modal.addEventListener('close', async () => {
const ele = getBodyEle();
await options.prepareClose?.(ele);
ignoredPromiseReasonSet.add(ele);
rej(ele);
});
modal.addEventListener('ok', async () => {
Expand Down
5 changes: 5 additions & 0 deletions packages/duoyun-ui/src/helper/error.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Toast } from '../elements/toast';
import { ignoredPromiseReasonSet } from '../lib/utils';

let unloading = false;
addEventListener('beforeunload', () => {
Expand All @@ -21,6 +22,10 @@ function printError(err: Error | ErrorEvent | DOMException) {
}

function handleRejection({ reason }: PromiseRejectionEvent) {
if (ignoredPromiseReasonSet.has(reason)) {
ignoredPromiseReasonSet.delete(reason);
return;
}
if (reason) {
const errors = reason.errors || reason;
if (Array.isArray(errors)) {
Expand Down
4 changes: 4 additions & 0 deletions packages/duoyun-ui/src/lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,3 +297,7 @@ export class DyPromise<T, E extends Record<string, unknown>> extends Promise<T>
return Object.assign(result, this) as unknown as DyPromise<T, E> & E;
}
}

// 不应该以发生错误的方式来处理拒绝的 Promise
// 缺点:同时有个相同原因的 Promise 需要用错误处理时会被忽略
export const ignoredPromiseReasonSet = new WeakSet();

0 comments on commit a6664a6

Please sign in to comment.