skip to content

Observable constructor

 

Lets you create your own Observable. How cool is that? 😎
Observable constructor takes in a function that will be called upon subscription. This function will decide how and when to notify the observer. It should return a teardown mechanism that will be activated when unsubscribed (for cleanup).

NOTE: While enjoying this might, don't forget that now you have greater responsibility. Take a better look at Creation section in the menu for ready-to-use factories.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const { rxObserver } = require('api/v0.3');
const { Observable } = require('rxjs');


const source$ = new Observable(observer => {
  let index = 0;

  // start pushing values to observer
  // once every 100ms
  const id = setInterval(()=>{
    observer.next(index++);
  }, 100);

  // return a teardown function
  return ()=>{
    clearInterval(id);
  };
});

source$
  .subscribe(rxObserver());
⚠️ Execution time is limited to 1000ms
0msstart00 11 22 33 44 55 66 77 88 99