Skip to content
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

fix(notification): fix notification, replace flushSync #2597

Merged
merged 1 commit into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions src/notification/NotificationList.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import React, { forwardRef, useImperativeHandle, useState, useEffect } from 'react';
import { flushSync } from 'react-dom';
import { render } from '../_util/react-render';
import useConfig from '../hooks/useConfig';
import {
Expand Down Expand Up @@ -86,9 +85,11 @@ const NotificationList = forwardRef<NotificationListInstance, NotificationListPr
]);

return new Promise((resolve) => {
requestAnimationFrame(() => {
// setTimeout replace requestAnimationFrame
// 在useEffect启动关闭Notification时,requestAnimationFrame可能会过早执行,导致ref.current为undefined
setTimeout(() => {
resolve(ref.current);
});
}, 1000 / 60);
});
};

Expand Down Expand Up @@ -127,29 +128,39 @@ const NotificationList = forwardRef<NotificationListInstance, NotificationListPr
);
});

// 判断多个Notification同时执行
let renderNotification = false;

export const fetchListInstance = (
placement: NotificationPlacementList,
attach: HTMLElement,
zIndex: number,
): Promise<NotificationListInstance> =>
new Promise((resolve) => {
// Fix the bug of Notification triggered for the first time in React 18 concurrent mode
flushSync(() => {
function idle() {
if (listMap.has(placement)) {
resolve(listMap.get(placement));
} else {
return;
}
if (!renderNotification) {
renderNotification = true;
render(
<NotificationList
attach={attach}
placement={placement}
zIndex={Number(zIndex)}
renderCallback={(instance) => {
renderNotification = false;
listMap.set(placement, instance);
resolve(instance);
}}
/>,
attach,
);
}
});
return;
} // 循环执行,直到NotificationList的回调执行
setTimeout(idle, 1000 / 60);
}
idle();
});
2 changes: 1 addition & 1 deletion src/notification/NotificationPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ NotificationPlugin.success = (options) => renderNotification('success', options)
NotificationPlugin.warning = (options) => renderNotification('warning', options);
NotificationPlugin.error = (options) => renderNotification('error', options);
NotificationPlugin.close = (promise) => promise.then((instance) => instance.close());
NotificationPlugin.closeAll = () => listMap.forEach((list) => list.removeAll());
NotificationPlugin.closeAll = () => [...listMap.values()].forEach((list) => list.removeAll());
NotificationPlugin.config = (options) => setGlobalConfig(options);
Loading