DEV Community

Discussion on: Intro to RxJS Concepts with Vanilla JavaScript

Collapse
 
babak2000ir profile image
Babak

Good job, fixed few things, here is the working code:

const observer = {
next: x => {
console.log(x)
},
error: err => {
console.log(err)
},
complete: () => {
console.log("done")
}
}

const observable = {
subscribe: observer => {
document.addEventListener("click", event => {
observer.next(event.clientX)
})
},
pipe: function (operator) {
return operator(this)
}
}

mostly here, correct pattern is return and return! ->
const map = function (f) {
return observable => {
return {
subscribe: observer => {
observable.subscribe({
next: x => {
observer.next(f(x))
},
error: err => {
console.error(err)
},
complete: () => {
console.log("finished")
}
})
},
pipe: function (operator) {
return operator(this)
},
}
}
}

//observable.subscribe(observer);
observable
.pipe(map(e => e.clientX))
.pipe(map(x => x - 1000))
.subscribe(observer)

aworking example for node.js using std-input is here too:

github.com/babak2000ir/rxjs-simula...