Skip to content

Instantly share code, notes, and snippets.

View shrynx's full-sized avatar
🤦‍♂️
github statuses are stupid. waiting for github stories

shrynx shrynx

🤦‍♂️
github statuses are stupid. waiting for github stories
View GitHub Profile
const time$ = Rx.Observable.interval(1000)
const filtered$ = time$
.filter(x => x%2 === 0)
.map(x => `${x} seconds have passed`)
filtered$.subscribe(x => console.log(x))
// Select the button we need need to listen upon.
const myButton = document.querySelector('#click-me')
// Create a stream of clicks, using fromEvent function,
// by passing the button and the event listener.
const click$ = Rx.Observable.fromEvent(myButton, 'click')
// From the click stream, we create a new stream,
// where we first collect all the clicks
// that happen in 250ms inverval of the previous click
// and group them together.
const dbClick$ = click$.bufferWhen(() => click$.debounceTime(250))
// Select the button we need need to listen upon.
const myButton = document.querySelector('#click-me')
// Create a stream of clicks, using fromEvent function,
// by passing the button and the event listener.
const click$ = Rx.Observable.fromEvent(myButton, 'click')
// declaring an independent variable
let a = 50
// declaring a mathematical constraint between the variable a and b
let b = a + 100
// variable b is now holding value 150
console.log(b) // 150
// manipulating value of variable a
a = 100
// variable b is still holding value 150
console.log(b) // 150
// module a.js
let a
function assignValueToA(val) {
a = val
calculateValueOfB(a)
}
// module b.js
let b
function calculateValueOfB(a) {
b = a + 100
// 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
const events = {
events: {},
on: function (eventName, fn) {
this.events[eventName] = this.events[eventName] || [];
this.events[eventName].push(fn);
},
emit: function (eventName, data) {
if (this.events[eventName]) {
this.events[eventName].map(function(fn) {
fn(data);