Skip to content

Instantly share code, notes, and snippets.

@FlorianRappl
Created February 27, 2020 14:14
Show Gist options
  • Save FlorianRappl/ca90ef66944457772d21af6297383485 to your computer and use it in GitHub Desktop.
Save FlorianRappl/ca90ef66944457772d21af6297383485 to your computer and use it in GitHub Desktop.
const data = {};
const getDataGlobal = name => {
const item = data[name];
return item && item.value;
}
const setDataGlobal = (owner, name, value) => {
const previous = data[name];
if (!previous || previous.owner === owner) {
data[name] = {
owner,
name,
value,
};
window.dispatchEvent(new CustomEvent('changed-data', {
detail: {
name,
previous: previous && previous.value,
current: value,
},
}));
}
};
microfrontends.forEach(mife => {
const api = {
getData: getDataGlobal,
setData(name, value) {
setDataGlobal(mife.name, name, value);
},
};
const script = document.createElement('script');
script.src = mife.url;
script.onload = () => {
script.setup(api);
};
document.body.appendChild(script);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment