1 |
|
2 | var singleSpa = Object.freeze({
|
3 | __proto__: null,
|
4 | get start () { return start; },
|
5 | get ensureJQuerySupport () { return ensureJQuerySupport; },
|
6 | get setBootstrapMaxTime () { return setBootstrapMaxTime; },
|
7 | get setMountMaxTime () { return setMountMaxTime; },
|
8 | get setUnmountMaxTime () { return setUnmountMaxTime; },
|
9 | get setUnloadMaxTime () { return setUnloadMaxTime; },
|
10 | get registerApplication () { return registerApplication; },
|
11 | get getMountedApps () { return getMountedApps; },
|
12 | get getAppStatus () { return getAppStatus; },
|
13 | get unloadApplication () { return unloadApplication; },
|
14 | get checkActivityFunctions () { return checkActivityFunctions; },
|
15 | get getAppNames () { return getAppNames; },
|
16 | get navigateToUrl () { return navigateToUrl; },
|
17 | get triggerAppChange () { return triggerAppChange; },
|
18 | get addErrorHandler () { return addErrorHandler; },
|
19 | get removeErrorHandler () { return removeErrorHandler; },
|
20 | get mountRootParcel () { return mountRootParcel; },
|
21 | get NOT_LOADED () { return NOT_LOADED; },
|
22 | get LOADING_SOURCE_CODE () { return LOADING_SOURCE_CODE; },
|
23 | get NOT_BOOTSTRAPPED () { return NOT_BOOTSTRAPPED; },
|
24 | get BOOTSTRAPPING () { return BOOTSTRAPPING; },
|
25 | get NOT_MOUNTED () { return NOT_MOUNTED; },
|
26 | get MOUNTING () { return MOUNTING; },
|
27 | get UPDATING () { return UPDATING; },
|
28 | get LOAD_ERROR () { return LOAD_ERROR; },
|
29 | get MOUNTED () { return MOUNTED; },
|
30 | get UNMOUNTING () { return UNMOUNTING; },
|
31 | get SKIP_BECAUSE_BROKEN () { return SKIP_BECAUSE_BROKEN; }
|
32 | });
|
33 |
|
34 | var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
35 |
|
36 | var NativeCustomEvent = commonjsGlobal.CustomEvent;
|
37 |
|
38 | function useNative () {
|
39 | try {
|
40 | var p = new NativeCustomEvent('cat', { detail: { foo: 'bar' } });
|
41 | return 'cat' === p.type && 'bar' === p.detail.foo;
|
42 | } catch (e) {
|
43 | }
|
44 | return false;
|
45 | }
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | var customEvent = useNative() ? NativeCustomEvent :
|
56 |
|
57 |
|
58 | 'undefined' !== typeof document && 'function' === typeof document.createEvent ? function CustomEvent (type, params) {
|
59 | var e = document.createEvent('CustomEvent');
|
60 | if (params) {
|
61 | e.initCustomEvent(type, params.bubbles, params.cancelable, params.detail);
|
62 | } else {
|
63 | e.initCustomEvent(type, false, false, void 0);
|
64 | }
|
65 | return e;
|
66 | } :
|
67 |
|
68 |
|
69 | function CustomEvent (type, params) {
|
70 | var e = document.createEventObject();
|
71 | e.type = type;
|
72 | if (params) {
|
73 | e.bubbles = Boolean(params.bubbles);
|
74 | e.cancelable = Boolean(params.cancelable);
|
75 | e.detail = params.detail;
|
76 | } else {
|
77 | e.bubbles = false;
|
78 | e.cancelable = false;
|
79 | e.detail = void 0;
|
80 | }
|
81 | return e;
|
82 | };
|
83 |
|
84 | var errorHandlers = [];
|
85 | function handleAppError(err, app) {
|
86 | var transformedErr = transformErr(err, app);
|
87 |
|
88 | if (errorHandlers.length) {
|
89 | errorHandlers.forEach(handler => handler(transformedErr));
|
90 | } else {
|
91 | setTimeout(() => {
|
92 | throw transformedErr;
|
93 | });
|
94 | }
|
95 | }
|
96 | function addErrorHandler(handler) {
|
97 | if (typeof handler !== "function") {
|
98 | throw Error(formatErrorMessage(28, "a single-spa error handler must be a function"));
|
99 | }
|
100 |
|
101 | errorHandlers.push(handler);
|
102 | }
|
103 | function removeErrorHandler(handler) {
|
104 | if (typeof handler !== "function") {
|
105 | throw Error(formatErrorMessage(29, "a single-spa error handler must be a function"));
|
106 | }
|
107 |
|
108 | var removedSomething = false;
|
109 | errorHandlers = errorHandlers.filter(h => {
|
110 | var isHandler = h === handler;
|
111 | removedSomething = removedSomething || isHandler;
|
112 | return !isHandler;
|
113 | });
|
114 | return removedSomething;
|
115 | }
|
116 | function formatErrorMessage(code, msg) {
|
117 | for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
|
118 | args[_key - 2] = arguments[_key];
|
119 | }
|
120 |
|
121 | return "single-spa minified message #".concat(code, ": ").concat(msg ? msg + " " : "", "See https://single-spa.js.org/error/?code=").concat(code).concat(args.length ? "&arg=".concat(args.join("&arg=")) : "");
|
122 | }
|
123 | function transformErr(ogErr, appOrParcel) {
|
124 | var errPrefix = "".concat(objectType(appOrParcel), " '").concat(toName(appOrParcel), "' died in status ").concat(appOrParcel.status, ": ");
|
125 | var result;
|
126 |
|
127 | if (ogErr instanceof Error) {
|
128 | try {
|
129 | ogErr.message = errPrefix + ogErr.message;
|
130 | } catch (err) {
|
131 | |
132 |
|
133 |
|
134 | }
|
135 |
|
136 | result = ogErr;
|
137 | } else {
|
138 | console.warn(formatErrorMessage(30, "While ".concat(appOrParcel.status, ", '").concat(toName(appOrParcel), "' rejected its lifecycle function promise with a non-Error. This will cause stack traces to not be accurate."), appOrParcel.status, toName(appOrParcel)));
|
139 |
|
140 | try {
|
141 | result = Error(errPrefix + JSON.stringify(ogErr));
|
142 | } catch (err) {
|
143 |
|
144 | result = ogErr;
|
145 | }
|
146 | }
|
147 |
|
148 | result.appOrParcelName = toName(appOrParcel);
|
149 | return result;
|
150 | }
|
151 |
|
152 | var NOT_LOADED = "NOT_LOADED";
|
153 | var LOADING_SOURCE_CODE = "LOADING_SOURCE_CODE";
|
154 | var NOT_BOOTSTRAPPED = "NOT_BOOTSTRAPPED";
|
155 | var BOOTSTRAPPING = "BOOTSTRAPPING";
|
156 | var NOT_MOUNTED = "NOT_MOUNTED";
|
157 | var MOUNTING = "MOUNTING";
|
158 | var MOUNTED = "MOUNTED";
|
159 | var UPDATING = "UPDATING";
|
160 | var UNMOUNTING = "UNMOUNTING";
|
161 | var UNLOADING = "UNLOADING";
|
162 | var LOAD_ERROR = "LOAD_ERROR";
|
163 | var SKIP_BECAUSE_BROKEN = "SKIP_BECAUSE_BROKEN";
|
164 | function isActive(app) {
|
165 | return app.status === MOUNTED;
|
166 | }
|
167 | function isntActive(app) {
|
168 | return !isActive(app);
|
169 | }
|
170 | function isLoaded(app) {
|
171 | return app.status !== NOT_LOADED && app.status !== LOADING_SOURCE_CODE && app.status !== LOAD_ERROR;
|
172 | }
|
173 | function isntLoaded(app) {
|
174 | return !isLoaded(app);
|
175 | }
|
176 | function shouldBeActive(app) {
|
177 | try {
|
178 | return app.activeWhen(window.location);
|
179 | } catch (err) {
|
180 | handleAppError(err, app);
|
181 | app.status = SKIP_BECAUSE_BROKEN;
|
182 | }
|
183 | }
|
184 | function shouldntBeActive(app) {
|
185 | try {
|
186 | return !app.activeWhen(window.location);
|
187 | } catch (err) {
|
188 | handleAppError(err, app);
|
189 | app.status = SKIP_BECAUSE_BROKEN;
|
190 | }
|
191 | }
|
192 | function notSkipped(item) {
|
193 | return item !== SKIP_BECAUSE_BROKEN && (!item || item.status !== SKIP_BECAUSE_BROKEN);
|
194 | }
|
195 | function withoutLoadErrors(app) {
|
196 | return app.status === LOAD_ERROR ? new Date().getTime() - app.loadErrorTime >= 200 : true;
|
197 | }
|
198 | function toName(app) {
|
199 | return app.name;
|
200 | }
|
201 | function isParcel(appOrParcel) {
|
202 | return Boolean(appOrParcel.unmountThisParcel);
|
203 | }
|
204 | function objectType(appOrParcel) {
|
205 | return isParcel(appOrParcel) ? "parcel" : "application";
|
206 | }
|
207 |
|
208 |
|
209 |
|
210 | function assign() {
|
211 | for (var i = arguments.length - 1; i > 0; i--) {
|
212 | for (var key in arguments[i]) {
|
213 | if (key === "__proto__") {
|
214 | continue;
|
215 | }
|
216 |
|
217 | arguments[i - 1][key] = arguments[i][key];
|
218 | }
|
219 | }
|
220 |
|
221 | return arguments[0];
|
222 | }
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | function find(arr, func) {
|
228 | for (var i = 0; i < arr.length; i++) {
|
229 | if (func(arr[i])) {
|
230 | return arr[i];
|
231 | }
|
232 | }
|
233 |
|
234 | return null;
|
235 | }
|
236 |
|
237 | function validLifecycleFn(fn) {
|
238 | return fn && (typeof fn === "function" || isArrayOfFns(fn));
|
239 |
|
240 | function isArrayOfFns(arr) {
|
241 | return Array.isArray(arr) && !find(arr, item => typeof item !== "function");
|
242 | }
|
243 | }
|
244 | function flattenFnArray(appOrParcel, lifecycle) {
|
245 | var fns = appOrParcel[lifecycle] || [];
|
246 | fns = Array.isArray(fns) ? fns : [fns];
|
247 |
|
248 | if (fns.length === 0) {
|
249 | fns = [() => Promise.resolve()];
|
250 | }
|
251 |
|
252 | var type = objectType(appOrParcel);
|
253 | var name = toName(appOrParcel);
|
254 | return function (props) {
|
255 | return fns.reduce((resultPromise, fn, index) => {
|
256 | return resultPromise.then(() => {
|
257 | var thisPromise = fn(props);
|
258 | return smellsLikeAPromise(thisPromise) ? thisPromise : Promise.reject(formatErrorMessage(15, "Within ".concat(type, " ").concat(name, ", the lifecycle function ").concat(lifecycle, " at array index ").concat(index, " did not return a promise"), type, name, lifecycle, index));
|
259 | });
|
260 | }, Promise.resolve());
|
261 | };
|
262 | }
|
263 | function smellsLikeAPromise(promise) {
|
264 | return promise && typeof promise.then === "function" && typeof promise.catch === "function";
|
265 | }
|
266 |
|
267 | function toBootstrapPromise(appOrParcel, hardFail) {
|
268 | return Promise.resolve().then(() => {
|
269 | if (appOrParcel.status !== NOT_BOOTSTRAPPED) {
|
270 | return appOrParcel;
|
271 | }
|
272 |
|
273 | appOrParcel.status = BOOTSTRAPPING;
|
274 | return reasonableTime(appOrParcel, "bootstrap").then(() => {
|
275 | appOrParcel.status = NOT_MOUNTED;
|
276 | return appOrParcel;
|
277 | }).catch(err => {
|
278 | appOrParcel.status = SKIP_BECAUSE_BROKEN;
|
279 |
|
280 | if (hardFail) {
|
281 | throw transformErr(err, appOrParcel);
|
282 | } else {
|
283 | handleAppError(err, appOrParcel);
|
284 | return appOrParcel;
|
285 | }
|
286 | });
|
287 | });
|
288 | }
|
289 |
|
290 | function toUnmountPromise(appOrParcel, hardFail) {
|
291 | return Promise.resolve().then(() => {
|
292 | if (appOrParcel.status !== MOUNTED) {
|
293 | return appOrParcel;
|
294 | }
|
295 |
|
296 | appOrParcel.status = UNMOUNTING;
|
297 | var unmountChildrenParcels = Object.keys(appOrParcel.parcels).map(parcelId => appOrParcel.parcels[parcelId].unmountThisParcel());
|
298 | return Promise.all(unmountChildrenParcels).then(unmountAppOrParcel, parcelError => {
|
299 |
|
300 | return unmountAppOrParcel().then(() => {
|
301 |
|
302 | var parentError = Error(parcelError.message);
|
303 |
|
304 | if (hardFail) {
|
305 | var transformedErr = transformErr(parentError, appOrParcel);
|
306 | appOrParcel.status = SKIP_BECAUSE_BROKEN;
|
307 | throw transformedErr;
|
308 | } else {
|
309 | handleAppError(parentError, appOrParcel);
|
310 | appOrParcel.status = SKIP_BECAUSE_BROKEN;
|
311 | }
|
312 | });
|
313 | }).then(() => appOrParcel);
|
314 |
|
315 | function unmountAppOrParcel() {
|
316 |
|
317 | return reasonableTime(appOrParcel, "unmount").then(() => {
|
318 |
|
319 | {
|
320 | appOrParcel.status = NOT_MOUNTED;
|
321 | }
|
322 | }).catch(err => {
|
323 | if (hardFail) {
|
324 | var transformedErr = transformErr(err, appOrParcel);
|
325 | appOrParcel.status = SKIP_BECAUSE_BROKEN;
|
326 | throw transformedErr;
|
327 | } else {
|
328 | handleAppError(err, appOrParcel);
|
329 | appOrParcel.status = SKIP_BECAUSE_BROKEN;
|
330 | }
|
331 | });
|
332 | }
|
333 | });
|
334 | }
|
335 |
|
336 | var beforeFirstMountFired = false;
|
337 | var firstMountFired = false;
|
338 | function toMountPromise(appOrParcel, hardFail) {
|
339 | return Promise.resolve().then(() => {
|
340 | if (appOrParcel.status !== NOT_MOUNTED) {
|
341 | return appOrParcel;
|
342 | }
|
343 |
|
344 | if (!beforeFirstMountFired) {
|
345 | window.dispatchEvent(new customEvent("single-spa:before-first-mount"));
|
346 | beforeFirstMountFired = true;
|
347 | }
|
348 |
|
349 | return reasonableTime(appOrParcel, "mount").then(() => {
|
350 | appOrParcel.status = MOUNTED;
|
351 |
|
352 | if (!firstMountFired) {
|
353 | window.dispatchEvent(new customEvent("single-spa:first-mount"));
|
354 | firstMountFired = true;
|
355 | }
|
356 |
|
357 | return appOrParcel;
|
358 | }).catch(err => {
|
359 |
|
360 |
|
361 |
|
362 | appOrParcel.status = MOUNTED;
|
363 | return toUnmountPromise(appOrParcel).then(setSkipBecauseBroken, setSkipBecauseBroken);
|
364 |
|
365 | function setSkipBecauseBroken() {
|
366 | if (!hardFail) {
|
367 | handleAppError(err, appOrParcel);
|
368 | appOrParcel.status = SKIP_BECAUSE_BROKEN;
|
369 | return appOrParcel;
|
370 | } else {
|
371 | var transformedErr = transformErr(err, appOrParcel);
|
372 | appOrParcel.status = SKIP_BECAUSE_BROKEN;
|
373 | throw transformedErr;
|
374 | }
|
375 | }
|
376 | });
|
377 | });
|
378 | }
|
379 |
|
380 | function toUpdatePromise(parcel) {
|
381 | return Promise.resolve().then(() => {
|
382 | if (parcel.status !== MOUNTED) {
|
383 | throw Error(formatErrorMessage(32, "Cannot update parcel '".concat(toName(parcel), "' because it is not mounted"), toName(parcel)));
|
384 | }
|
385 |
|
386 | parcel.status = UPDATING;
|
387 | return reasonableTime(parcel, "update").then(() => {
|
388 | parcel.status = MOUNTED;
|
389 | return parcel;
|
390 | }).catch(err => {
|
391 | var transformedErr = transformErr(err, parcel);
|
392 | parcel.status = SKIP_BECAUSE_BROKEN;
|
393 | throw transformedErr;
|
394 | });
|
395 | });
|
396 | }
|
397 |
|
398 | var parcelCount = 0;
|
399 | var rootParcels = {
|
400 | parcels: {}
|
401 | };
|
402 |
|
403 | function mountRootParcel() {
|
404 | return mountParcel.apply(rootParcels, arguments);
|
405 | }
|
406 | function mountParcel(config, customProps) {
|
407 | var owningAppOrParcel = this;
|
408 |
|
409 | if (!config || typeof config !== "object" && typeof config !== "function") {
|
410 | throw Error(formatErrorMessage(2, "Cannot mount parcel without a config object or config loading function"));
|
411 | }
|
412 |
|
413 | if (config.name && typeof config.name !== "string") {
|
414 | throw Error(formatErrorMessage(3, "Parcel name must be a string, if provided. Was given ".concat(typeof config.name), typeof config.name));
|
415 | }
|
416 |
|
417 | if (typeof customProps !== "object") {
|
418 | throw Error(formatErrorMessage(4, "Parcel ".concat(name, " has invalid customProps -- must be an object but was given ").concat(typeof customProps), name, typeof customProps));
|
419 | }
|
420 |
|
421 | if (!customProps.domElement) {
|
422 | throw Error(formatErrorMessage(5, "Parcel ".concat(name, " cannot be mounted without a domElement provided as a prop"), name));
|
423 | }
|
424 |
|
425 | var id = parcelCount++;
|
426 | var passedConfigLoadingFunction = typeof config === "function";
|
427 | var configLoadingFunction = passedConfigLoadingFunction ? config : () => Promise.resolve(config);
|
428 |
|
429 | var parcel = {
|
430 | id,
|
431 | parcels: {},
|
432 | status: passedConfigLoadingFunction ? LOADING_SOURCE_CODE : NOT_BOOTSTRAPPED,
|
433 | customProps,
|
434 | parentName: toName(owningAppOrParcel),
|
435 |
|
436 | unmountThisParcel() {
|
437 | if (parcel.status !== MOUNTED) {
|
438 | throw Error(formatErrorMessage(6, "Cannot unmount parcel '".concat(name, "' -- it is in a ").concat(parcel.status, " status"), name, parcel.status));
|
439 | }
|
440 |
|
441 | return toUnmountPromise(parcel, true).then(value => {
|
442 | if (parcel.parentName) {
|
443 | delete owningAppOrParcel.parcels[parcel.id];
|
444 | }
|
445 |
|
446 | return value;
|
447 | }).then(value => {
|
448 | resolveUnmount(value);
|
449 | return value;
|
450 | }).catch(err => {
|
451 | parcel.status = SKIP_BECAUSE_BROKEN;
|
452 | rejectUnmount(err);
|
453 | throw err;
|
454 | });
|
455 | }
|
456 |
|
457 | };
|
458 |
|
459 | var externalRepresentation;
|
460 |
|
461 | owningAppOrParcel.parcels[id] = parcel;
|
462 | var loadPromise = configLoadingFunction();
|
463 |
|
464 | if (!loadPromise || typeof loadPromise.then !== "function") {
|
465 | throw Error(formatErrorMessage(7, "When mounting a parcel, the config loading function must return a promise that resolves with the parcel config"));
|
466 | }
|
467 |
|
468 | loadPromise = loadPromise.then(config => {
|
469 | if (!config) {
|
470 | throw Error(formatErrorMessage(8, "When mounting a parcel, the config loading function returned a promise that did not resolve with a parcel config"));
|
471 | }
|
472 |
|
473 | var name = config.name || "parcel-".concat(id);
|
474 |
|
475 | if (!validLifecycleFn(config.bootstrap)) {
|
476 | throw Error(formatErrorMessage(9, "Parcel ".concat(name, " must have a valid bootstrap function"), name));
|
477 | }
|
478 |
|
479 | if (!validLifecycleFn(config.mount)) {
|
480 | throw Error(formatErrorMessage(10, "Parcel ".concat(name, " must have a valid mount function"), name));
|
481 | }
|
482 |
|
483 | if (!validLifecycleFn(config.unmount)) {
|
484 | throw Error(formatErrorMessage(11, "Parcel ".concat(name, " must have a valid unmount function"), name));
|
485 | }
|
486 |
|
487 | if (config.update && !validLifecycleFn(config.update)) {
|
488 | throw Error(formatErrorMessage(12, "Parcel ".concat(name, " provided an invalid update function"), name));
|
489 | }
|
490 |
|
491 | var bootstrap = flattenFnArray(config, "bootstrap");
|
492 | var mount = flattenFnArray(config, "mount");
|
493 | var unmount = flattenFnArray(config, "unmount");
|
494 | parcel.status = NOT_BOOTSTRAPPED;
|
495 | parcel.name = name;
|
496 | parcel.bootstrap = bootstrap;
|
497 | parcel.mount = mount;
|
498 | parcel.unmount = unmount;
|
499 | parcel.timeouts = ensureValidAppTimeouts(config.timeouts);
|
500 |
|
501 | if (config.update) {
|
502 | parcel.update = flattenFnArray(config, "update");
|
503 |
|
504 | externalRepresentation.update = function (customProps) {
|
505 | parcel.customProps = customProps;
|
506 | return promiseWithoutReturnValue(toUpdatePromise(parcel));
|
507 | };
|
508 | }
|
509 | });
|
510 |
|
511 |
|
512 | var bootstrapPromise = loadPromise.then(() => toBootstrapPromise(parcel, true));
|
513 | var mountPromise = bootstrapPromise.then(() => toMountPromise(parcel, true));
|
514 | var resolveUnmount, rejectUnmount;
|
515 | var unmountPromise = new Promise((resolve, reject) => {
|
516 | resolveUnmount = resolve;
|
517 | rejectUnmount = reject;
|
518 | });
|
519 | externalRepresentation = {
|
520 | mount() {
|
521 | return promiseWithoutReturnValue(Promise.resolve().then(() => {
|
522 | if (parcel.status !== NOT_MOUNTED) {
|
523 | throw Error(formatErrorMessage(13, "Cannot mount parcel '".concat(name, "' -- it is in a ").concat(parcel.status, " status"), name, parcel.status));
|
524 | }
|
525 |
|
526 |
|
527 | owningAppOrParcel.parcels[id] = parcel;
|
528 | return toMountPromise(parcel);
|
529 | }));
|
530 | },
|
531 |
|
532 | unmount() {
|
533 | return promiseWithoutReturnValue(parcel.unmountThisParcel());
|
534 | },
|
535 |
|
536 | getStatus() {
|
537 | return parcel.status;
|
538 | },
|
539 |
|
540 | loadPromise: promiseWithoutReturnValue(loadPromise),
|
541 | bootstrapPromise: promiseWithoutReturnValue(bootstrapPromise),
|
542 | mountPromise: promiseWithoutReturnValue(mountPromise),
|
543 | unmountPromise: promiseWithoutReturnValue(unmountPromise)
|
544 | };
|
545 | return externalRepresentation;
|
546 | }
|
547 |
|
548 | function promiseWithoutReturnValue(promise) {
|
549 | return promise.then(() => null);
|
550 | }
|
551 |
|
552 | function getProps(appOrParcel) {
|
553 | var result = assign({}, appOrParcel.customProps, {
|
554 | name: toName(appOrParcel),
|
555 | mountParcel: mountParcel.bind(appOrParcel),
|
556 | singleSpa
|
557 | });
|
558 |
|
559 | if (isParcel(appOrParcel)) {
|
560 | result.unmountSelf = appOrParcel.unmountThisParcel;
|
561 | }
|
562 |
|
563 | return result;
|
564 | }
|
565 |
|
566 | var defaultWarningMillis = 1000;
|
567 | var globalTimeoutConfig = {
|
568 | bootstrap: {
|
569 | millis: 4000,
|
570 | dieOnTimeout: false,
|
571 | warningMillis: defaultWarningMillis
|
572 | },
|
573 | mount: {
|
574 | millis: 3000,
|
575 | dieOnTimeout: false,
|
576 | warningMillis: defaultWarningMillis
|
577 | },
|
578 | unmount: {
|
579 | millis: 3000,
|
580 | dieOnTimeout: false,
|
581 | warningMillis: defaultWarningMillis
|
582 | },
|
583 | unload: {
|
584 | millis: 3000,
|
585 | dieOnTimeout: false,
|
586 | warningMillis: defaultWarningMillis
|
587 | },
|
588 | update: {
|
589 | millis: 3000,
|
590 | dieOnTimeout: false,
|
591 | warningMillis: defaultWarningMillis
|
592 | }
|
593 | };
|
594 | function setBootstrapMaxTime(time, dieOnTimeout, warningMillis) {
|
595 | if (typeof time !== "number" || time <= 0) {
|
596 | throw Error(formatErrorMessage(16, "bootstrap max time must be a positive integer number of milliseconds"));
|
597 | }
|
598 |
|
599 | globalTimeoutConfig.bootstrap = {
|
600 | millis: time,
|
601 | dieOnTimeout,
|
602 | warningMillis: warningMillis || defaultWarningMillis
|
603 | };
|
604 | }
|
605 | function setMountMaxTime(time, dieOnTimeout, warningMillis) {
|
606 | if (typeof time !== "number" || time <= 0) {
|
607 | throw Error(formatErrorMessage(17, "mount max time must be a positive integer number of milliseconds"));
|
608 | }
|
609 |
|
610 | globalTimeoutConfig.mount = {
|
611 | millis: time,
|
612 | dieOnTimeout,
|
613 | warningMillis: warningMillis || defaultWarningMillis
|
614 | };
|
615 | }
|
616 | function setUnmountMaxTime(time, dieOnTimeout, warningMillis) {
|
617 | if (typeof time !== "number" || time <= 0) {
|
618 | throw Error(formatErrorMessage(18, "unmount max time must be a positive integer number of milliseconds"));
|
619 | }
|
620 |
|
621 | globalTimeoutConfig.unmount = {
|
622 | millis: time,
|
623 | dieOnTimeout,
|
624 | warningMillis: warningMillis || defaultWarningMillis
|
625 | };
|
626 | }
|
627 | function setUnloadMaxTime(time, dieOnTimeout, warningMillis) {
|
628 | if (typeof time !== "number" || time <= 0) {
|
629 | throw Error(formatErrorMessage(19, "unload max time must be a positive integer number of milliseconds"));
|
630 | }
|
631 |
|
632 | globalTimeoutConfig.unload = {
|
633 | millis: time,
|
634 | dieOnTimeout,
|
635 | warningMillis: warningMillis || defaultWarningMillis
|
636 | };
|
637 | }
|
638 | function reasonableTime(appOrParcel, lifecycle) {
|
639 | var timeoutConfig = appOrParcel.timeouts[lifecycle];
|
640 | var warningPeriod = timeoutConfig.warningMillis;
|
641 | var type = objectType(appOrParcel);
|
642 | return new Promise((resolve, reject) => {
|
643 | var finished = false;
|
644 | var errored = false;
|
645 | appOrParcel[lifecycle](getProps(appOrParcel)).then(val => {
|
646 | finished = true;
|
647 | resolve(val);
|
648 | }).catch(val => {
|
649 | finished = true;
|
650 | reject(val);
|
651 | });
|
652 | setTimeout(() => maybeTimingOut(1), warningPeriod);
|
653 | setTimeout(() => maybeTimingOut(true), timeoutConfig.millis);
|
654 | var errMsg = formatErrorMessage(31, "Lifecycle function ".concat(lifecycle, " for ").concat(type, " ").concat(toName(appOrParcel), " lifecycle did not resolve or reject for ").concat(timeoutConfig.millis, " ms."), lifecycle, type, toName(appOrParcel), timeoutConfig.millis);
|
655 |
|
656 | function maybeTimingOut(shouldError) {
|
657 | if (!finished) {
|
658 | if (shouldError === true) {
|
659 | errored = true;
|
660 |
|
661 | if (timeoutConfig.dieOnTimeout) {
|
662 | reject(Error(errMsg));
|
663 | } else {
|
664 | console.error(errMsg);
|
665 | }
|
666 | } else if (!errored) {
|
667 | var numWarnings = shouldError;
|
668 | var numMillis = numWarnings * warningPeriod;
|
669 | console.warn(errMsg);
|
670 |
|
671 | if (numMillis + warningPeriod < timeoutConfig.millis) {
|
672 | setTimeout(() => maybeTimingOut(numWarnings + 1), warningPeriod);
|
673 | }
|
674 | }
|
675 | }
|
676 | }
|
677 | });
|
678 | }
|
679 | function ensureValidAppTimeouts(timeouts) {
|
680 | var result = {};
|
681 |
|
682 | for (var key in globalTimeoutConfig) {
|
683 | result[key] = assign({}, globalTimeoutConfig[key], timeouts && timeouts[key] || {});
|
684 | }
|
685 |
|
686 | return result;
|
687 | }
|
688 |
|
689 | function toLoadPromise(app) {
|
690 | return Promise.resolve().then(() => {
|
691 | if (app.status !== NOT_LOADED && app.status !== LOAD_ERROR) {
|
692 | return app;
|
693 | }
|
694 |
|
695 | app.status = LOADING_SOURCE_CODE;
|
696 | var appOpts, isUserErr;
|
697 | return Promise.resolve().then(() => {
|
698 | var loadPromise = app.loadImpl(getProps(app));
|
699 |
|
700 | if (!smellsLikeAPromise(loadPromise)) {
|
701 |
|
702 | isUserErr = true;
|
703 | throw Error(formatErrorMessage(33, "single-spa loading function did not return a promise. Check the second argument to registerApplication('".concat(toName(app), "', loadingFunction, activityFunction)"), toName(app)));
|
704 | }
|
705 |
|
706 | return loadPromise.then(val => {
|
707 | app.loadErrorTime = null;
|
708 | appOpts = val;
|
709 | var validationErrMessage, validationErrCode;
|
710 |
|
711 | if (typeof appOpts !== "object") {
|
712 | validationErrCode = 34;
|
713 |
|
714 | {
|
715 | validationErrMessage = "does not export anything";
|
716 | }
|
717 | }
|
718 |
|
719 | if (!validLifecycleFn(appOpts.bootstrap)) {
|
720 | validationErrCode = 35;
|
721 |
|
722 | {
|
723 | validationErrMessage = "does not export a bootstrap function or array of functions";
|
724 | }
|
725 | }
|
726 |
|
727 | if (!validLifecycleFn(appOpts.mount)) {
|
728 | validationErrCode = 36;
|
729 |
|
730 | {
|
731 | validationErrMessage = "does not export a bootstrap function or array of functions";
|
732 | }
|
733 | }
|
734 |
|
735 | if (!validLifecycleFn(appOpts.unmount)) {
|
736 | validationErrCode = 37;
|
737 |
|
738 | {
|
739 | validationErrMessage = "does not export a bootstrap function or array of functions";
|
740 | }
|
741 | }
|
742 |
|
743 | var type = objectType(appOpts);
|
744 |
|
745 | if (validationErrCode) {
|
746 | var appOptsStr;
|
747 |
|
748 | try {
|
749 | appOptsStr = JSON.stringify(appOpts);
|
750 | } catch (_unused) {}
|
751 |
|
752 | console.error(formatErrorMessage(validationErrCode, "The loading function for single-spa ".concat(type, " '").concat(toName(app), "' resolved with the following, which does not have bootstrap, mount, and unmount functions"), type, toName(app), appOptsStr), appOpts);
|
753 | handleAppError(validationErrMessage, app);
|
754 | app.status = SKIP_BECAUSE_BROKEN;
|
755 | return app;
|
756 | }
|
757 |
|
758 | if (appOpts.devtools && appOpts.devtools.overlays) {
|
759 | app.devtools.overlays = assign({}, app.devtools.overlays, appOpts.devtools.overlays);
|
760 | }
|
761 |
|
762 | app.status = NOT_BOOTSTRAPPED;
|
763 | app.bootstrap = flattenFnArray(appOpts, "bootstrap");
|
764 | app.mount = flattenFnArray(appOpts, "mount");
|
765 | app.unmount = flattenFnArray(appOpts, "unmount");
|
766 | app.unload = flattenFnArray(appOpts, "unload");
|
767 | app.timeouts = ensureValidAppTimeouts(appOpts.timeouts);
|
768 | return app;
|
769 | });
|
770 | }).catch(err => {
|
771 | handleAppError(err, app);
|
772 |
|
773 | if (isUserErr) {
|
774 | app.status = SKIP_BECAUSE_BROKEN;
|
775 | } else {
|
776 | app.status = LOAD_ERROR;
|
777 | app.loadErrorTime = new Date().getTime();
|
778 | }
|
779 |
|
780 | return app;
|
781 | });
|
782 | });
|
783 | }
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 |
|
790 |
|
791 | var capturedEventListeners = {
|
792 | hashchange: [],
|
793 | popstate: []
|
794 | };
|
795 | var routingEventsListeningTo = ["hashchange", "popstate"];
|
796 | function navigateToUrl(obj) {
|
797 | var url;
|
798 |
|
799 | if (typeof obj === "string") {
|
800 | url = obj;
|
801 | } else if (this && this.href) {
|
802 | url = this.href;
|
803 | } else if (obj && obj.currentTarget && obj.currentTarget.href && obj.preventDefault) {
|
804 | url = obj.currentTarget.href;
|
805 | obj.preventDefault();
|
806 | } else {
|
807 | throw Error(formatErrorMessage(14, "singleSpaNavigate/navigateToUrl must be either called with a string url, with an <a> tag as its context, or with an event whose currentTarget is an <a> tag"));
|
808 | }
|
809 |
|
810 | var current = parseUri(window.location.href);
|
811 | var destination = parseUri(url);
|
812 |
|
813 | if (url.indexOf("#") === 0) {
|
814 | window.location.hash = destination.hash;
|
815 | } else if (current.host !== destination.host && destination.host) {
|
816 | {
|
817 | window.location.href = url;
|
818 | }
|
819 | } else if (destination.pathname === current.pathname && destination.search === current.pathname) {
|
820 | window.location.hash = destination.hash;
|
821 | } else {
|
822 |
|
823 | window.history.pushState(null, null, url);
|
824 | }
|
825 | }
|
826 | function callCapturedEventListeners(eventArguments) {
|
827 | if (eventArguments) {
|
828 | var eventType = eventArguments[0].type;
|
829 |
|
830 | if (routingEventsListeningTo.indexOf(eventType) >= 0) {
|
831 | capturedEventListeners[eventType].forEach(listener => {
|
832 | try {
|
833 |
|
834 |
|
835 | listener.apply(this, eventArguments);
|
836 | } catch (e) {
|
837 | setTimeout(() => {
|
838 | throw e;
|
839 | });
|
840 | }
|
841 | });
|
842 | }
|
843 | }
|
844 | }
|
845 |
|
846 | function urlReroute() {
|
847 | reroute([], arguments);
|
848 | }
|
849 |
|
850 |
|
851 | window.addEventListener("hashchange", urlReroute);
|
852 | window.addEventListener("popstate", urlReroute);
|
853 |
|
854 | var originalAddEventListener = window.addEventListener;
|
855 | var originalRemoveEventListener = window.removeEventListener;
|
856 |
|
857 | window.addEventListener = function (eventName, fn) {
|
858 | if (typeof fn === "function") {
|
859 | if (routingEventsListeningTo.indexOf(eventName) >= 0 && !find(capturedEventListeners[eventName], listener => listener === fn)) {
|
860 | capturedEventListeners[eventName].push(fn);
|
861 | return;
|
862 | }
|
863 | }
|
864 |
|
865 | return originalAddEventListener.apply(this, arguments);
|
866 | };
|
867 |
|
868 | window.removeEventListener = function (eventName, listenerFn) {
|
869 | if (typeof listenerFn === "function") {
|
870 | if (routingEventsListeningTo.indexOf(eventName) >= 0) {
|
871 | capturedEventListeners[eventName] = capturedEventListeners[eventName].filter(fn => fn !== listenerFn);
|
872 | return;
|
873 | }
|
874 | }
|
875 |
|
876 | return originalRemoveEventListener.apply(this, arguments);
|
877 | };
|
878 |
|
879 | var originalPushState = window.history.pushState;
|
880 |
|
881 | window.history.pushState = function (state) {
|
882 | var result = originalPushState.apply(this, arguments);
|
883 | urlReroute(createPopStateEvent(state));
|
884 | return result;
|
885 | };
|
886 |
|
887 | var originalReplaceState = window.history.replaceState;
|
888 |
|
889 | window.history.replaceState = function (state) {
|
890 | var result = originalReplaceState.apply(this, arguments);
|
891 | urlReroute(createPopStateEvent(state));
|
892 | return result;
|
893 | };
|
894 |
|
895 | function createPopStateEvent(state) {
|
896 |
|
897 |
|
898 |
|
899 | try {
|
900 | return new PopStateEvent("popstate", {
|
901 | state
|
902 | });
|
903 | } catch (err) {
|
904 |
|
905 |
|
906 | var evt = document.createEvent("PopStateEvent");
|
907 | evt.initPopStateEvent("popstate", false, false, state);
|
908 | return evt;
|
909 | }
|
910 | }
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 | window.singleSpaNavigate = navigateToUrl;
|
917 |
|
918 | function parseUri(str) {
|
919 | var anchor = document.createElement("a");
|
920 | anchor.href = str;
|
921 | return anchor;
|
922 | }
|
923 |
|
924 | var hasInitialized = false;
|
925 | function ensureJQuerySupport() {
|
926 | var jQuery = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : window.jQuery;
|
927 |
|
928 | if (!jQuery) {
|
929 | if (window.$ && window.$.fn && window.$.fn.jquery) {
|
930 | jQuery = window.$;
|
931 | }
|
932 | }
|
933 |
|
934 | if (jQuery && !hasInitialized) {
|
935 | var originalJQueryOn = jQuery.fn.on;
|
936 | var originalJQueryOff = jQuery.fn.off;
|
937 |
|
938 | jQuery.fn.on = function (eventString, fn) {
|
939 | return captureRoutingEvents.call(this, originalJQueryOn, window.addEventListener, eventString, fn, arguments);
|
940 | };
|
941 |
|
942 | jQuery.fn.off = function (eventString, fn) {
|
943 | return captureRoutingEvents.call(this, originalJQueryOff, window.removeEventListener, eventString, fn, arguments);
|
944 | };
|
945 |
|
946 | hasInitialized = true;
|
947 | }
|
948 | }
|
949 |
|
950 | function captureRoutingEvents(originalJQueryFunction, nativeFunctionToCall, eventString, fn, originalArgs) {
|
951 | if (typeof eventString !== "string") {
|
952 | return originalJQueryFunction.apply(this, originalArgs);
|
953 | }
|
954 |
|
955 | var eventNames = eventString.split(/\s+/);
|
956 | eventNames.forEach(eventName => {
|
957 | if (routingEventsListeningTo.indexOf(eventName) >= 0) {
|
958 | nativeFunctionToCall(eventName, fn);
|
959 | eventString = eventString.replace(eventName, "");
|
960 | }
|
961 | });
|
962 |
|
963 | if (eventString.trim() === "") {
|
964 | return this;
|
965 | } else {
|
966 | return originalJQueryFunction.apply(this, originalArgs);
|
967 | }
|
968 | }
|
969 |
|
970 | var appsToUnload = {};
|
971 | function toUnloadPromise(app) {
|
972 | return Promise.resolve().then(() => {
|
973 | var unloadInfo = appsToUnload[toName(app)];
|
974 |
|
975 | if (!unloadInfo) {
|
976 | |
977 |
|
978 | return app;
|
979 | }
|
980 |
|
981 | if (app.status === NOT_LOADED) {
|
982 | |
983 |
|
984 |
|
985 | finishUnloadingApp(app, unloadInfo);
|
986 | return app;
|
987 | }
|
988 |
|
989 | if (app.status === UNLOADING) {
|
990 | |
991 |
|
992 |
|
993 | return unloadInfo.promise.then(() => app);
|
994 | }
|
995 |
|
996 | if (app.status !== NOT_MOUNTED) {
|
997 | |
998 |
|
999 | return app;
|
1000 | }
|
1001 |
|
1002 | app.status = UNLOADING;
|
1003 | return reasonableTime(app, "unload").then(() => {
|
1004 | finishUnloadingApp(app, unloadInfo);
|
1005 | return app;
|
1006 | }).catch(err => {
|
1007 | errorUnloadingApp(app, unloadInfo, err);
|
1008 | return app;
|
1009 | });
|
1010 | });
|
1011 | }
|
1012 |
|
1013 | function finishUnloadingApp(app, unloadInfo) {
|
1014 | delete appsToUnload[toName(app)];
|
1015 |
|
1016 | delete app.bootstrap;
|
1017 | delete app.mount;
|
1018 | delete app.unmount;
|
1019 | delete app.unload;
|
1020 | app.status = NOT_LOADED;
|
1021 | |
1022 |
|
1023 |
|
1024 |
|
1025 | unloadInfo.resolve();
|
1026 | }
|
1027 |
|
1028 | function errorUnloadingApp(app, unloadInfo, err) {
|
1029 | delete appsToUnload[toName(app)];
|
1030 |
|
1031 | delete app.bootstrap;
|
1032 | delete app.mount;
|
1033 | delete app.unmount;
|
1034 | delete app.unload;
|
1035 | handleAppError(err, app);
|
1036 | app.status = SKIP_BECAUSE_BROKEN;
|
1037 | unloadInfo.reject(err);
|
1038 | }
|
1039 |
|
1040 | function addAppToUnload(app, promiseGetter, resolve, reject) {
|
1041 | appsToUnload[toName(app)] = {
|
1042 | app,
|
1043 | resolve,
|
1044 | reject
|
1045 | };
|
1046 | Object.defineProperty(appsToUnload[toName(app)], "promise", {
|
1047 | get: promiseGetter
|
1048 | });
|
1049 | }
|
1050 | function getAppUnloadInfo(appName) {
|
1051 | return appsToUnload[appName];
|
1052 | }
|
1053 | function getAppsToUnload() {
|
1054 | return Object.keys(appsToUnload).map(appName => appsToUnload[appName].app).filter(isntActive);
|
1055 | }
|
1056 |
|
1057 | var apps = [];
|
1058 | function getMountedApps() {
|
1059 | return apps.filter(isActive).map(toName);
|
1060 | }
|
1061 | function getAppNames() {
|
1062 | return apps.map(toName);
|
1063 | }
|
1064 |
|
1065 | function getRawAppData() {
|
1066 | return [...apps];
|
1067 | }
|
1068 | function getAppStatus(appName) {
|
1069 | var app = find(apps, app => toName(app) === appName);
|
1070 | return app ? app.status : null;
|
1071 | }
|
1072 | function registerApplication(appName, applicationOrLoadingFn, activityFn) {
|
1073 | var customProps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
1074 | if (typeof appName !== "string" || appName.length === 0) throw Error(formatErrorMessage(20, "The first argument to registerApplication must be a non-empty string 'appName'"));
|
1075 | if (getAppNames().indexOf(appName) !== -1) throw Error(formatErrorMessage(21, "There is already an app declared with name ".concat(appName), appName));
|
1076 | if (typeof customProps !== "object" || Array.isArray(customProps)) throw Error(formatErrorMessage(22, "customProps must be an object"));
|
1077 | if (!applicationOrLoadingFn) throw Error(formatErrorMessage(23, "The application or loading function is required"));
|
1078 | var loadImpl;
|
1079 |
|
1080 | if (typeof applicationOrLoadingFn !== "function") {
|
1081 |
|
1082 | loadImpl = () => Promise.resolve(applicationOrLoadingFn);
|
1083 | } else {
|
1084 |
|
1085 | loadImpl = applicationOrLoadingFn;
|
1086 | }
|
1087 |
|
1088 | if (typeof activityFn !== "function") throw Error(formatErrorMessage(24, "The activityFunction argument must be a function"));
|
1089 | apps.push({
|
1090 | loadErrorTime: null,
|
1091 | name: appName,
|
1092 | loadImpl,
|
1093 | activeWhen: activityFn,
|
1094 | status: NOT_LOADED,
|
1095 | parcels: {},
|
1096 | devtools: {
|
1097 | overlays: {
|
1098 | options: {},
|
1099 | selectors: []
|
1100 | }
|
1101 | },
|
1102 | customProps
|
1103 | });
|
1104 | ensureJQuerySupport();
|
1105 | reroute();
|
1106 | }
|
1107 | function checkActivityFunctions(location) {
|
1108 | return apps.filter(app => app.activeWhen(location)).map(toName);
|
1109 | }
|
1110 | function getAppsToLoad() {
|
1111 | return apps.filter(notSkipped).filter(withoutLoadErrors).filter(isntLoaded).filter(shouldBeActive);
|
1112 | }
|
1113 | function getAppsToUnmount() {
|
1114 | return apps.filter(notSkipped).filter(isActive).filter(shouldntBeActive);
|
1115 | }
|
1116 | function getAppsToMount() {
|
1117 | return apps.filter(notSkipped).filter(isntActive).filter(isLoaded).filter(shouldBeActive);
|
1118 | }
|
1119 | function unregisterApplication(appName) {
|
1120 | if (!apps.find(app => toName(app) === appName)) {
|
1121 | throw Error(formatErrorMessage(25, "Cannot unregister application '".concat(appName, "' because no such application has been registered"), appName));
|
1122 | }
|
1123 |
|
1124 | return unloadApplication(appName).then(() => {
|
1125 | var appIndex = apps.findIndex(app => toName(app) === appName);
|
1126 | apps.splice(appIndex, 1);
|
1127 | });
|
1128 | }
|
1129 | function unloadApplication(appName) {
|
1130 | var opts = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {
|
1131 | waitForUnmount: false
|
1132 | };
|
1133 |
|
1134 | if (typeof appName !== "string") {
|
1135 | throw Error(formatErrorMessage(26, "unloadApplication requires a string 'appName'"));
|
1136 | }
|
1137 |
|
1138 | var app = find(apps, App => toName(App) === appName);
|
1139 |
|
1140 | if (!app) {
|
1141 | throw Error(formatErrorMessage(27, "Could not unload application '".concat(appName, "' because no such application has been registered"), appName));
|
1142 | }
|
1143 |
|
1144 | var appUnloadInfo = getAppUnloadInfo(toName(app));
|
1145 |
|
1146 | if (opts && opts.waitForUnmount) {
|
1147 |
|
1148 | if (appUnloadInfo) {
|
1149 |
|
1150 | return appUnloadInfo.promise;
|
1151 | } else {
|
1152 |
|
1153 | var promise = new Promise((resolve, reject) => {
|
1154 | addAppToUnload(app, () => promise, resolve, reject);
|
1155 | });
|
1156 | return promise;
|
1157 | }
|
1158 | } else {
|
1159 | |
1160 |
|
1161 | var resultPromise;
|
1162 |
|
1163 | if (appUnloadInfo) {
|
1164 |
|
1165 | resultPromise = appUnloadInfo.promise;
|
1166 | immediatelyUnloadApp(app, appUnloadInfo.resolve, appUnloadInfo.reject);
|
1167 | } else {
|
1168 |
|
1169 | resultPromise = new Promise((resolve, reject) => {
|
1170 | addAppToUnload(app, () => resultPromise, resolve, reject);
|
1171 | immediatelyUnloadApp(app, resolve, reject);
|
1172 | });
|
1173 | }
|
1174 |
|
1175 | return resultPromise;
|
1176 | }
|
1177 | }
|
1178 |
|
1179 | function immediatelyUnloadApp(app, resolve, reject) {
|
1180 | toUnmountPromise(app).then(toUnloadPromise).then(() => {
|
1181 | resolve();
|
1182 | setTimeout(() => {
|
1183 |
|
1184 | reroute();
|
1185 | });
|
1186 | }).catch(reject);
|
1187 | }
|
1188 |
|
1189 | var appChangeUnderway = false,
|
1190 | peopleWaitingOnAppChange = [];
|
1191 | function triggerAppChange() {
|
1192 |
|
1193 | return reroute();
|
1194 | }
|
1195 | function reroute() {
|
1196 | var pendingPromises = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : [];
|
1197 | var eventArguments = arguments.length > 1 ? arguments[1] : undefined;
|
1198 |
|
1199 | if (appChangeUnderway) {
|
1200 | return new Promise((resolve, reject) => {
|
1201 | peopleWaitingOnAppChange.push({
|
1202 | resolve,
|
1203 | reject,
|
1204 | eventArguments
|
1205 | });
|
1206 | });
|
1207 | }
|
1208 |
|
1209 | appChangeUnderway = true;
|
1210 | var wasNoOp = true;
|
1211 |
|
1212 | if (isStarted()) {
|
1213 | return performAppChanges();
|
1214 | } else {
|
1215 | return loadApps();
|
1216 | }
|
1217 |
|
1218 | function loadApps() {
|
1219 | return Promise.resolve().then(() => {
|
1220 | var loadPromises = getAppsToLoad().map(toLoadPromise);
|
1221 |
|
1222 | if (loadPromises.length > 0) {
|
1223 | wasNoOp = false;
|
1224 | }
|
1225 |
|
1226 | return Promise.all(loadPromises).then(finishUpAndReturn).catch(err => {
|
1227 | callAllEventListeners();
|
1228 | throw err;
|
1229 | });
|
1230 | });
|
1231 | }
|
1232 |
|
1233 | function performAppChanges() {
|
1234 | return Promise.resolve().then(() => {
|
1235 | window.dispatchEvent(new customEvent("single-spa:before-routing-event", getCustomEventDetail()));
|
1236 | var unloadPromises = getAppsToUnload().map(toUnloadPromise);
|
1237 | var unmountUnloadPromises = getAppsToUnmount().map(toUnmountPromise).map(unmountPromise => unmountPromise.then(toUnloadPromise));
|
1238 | var allUnmountPromises = unmountUnloadPromises.concat(unloadPromises);
|
1239 |
|
1240 | if (allUnmountPromises.length > 0) {
|
1241 | wasNoOp = false;
|
1242 | }
|
1243 |
|
1244 | var unmountAllPromise = Promise.all(allUnmountPromises);
|
1245 | var appsToLoad = getAppsToLoad();
|
1246 | |
1247 |
|
1248 |
|
1249 |
|
1250 | var loadThenMountPromises = appsToLoad.map(app => {
|
1251 | return toLoadPromise(app).then(toBootstrapPromise).then(app => {
|
1252 | return unmountAllPromise.then(() => toMountPromise(app));
|
1253 | });
|
1254 | });
|
1255 |
|
1256 | if (loadThenMountPromises.length > 0) {
|
1257 | wasNoOp = false;
|
1258 | }
|
1259 | |
1260 |
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 | var mountPromises = getAppsToMount().filter(appToMount => appsToLoad.indexOf(appToMount) < 0).map(appToMount => {
|
1266 | return toBootstrapPromise(appToMount).then(() => unmountAllPromise).then(() => toMountPromise(appToMount));
|
1267 | });
|
1268 |
|
1269 | if (mountPromises.length > 0) {
|
1270 | wasNoOp = false;
|
1271 | }
|
1272 |
|
1273 | return unmountAllPromise.catch(err => {
|
1274 | callAllEventListeners();
|
1275 | throw err;
|
1276 | }).then(() => {
|
1277 | |
1278 |
|
1279 |
|
1280 |
|
1281 | callAllEventListeners();
|
1282 | return Promise.all(loadThenMountPromises.concat(mountPromises)).catch(err => {
|
1283 | pendingPromises.forEach(promise => promise.reject(err));
|
1284 | throw err;
|
1285 | }).then(() => finishUpAndReturn(false));
|
1286 | });
|
1287 | });
|
1288 | }
|
1289 |
|
1290 | function finishUpAndReturn() {
|
1291 | var callEventListeners = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
|
1292 | var returnValue = getMountedApps();
|
1293 |
|
1294 | if (callEventListeners) {
|
1295 | callAllEventListeners();
|
1296 | }
|
1297 |
|
1298 | pendingPromises.forEach(promise => promise.resolve(returnValue));
|
1299 |
|
1300 | try {
|
1301 | var appChangeEventName = wasNoOp ? "single-spa:no-app-change" : "single-spa:app-change";
|
1302 | window.dispatchEvent(new customEvent(appChangeEventName, getCustomEventDetail()));
|
1303 | window.dispatchEvent(new customEvent("single-spa:routing-event", getCustomEventDetail()));
|
1304 | } catch (err) {
|
1305 | |
1306 |
|
1307 |
|
1308 |
|
1309 | setTimeout(() => {
|
1310 | throw err;
|
1311 | });
|
1312 | }
|
1313 | |
1314 |
|
1315 |
|
1316 |
|
1317 |
|
1318 |
|
1319 |
|
1320 | appChangeUnderway = false;
|
1321 |
|
1322 | if (peopleWaitingOnAppChange.length > 0) {
|
1323 | |
1324 |
|
1325 |
|
1326 | var nextPendingPromises = peopleWaitingOnAppChange;
|
1327 | peopleWaitingOnAppChange = [];
|
1328 | reroute(nextPendingPromises);
|
1329 | }
|
1330 |
|
1331 | return returnValue;
|
1332 | }
|
1333 | |
1334 |
|
1335 |
|
1336 |
|
1337 |
|
1338 |
|
1339 |
|
1340 |
|
1341 | function callAllEventListeners() {
|
1342 | pendingPromises.forEach(pendingPromise => {
|
1343 | callCapturedEventListeners(pendingPromise.eventArguments);
|
1344 | });
|
1345 | callCapturedEventListeners(eventArguments);
|
1346 | }
|
1347 |
|
1348 | function getCustomEventDetail() {
|
1349 | var result = {
|
1350 | detail: {}
|
1351 | };
|
1352 |
|
1353 | if (eventArguments && eventArguments[0]) {
|
1354 | result.detail.originalEvent = eventArguments[0];
|
1355 | }
|
1356 |
|
1357 | return result;
|
1358 | }
|
1359 | }
|
1360 |
|
1361 | var started = false;
|
1362 | function start() {
|
1363 | started = true;
|
1364 | reroute();
|
1365 | }
|
1366 | function isStarted() {
|
1367 | return started;
|
1368 | }
|
1369 | setTimeout(() => {
|
1370 | if (!started) {
|
1371 | console.warn(formatErrorMessage(1, "singleSpa.start() has not been called, 5000ms after single-spa was loaded. Before start() is called, apps can be declared and loaded, but not bootstrapped or mounted."));
|
1372 | }
|
1373 | }, 5000);
|
1374 |
|
1375 | var devtools = {
|
1376 | getRawAppData,
|
1377 | reroute,
|
1378 | NOT_LOADED,
|
1379 | toLoadPromise,
|
1380 | toBootstrapPromise,
|
1381 | unregisterApplication
|
1382 | };
|
1383 |
|
1384 | if (window && window.__SINGLE_SPA_DEVTOOLS__) {
|
1385 | window.__SINGLE_SPA_DEVTOOLS__.exposedMethods = devtools;
|
1386 | }
|
1387 |
|
1388 | export { BOOTSTRAPPING, LOADING_SOURCE_CODE, LOAD_ERROR, MOUNTED, MOUNTING, NOT_BOOTSTRAPPED, NOT_LOADED, NOT_MOUNTED, SKIP_BECAUSE_BROKEN, UNMOUNTING, UPDATING, addErrorHandler, checkActivityFunctions, ensureJQuerySupport, getAppNames, getAppStatus, getMountedApps, mountRootParcel, navigateToUrl, registerApplication, removeErrorHandler, setBootstrapMaxTime, setMountMaxTime, setUnloadMaxTime, setUnmountMaxTime, start, triggerAppChange, unloadApplication };
|
1389 |
|