Skip to content

nlarche/simple-observable

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Simple Observable

Simple Observable implementation from André Staltz presentation

André Staltz presentation from ng-europe 2016

Just to learn.

An Observable suscribe to an observer

An observer is an object with 3 functions (callback)

const observer = {
    next: function next(period) { console.log(`next -> ${period}`) },
    error: function error(err) { console.log(`error -> ${err}`) },
    complete: function complete() { console.log(`done`) }
}

An observable is an object with at least a function 'subscribe'

subscribe means give me some data

export default function createObservable(subscribe) {
    return {
        subscribe: subscribe,    
    }
}

Create an Observable from an array

const arrayObservable = createObservable(function subscribe(observer) {
    [10, 20, 30].forEach(x => observer.next(x));
    observer.complete();
});

Create an Observable from an event

const clickObservable = createObservable(function subscribe(observer) {
    document.addEventListener('click', observer.next)
});

Finally you can susbscribe to your observer

arrayObservable  
    .subscribe(observer);

clickObservable  
    .subscribe(observer);

About

Simple Observable implementation - rxjs - xstream

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published