Skip to content

Instantly share code, notes, and snippets.

@luistak
Created January 27, 2021 15:05
Show Gist options
  • Save luistak/944180571617588626e3918941440f57 to your computer and use it in GitHub Desktop.
Save luistak/944180571617588626e3918941440f57 to your computer and use it in GitHub Desktop.
Cross micro frontends communication Gists
function App() {
const [messages, setMessages] = useState([]);
const handleNewMessage = (event) => {
setMessages((currentMessages) => currentMessages.concat(event.detail));
};
useEffect(() => {
window.addEventListener('message', handleNewMessage);
return () => {
window.removeEventListener('message', handleNewMessage)
}
}, [handleNewMessage]);
...
}
function App({ onNewMessage }) {
const handleSubmit = (e) => {
e.preventDefault();
const { target: form } = e;
const input = form?.elements?.something;
const customEvent = new CustomEvent('message', { detail: input.value });
window.dispatchEvent(customEvent)
form.reset();
}
...
}
const App = ({ microfrontends }) => {
const [messages, setMessages] = useState([]);
const handleNewMessage = (message) => {
setMessages((currentMessages) => currentMessages.concat(message));
};
return (
<main className="App">
<div className="App__header">
<h1>⚔️ Cross microfrontend communication 📦</h1>
<p>Workerized example</p>
</div>
<div className="App__content">
<div className="App__content-container">
{
Object.keys(microfrontends).map(microfrontend => (
<Microfrontend
key={microfrontend}
microfrontend={microfrontends[microfrontend]}
customProps={{
messages,
onNewMessage: handleNewMessage,
}}
/>
))
}
</div>
</div>
</main>
);
}
function App({ messages = [] }) {
return (
<div className="MF">
<h3>Microfrontend 1️⃣</h3>
<p>New messages will be displayed below 👇</p>
<div className="MF__messages">
{messages.map((something, i) => <p key={something + i}>{something}</p>)}
</div>
</div>
);
}
function App({ onNewMessage }) {
const handleSubmit = (e) => {
e.preventDefault();
const { target: form } = e;
const input = form?.elements?.something;
onNewMessage(input.value);
form.reset();
}
...
}
import { Observable } from 'windowed-observable';
// Define a specific context namespace
const observable = new Observable('cart-items');
const observer = (item) => console.log(item);
// Add an observer subscribing to new events on this observable
observable.subscribe(observer)
// Unsubscribing
observable.unsubscribe(observer);
...
// On the publisher part of the app
const observable = new Observable('cart-items');
observable.publish({ id: 1234, name: 'Mouse Gamer XyZ', quantity: 1 });
import { Observable } from 'windowed-observable';
const observable = new Observable('messages');
function App() {
const [messages, setMessages] = useState([]);
const handleNewMessage = (newMessage) => {
setMessages((currentMessages) => currentMessages.concat(newMessage));
};
useEffect(() => {
observable.subscribe(handleNewMessage);
return () => {
observable.unsubscribe(handleNewMessage)
}
}, [handleNewMessage]);
...
}
import { Observable } from 'windowed-observable';
const observable = new Observable('messages');
function App() {
const handleSubmit = (e) => {
e.preventDefault();
const { target: form } = e;
const input = form?.elements?.something;
observable.publish(input.value);
form.reset();
}
...
}
// ...
import worky from 'worky';
window.worky = worky;
// ...
const { worky } = window;
function App() {
const [messages, setMessages] = useState([]);
const handleNewMessage = (message) => {
if (message.data.type) {
return;
}
setMessages((currentMessages) => currentMessages.concat(message.data));
};
useEffect(() => {
worky.addEventListener('message', handleNewMessage);
return () => {
worky.removeEventListener('message', handleNewMessage)
}
}, [handleNewMessage]);
return (
<div className="MF">
<h3>Microfrontend 1️⃣</h3>
<p>New messages will be displayed below 👇</p>
<div className="MF__messages">
{messages.map((something, i) => <p key={something + i}>{something}</p>)}
</div>
</div>
);
}
const { worky } = window;
function App() {
const handleSubmit = (e) => {
e.preventDefault();
const { target: form } = e;
const input = form?.elements?.something;
worky.say(input.value);
form.reset();
}
return (
<div className="MF">
<h3>Microfrontend 2️⃣</h3>
<p>⌨️ Use this form to communicate with the other microfrontend</p>
<form onSubmit={handleSubmit}>
<input type="text" name="something" placeholder="Type something in here"/>
<button type="submit">Communicate!</button>
</form>
</div>
);
}
let said = [];
export function say(message) {
console.log({ message, said });
said.push(message)
// This postMessage communicates with everyone listening to this worker
postMessage(message);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment