Skip to content

Instantly share code, notes, and snippets.

@dhl
Last active February 28, 2018 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dhl/a10167dcc76310c909dedf6951e596cb to your computer and use it in GitHub Desktop.
Save dhl/a10167dcc76310c909dedf6951e596cb to your computer and use it in GitHub Desktop.
Example of Inter Object Communication with EventEmitter3
<!doctype>
<html>
<head>
<title>Inter Object Communication Example</title>
</head>
<body>
<h1>OrderBooks</h1>
<p id="orders"></p>
<script src="https://unpkg.com/eventemitter3@latest/umd/eventemitter3.min.js"></script>
<script>
const messageBus = new EventEmitter3()
Core = {
setMessageBus(bus) {
this.bus = bus
},
updateOrderbook(orders) {
this.bus.emit('core:orderBookUpdated', orders)
}
}
UI = {
setMessageBus(bus) {
this.bus = bus
},
subscribeToOrderBookUpdate() {
this.bus.on('core:orderBookUpdated', orders => {
document.getElementById('orders').innerText = JSON.stringify(orders)
})
}
}
const core = Object.create(Core)
const ui = Object.create(UI)
core.setMessageBus(messageBus);
ui.setMessageBus(messageBus);
ui.subscribeToOrderBookUpdate()
core.updateOrderbook([{price: 10, volume: 1000000}, {price: 0.42, volume: 10000}])
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment