UNPKG

rsuite

Version:

A suite of react components

95 lines (89 loc) 3.97 kB
'use client'; "use strict"; exports.__esModule = true; exports.default = void 0; var _ToastContainer = _interopRequireWildcard(require("./ToastContainer")); var _symbols = require("../internals/symbols"); var _utils = require("../internals/utils"); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); } const containers = new Map(); /** * Track in-progress container creation promises keyed by `${containerId}_${placement}`. * This prevents duplicate containers from being created when `push` is called multiple * times synchronously (e.g. inside a loop) before the first container has mounted. */ const pendingContainerPromises = new Map(); /** * Create a container instance. * @param placement * @param props */ async function createContainer(placement, props) { const [container, containerId] = await _ToastContainer.default.getInstance(props); const key = `${containerId}_${placement}`; containers.set(key, container); pendingContainerPromises.delete(key); return container; } /** * Get the container by ID. Use default ID when ID is not available. * @param containerId * @param placement */ function getContainer(containerId, placement) { return containers.get(`${containerId}_${placement}`); } const toaster = message => toaster.push(message); toaster.push = (message, options = {}) => { const { placement = 'topCenter', container = _ToastContainer.defaultToasterContainer, ...restOptions } = options; const containerElement = typeof container === 'function' ? container() : container; if (containerElement) { // Pre-assign the container ID so subsequent synchronous calls can find it // before the async container creation has completed. if (!containerElement[_symbols.RSUITE_TOASTER_ID]) { containerElement[_symbols.RSUITE_TOASTER_ID] = (0, _utils.guid)(); } const containerElementId = containerElement[_symbols.RSUITE_TOASTER_ID]; const key = `${containerElementId}_${placement}`; const existedContainer = getContainer(containerElementId, placement); if (existedContainer) { return existedContainer.current?.push(message, restOptions); } // A container creation for this placement may already be in progress (e.g. when `push` // is called multiple times synchronously in a loop). Reuse that promise instead of // creating a second container. const pendingPromise = pendingContainerPromises.get(key); if (pendingPromise) { return pendingPromise.then(ref => ref.current?.push(message, restOptions)); } const newOptions = { ...options, container: containerElement, placement }; const containerPromise = createContainer(placement, newOptions); // Register the pending promise before any async work begins so that subsequent // synchronous `push` calls for the same placement chain onto it. pendingContainerPromises.set(key, containerPromise); return containerPromise.then(ref => ref.current?.push(message, restOptions)); } const newOptions = { ...options, container: containerElement, placement }; return createContainer(placement, newOptions).then(ref => { return ref.current?.push(message, restOptions); }); }; toaster.remove = key => { containers.forEach(c => c.current?.remove(key)); }; toaster.clear = () => { containers.forEach(c => c.current?.clear()); }; var _default = exports.default = toaster;