rxjs code example

Example 1: rxjs

content_copy
      
      
        open_in_new
      
      let count = 0;
let rate = 1000;
let lastClick = Date.now() - rate;
document.addEventListener('click', () => {
  if (Date.now() - lastClick >= rate) {
    console.log(`Clicked ${++count} times`);
    lastClick = Date.now();
  }
});

Example 2: rxjs que recibe como parametro un observable

import { Observable } from 'rxjs';const fromEvent = (node, name) => Observable.create((observer) => {  function handler(event) {    observer.next(event)  }  node.addEventListener(name, handler);  return () => {    node.removeEventListener(name, handler);  };});

Example 3: rxjs que recibe como parametro un observable

const $button = document.getElementById('main-button');const click$ = fromEvent($button, 'click');const subscription = click$.subscribe({  next: (e) => console.log('Event :', e)});// Luego de 5 segundos se cancela la suscripción a click$setTimeout(() => {  subscription.unsubscribe();}, 5000);