天天看點

[RxJS] Build your own RxJS

JavaScript has multiple APIs that use callback functions that all do nearly the same thing with slight variations. Event listeners, array methods such as <code>.forEach</code>, promises, and NodeJS streams all are very close in the way they are written. Instead, in RxJS you'd unify all of these APIs under one abstraction.

Normal RxJS API:

We can build our own RxJS operator

  it has API:

We can create a function call 'createObservable(subscribe)', take a subscribe function, return a subscribe and pipe function:

We can use it to create Observables:

  Observer is easy, it takes a object which contains 'next', 'error', 'complete' functions:

map(fn)(observable) filter(predFn)(observable)  

It is important to know that map &amp; filter, those operator, takes an inputObservable and will return an outputObservable.

We subscribe inputObservable, and inputObserver, inside inputObserver, we call outputObserver which is passed in from the consumer.

--

Full Code:

繼續閱讀