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...
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
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...