Observable connect

In this example below you will see how to do a Observable connect with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 1kohei1, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 1kohei1 ©

Technologies

  • HTML
  • CSS
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>Observable connect</title>
  
  
  
      <link rel="stylesheet" href="css/style.css">

  
</head>

<body>

  <div id="myWindow">
  <button id="subscribe">Subscribe</button>
  <button id="unsubscribe">Unsubscribe</button>
</div>
  <script src='https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.5.5/Rx.js'></script>

  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/1kohei1/observable-connect-QORxeV */
body, #myWindow {
  height: 1000px;
}

/*Downloaded from https://www.codeseek.co/1kohei1/observable-connect-QORxeV */
const a = document.getElementById("myWindow");
const b = document.getElementById("subscribe");
const c = document.getElementById("unsubscribe");

const x$ = Rx.Observable.fromEvent(a, 'mousemove')
.publish()
.refCount();

const y$ = Rx.Observable.fromEvent(b, 'click');
const z$ = Rx.Observable.fromEvent(c, 'click');

let sub = null;

y$.subscribe(() => {
  sub = x$.subscribe(e => console.log(e));
});
z$.subscribe(() => {
  sub.unsubscribe();
})

Comments