Skip to content

Instantly share code, notes, and snippets.

@shrynx
Created October 20, 2016 01:49
Show Gist options
  • Save shrynx/2c00a9e547e1cb9434ae997a06ed1431 to your computer and use it in GitHub Desktop.
Save shrynx/2c00a9e547e1cb9434ae997a06ed1431 to your computer and use it in GitHub Desktop.
// module a.js
let a
function setValueOfA(val) {
a = val
// we are now emmiting event 'aChanged'
// with data as value of a
events.emit('aChanged', a)
}
// module b.js
let b
function computeValueOfB(a) {
b = a + 100
}
// here we are subscribing the event 'aChanged',
// then module b is self computing itself.
events.on('aChanged', computeValueOfB)
// main.js
setValueOfA(50)
// we can see the value of b
// is now computed automatically by listening to a.
console.log("value of a is", a ,"and value of b is" ,b) // 150
// Now we change the value of a.
setValueOfA(100)
// When we change value of a again, the value of b reactively changes.
console.log("New value of a is", a ,"and now value of b is" ,b) // 200
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment