single-spa
Advanced tools
Comparing version 2.0.0-alpha-3 to 2.0.0-alpha-4
@@ -16,2 +16,3 @@ 'use strict'; | ||
console.log('---------------'); | ||
console.error(message); | ||
console.error(new SingleSpaChildAppError(message).stack); | ||
@@ -18,0 +19,0 @@ console.log('---------------'); |
@@ -23,2 +23,7 @@ 'use strict'; | ||
var _jquerySupport = require('./jquery-support.js'); | ||
var originalAddEventListener = window.addEventListener; | ||
var originalRemoveEventListener = window.removeEventListener; | ||
// App statuses | ||
@@ -41,3 +46,4 @@ var NOT_BOOTSTRAPPED = 'NOT_BOOTSTRAPPED', | ||
peopleWaitingOnAppChange = void 0, | ||
appChangeUnderway = void 0; | ||
appChangeUnderway = void 0, | ||
capturedEventListeners = void 0; | ||
@@ -57,4 +63,67 @@ function reset() { | ||
window.addEventListener('hashchange', triggerAppChange); | ||
window.addEventListener('popstate', triggerAppChange); | ||
window.addEventListener('hashchange', urlReroute); | ||
window.addEventListener('popstate', urlReroute); | ||
capturedEventListeners = { | ||
hashchange: [], | ||
popstate: [] | ||
}; | ||
window.addEventListener = function (eventName, fn) { | ||
if (typeof fn === 'function') { | ||
if (eventName === 'hashchange' && !capturedEventListeners.hashchange.find(function (listener) { | ||
return listener === fn; | ||
})) { | ||
capturedEventListeners.hashchange.push(fn); | ||
return; | ||
} else if (eventName === 'popstate' && !capturedEventListeners.popstate.find(function (listener) { | ||
return listener === fn; | ||
})) { | ||
capturedEventListeners.popstate.push(fn); | ||
return; | ||
} | ||
} | ||
return originalAddEventListener.apply(this, arguments); | ||
}; | ||
window.removeEventListener = function (eventName, listenerFn) { | ||
if (typeof listenerFn === 'function') { | ||
if (eventName === 'hashchange') { | ||
capturedEventListeners.hashchange = capturedEventListeners.hashchange.filter(function (fn) { | ||
return fn.toString() !== listenerFn.toString(); | ||
}); | ||
return; | ||
} else if (eventName === 'popstate') { | ||
capturedEventListeners.popstate = capturedEventListeners.popstate.filter(function (fn) { | ||
return fn.toString() !== listenerFn.toString(); | ||
}); | ||
return; | ||
} | ||
} | ||
return originalRemoveEventListener.apply(this, arguments); | ||
}; | ||
var originalPushState = window.history.pushState; | ||
window.history.pushState = function (state) { | ||
var result = originalPushState.apply(this, arguments); | ||
var popstateEvent = new PopStateEvent('popstate', { | ||
target: window, | ||
type: 'popstate', | ||
bubbles: true, | ||
cancelable: false, | ||
state: state | ||
}); | ||
document.dispatchEvent(popstateEvent); | ||
return result; | ||
}; | ||
window.history.replaceState = function () { | ||
var result = originalReplaceState.apply(this, arguments); | ||
performAppChanges(); | ||
return result; | ||
}; | ||
} | ||
@@ -117,2 +186,4 @@ // initialize for the first time | ||
(0, _jquerySupport.ensureJQuerySupport)(); | ||
triggerAppChange(); | ||
@@ -125,4 +196,9 @@ } | ||
function urlReroute() { | ||
performAppChanges([], arguments); | ||
} | ||
function performAppChanges() { | ||
var pendingPromises = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0]; | ||
var eventArguments = arguments[1]; | ||
@@ -148,2 +224,3 @@ // console.log('\n\n\n') | ||
Promise.all(unmountPromises).then(function () { | ||
callCapturedEventListeners(); | ||
// console.log('done unmounting apps') | ||
@@ -165,6 +242,25 @@ | ||
}).catch(reject); | ||
}).catch(reject); | ||
}).catch(function (ex) { | ||
callCapturedEventListeners(); | ||
reject(ex); | ||
}); | ||
function callCapturedEventListeners() { | ||
var _this = this; | ||
if (eventArguments) { | ||
if (eventArguments[0].type === 'hashchange') { | ||
capturedEventListeners.hashchange.forEach(function (listener) { | ||
listener.apply(_this, eventArguments); | ||
}); | ||
} else if (eventArguments[0].type === 'popstate') { | ||
capturedEventListeners.popstate.forEach(function (listener) { | ||
listener.apply(_this, eventArguments); | ||
}); | ||
} | ||
} | ||
} | ||
function resolve() { | ||
var _this = this, | ||
var _this2 = this, | ||
_arguments = arguments; | ||
@@ -175,3 +271,3 @@ | ||
pendingPromises.forEach(function (promise) { | ||
return promise.resolve.apply(_this, _arguments); | ||
return promise.resolve.apply(_this2, _arguments); | ||
}); | ||
@@ -187,3 +283,3 @@ | ||
function reject() { | ||
var _this2 = this, | ||
var _this3 = this, | ||
_arguments2 = arguments; | ||
@@ -193,3 +289,3 @@ | ||
pendingPromises.forEach(function (promise) { | ||
return promise.reject.apply(_this2, _arguments2); | ||
return promise.reject.apply(_this3, _arguments2); | ||
}); | ||
@@ -196,0 +292,0 @@ } |
{ | ||
"name": "single-spa", | ||
"version": "2.0.0-alpha-3", | ||
"version": "2.0.0-alpha-4", | ||
"description": "Multiple applications, one page", | ||
@@ -12,3 +12,3 @@ "main": "lib/single-spa.js", | ||
"test": "jasmine", | ||
"build:canopy": "npm run build && rm -rf ../../workplace/customer-spa/jspm_packages/npm/single-spa@2.0.0-alpha-3 && rsync -av . ../../workplace/customer-spa/jspm_packages/npm/single-spa@2.0.0-alpha-3 --exclude node_modules --exclude .git --exclude --spec --exclude examples" | ||
"build:canopy": "npm run build && rm -rf ../../workplace/customer-spa/jspm_packages/npm/single-spa@2.0.0-alpha-4 && rsync -av . ../../workplace/customer-spa/jspm_packages/npm/single-spa@2.0.0-alpha-4 --exclude node_modules --exclude .git --exclude --spec --exclude examples" | ||
}, | ||
@@ -15,0 +15,0 @@ "repository": "https://github.com/joeldenning/single-spa", |
@@ -10,2 +10,3 @@ export function handleChildAppError(message) { | ||
console.log(`---------------`) | ||
console.error(message); | ||
console.error((new SingleSpaChildAppError(message)).stack); | ||
@@ -12,0 +13,0 @@ console.log(`---------------`) |
import { handleChildAppError } from './single-spa-child-app-error.js'; | ||
import { ensureJQuerySupport } from './jquery-support.js'; | ||
const originalAddEventListener = window.addEventListener; | ||
const originalRemoveEventListener = window.removeEventListener; | ||
// App statuses | ||
@@ -14,3 +18,3 @@ const NOT_BOOTSTRAPPED = 'NOT_BOOTSTRAPPED', | ||
// Things that need to be reset with the init function; | ||
let Loader, childApps, bootstrapMaxTime, mountMaxTime, unmountMaxTime, peopleWaitingOnAppChange, appChangeUnderway; | ||
let Loader, childApps, bootstrapMaxTime, mountMaxTime, unmountMaxTime, peopleWaitingOnAppChange, appChangeUnderway, capturedEventListeners; | ||
@@ -30,4 +34,59 @@ export function reset() { | ||
window.addEventListener('hashchange', triggerAppChange); | ||
window.addEventListener('popstate', triggerAppChange); | ||
window.addEventListener('hashchange', urlReroute); | ||
window.addEventListener('popstate', urlReroute); | ||
capturedEventListeners = { | ||
hashchange: [], | ||
popstate: [], | ||
}; | ||
window.addEventListener = function(eventName, fn) { | ||
if (typeof fn === 'function') { | ||
if (eventName === 'hashchange' && !capturedEventListeners.hashchange.find(listener => listener === fn)) { | ||
capturedEventListeners.hashchange.push(fn); | ||
return; | ||
} else if (eventName === 'popstate' && !capturedEventListeners.popstate.find(listener => listener === fn)) { | ||
capturedEventListeners.popstate.push(fn); | ||
return; | ||
} | ||
} | ||
return originalAddEventListener.apply(this, arguments); | ||
} | ||
window.removeEventListener = function(eventName, listenerFn) { | ||
if (typeof listenerFn === 'function') { | ||
if (eventName === 'hashchange') { | ||
capturedEventListeners.hashchange = capturedEventListeners.hashchange.filter(fn => fn.toString() !== listenerFn.toString()); | ||
return; | ||
} else if (eventName === 'popstate') { | ||
capturedEventListeners.popstate = capturedEventListeners.popstate.filter(fn => fn.toString() !== listenerFn.toString()); | ||
return; | ||
} | ||
} | ||
return originalRemoveEventListener.apply(this, arguments); | ||
} | ||
const originalPushState = window.history.pushState; | ||
window.history.pushState = function(state) { | ||
const result = originalPushState.apply(this, arguments); | ||
const popstateEvent = new PopStateEvent('popstate', { | ||
target: window, | ||
type: 'popstate', | ||
bubbles: true, | ||
cancelable: false, | ||
state: state | ||
}); | ||
document.dispatchEvent(popstateEvent); | ||
return result; | ||
} | ||
window.history.replaceState = function() { | ||
const result = originalReplaceState.apply(this, arguments); | ||
performAppChanges(); | ||
return result; | ||
} | ||
} | ||
@@ -91,2 +150,4 @@ // initialize for the first time | ||
ensureJQuerySupport(); | ||
triggerAppChange(); | ||
@@ -99,3 +160,7 @@ } | ||
function performAppChanges(pendingPromises = []) { | ||
function urlReroute() { | ||
performAppChanges([], arguments) | ||
} | ||
function performAppChanges(pendingPromises = [], eventArguments) { | ||
// console.log('\n\n\n') | ||
@@ -126,2 +191,3 @@ if (appChangeUnderway) { | ||
.then(() => { | ||
callCapturedEventListeners(); | ||
// console.log('done unmounting apps') | ||
@@ -156,4 +222,21 @@ | ||
}) | ||
.catch(reject); | ||
.catch(ex => { | ||
callCapturedEventListeners(); | ||
reject(ex); | ||
}); | ||
function callCapturedEventListeners() { | ||
if (eventArguments) { | ||
if (eventArguments[0].type === 'hashchange') { | ||
capturedEventListeners.hashchange.forEach(listener => { | ||
listener.apply(this, eventArguments); | ||
}); | ||
} else if (eventArguments[0].type === 'popstate') { | ||
capturedEventListeners.popstate.forEach(listener => { | ||
listener.apply(this, eventArguments); | ||
}); | ||
} | ||
} | ||
} | ||
function resolve() { | ||
@@ -160,0 +243,0 @@ _resolve.apply(this, arguments); |
44793
12
993