-
-
Save FlorianRappl/82cebd993bf73676e6b160a9ec2b27a4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const scripts = [ | |
'https://example.com/script1.js', | |
'https://example.com/script2.js', | |
]; | |
const registrations = {}; | |
function activityCheck(name) { | |
const current = location.hash; | |
const registration = registrations[name]; | |
if (registration) { | |
if (registration.activity(current) !== registration.active) { | |
if (registration.active) { | |
registration.lifecycle.unmount(); | |
} else { | |
registration.lifecycle.mount(); | |
} | |
registration.active = !registration.active; | |
} | |
} | |
} | |
window.addEventListener('hashchange', function () { | |
Object.keys(registrations).forEach(activityCheck); | |
}); | |
window.registerApp = function(name, activity, lifecycle) { | |
registrations[name] = { | |
activity, | |
lifecycle, | |
active: false, | |
}; | |
activityCheck(name); | |
} | |
scripts.forEach(src => { | |
const script = document.createElement('script'); | |
script.src = src; | |
document.body.appendChild(script); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment