Last active
February 28, 2018 16:07
-
-
Save dhl/a10167dcc76310c909dedf6951e596cb to your computer and use it in GitHub Desktop.
Example of Inter Object Communication with EventEmitter3
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
<!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