@atlaskit/editor-plugin-paste-options-toolbar
Version:
Paste options toolbar for @atlaskit/editor-core
88 lines (87 loc) • 3.19 kB
JavaScript
import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
import _regeneratorRuntime from "@babel/runtime/regenerator";
/** How long to wait for each `isAsyncHidden` check before treating it as hidden. */
export var ASYNC_HIDDEN_TIMEOUT_MS = 3000;
var hasAsyncHidden = function hasAsyncHidden(component) {
return typeof component.isAsyncHidden === 'function';
};
var rejectOnTimeout = function rejectOnTimeout(promise, timeoutMs) {
var timeoutId;
var timeoutPromise = new Promise(function (_, reject) {
timeoutId = setTimeout(function () {
return reject(new Error('isAsyncHidden timeout'));
}, timeoutMs);
});
return Promise.race([promise.finally(function () {
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}
}), timeoutPromise]);
};
var evaluateComponentAsyncHidden = /*#__PURE__*/function () {
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(component, context) {
var hidden, _t;
return _regeneratorRuntime.wrap(function (_context) {
while (1) switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
_context.next = 1;
return rejectOnTimeout(component.isAsyncHidden(context), ASYNC_HIDDEN_TIMEOUT_MS);
case 1:
hidden = _context.sent;
return _context.abrupt("return", {
key: component.key,
hidden: hidden
});
case 2:
_context.prev = 2;
_t = _context["catch"](0);
return _context.abrupt("return", {
key: component.key,
hidden: true
});
case 3:
case "end":
return _context.stop();
}
}, _callee, null, [[0, 2]]);
}));
return function evaluateComponentAsyncHidden(_x, _x2) {
return _ref.apply(this, arguments);
};
}();
/**
* Evaluates all `isAsyncHidden` functions in parallel.
* Each check is raced against a timeout; timeouts and errors are hidden.
*
* Returns a plain object that maps each component key to its hidden state.
* Components without `isAsyncHidden` are not present in the result.
*/
export function evaluateAsyncHidden(_x3, _x4) {
return _evaluateAsyncHidden.apply(this, arguments);
}
function _evaluateAsyncHidden() {
_evaluateAsyncHidden = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(components, context) {
var results;
return _regeneratorRuntime.wrap(function (_context2) {
while (1) switch (_context2.prev = _context2.next) {
case 0:
_context2.next = 1;
return Promise.all(components.filter(hasAsyncHidden).map(function (component) {
return evaluateComponentAsyncHidden(component, context);
}));
case 1:
results = _context2.sent;
return _context2.abrupt("return", Object.fromEntries(results.map(function (_ref2) {
var key = _ref2.key,
hidden = _ref2.hidden;
return [key, hidden];
})));
case 2:
case "end":
return _context2.stop();
}
}, _callee2);
}));
return _evaluateAsyncHidden.apply(this, arguments);
}