import _typeof from "@babel/runtime/helpers/esm/typeof"; import _createClass from "@babel/runtime/helpers/esm/createClass"; import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck"; import _isFunction from "lodash/isFunction"; import _once from "lodash/once"; import _snakeCase from "lodash/snakeCase"; import { version } from './version'; export function toArray(array) { return Array.isArray(array) ? array : [array]; } export function sleep(ms) { return new Promise(function (resolve) { return setTimeout(resolve, ms); }); } // Promise.then might be synchronized in Zone.js context, we need to use setTimeout instead to mock next tick. var nextTick = typeof window.Zone === 'function' ? setTimeout : function (cb) { return Promise.resolve().then(cb); }; var globalTaskPending = false; /** * Run a callback before next task executing, and the invocation is idempotent in every singular task * That means even we called nextTask multi times in one task, only the first callback will be pushed to nextTick to be invoked. * @param cb */ export function nextTask(cb) { if (!globalTaskPending) { globalTaskPending = true; nextTick(function () { cb(); globalTaskPending = false; }); } } var fnRegexCheckCacheMap = new WeakMap(); export function isConstructable(fn) { // prototype methods might be changed while code running, so we need check it every time var hasPrototypeMethods = fn.prototype && fn.prototype.constructor === fn && Object.getOwnPropertyNames(fn.prototype).length > 1; if (hasPrototypeMethods) return true; if (fnRegexCheckCacheMap.has(fn)) { return fnRegexCheckCacheMap.get(fn); } /* 1. 有 prototype 并且 prototype 上有定义一系列非 constructor 属性 2. 函数名大写开头 3. class 函数 满足其一则可认定为构造函数 */ var constructable = hasPrototypeMethods; if (!constructable) { // fn.toString has a significant performance overhead, if hasPrototypeMethods check not passed, we will check the function string with regex var fnString = fn.toString(); var constructableFunctionRegex = /^function\b\s[A-Z].*/; var classRegex = /^class\b/; constructable = constructableFunctionRegex.test(fnString) || classRegex.test(fnString); } fnRegexCheckCacheMap.set(fn, constructable); return constructable; } /** * in safari * typeof document.all === 'undefined' // true * typeof document.all === 'function' // true * We need to discriminate safari for better performance */ var naughtySafari = typeof document.all === 'function' && typeof document.all === 'undefined'; var callableFnCacheMap = new WeakMap(); export var isCallable = function isCallable(fn) { if (callableFnCacheMap.has(fn)) { return true; } var callable = naughtySafari ? typeof fn === 'function' && typeof fn !== 'undefined' : typeof fn === 'function'; if (callable) { callableFnCacheMap.set(fn, callable); } return callable; }; var boundedMap = new WeakMap(); export function isBoundedFunction(fn) { if (boundedMap.has(fn)) { return boundedMap.get(fn); } /* indexOf is faster than startsWith see https://jsperf.com/string-startswith/72 */ var bounded = fn.name.indexOf('bound ') === 0 && !fn.hasOwnProperty('prototype'); boundedMap.set(fn, bounded); return bounded; } export var qiankunHeadTagName = 'qiankun-head'; export function getDefaultTplWrapper(name) { return function (tpl) { var tplWithSimulatedHead; if (tpl.indexOf('
') !== -1) { // We need to mock a head placeholder as native head element will be erased by browser in micro app tplWithSimulatedHead = tpl.replace('', "<".concat(qiankunHeadTagName, ">")).replace('', "".concat(qiankunHeadTagName, ">")); } else { // Some template might not be a standard html document, thus we need to add a simulated head tag for them tplWithSimulatedHead = "<".concat(qiankunHeadTagName, ">").concat(qiankunHeadTagName, ">").concat(tpl); } return "