Simple Observable implementation from André Staltz presentation
Just to learn.
const observer = {
next: function next(period) { console.log(`next -> ${period}`) },
error: function error(err) { console.log(`error -> ${err}`) },
complete: function complete() { console.log(`done`) }
}
export default function createObservable(subscribe) {
return {
subscribe: subscribe,
}
}
const arrayObservable = createObservable(function subscribe(observer) {
[10, 20, 30].forEach(x => observer.next(x));
observer.complete();
});
const clickObservable = createObservable(function subscribe(observer) {
document.addEventListener('click', observer.next)
});
arrayObservable
.subscribe(observer);
clickObservable
.subscribe(observer);