Giter Club home page Giter Club logo

rxjs-demo's Introduction

rxjs-demo

setup

In a working directory run the following:

  npm install @reactivex/rxjs
  npm install requirejs

This will create node_modules/@reactivex/rxjs and a node_modules/requirejs directory. RxJS is written in a modular format so we'll use RequireJS to load and use RxJS.

Create a simple html page rxjs-demo.html that will run RxJS:

<html>
<head>
  <title>RxJS Demo</title>
  <script data-main="main" src="node_modules/requirejs/require.js"></script>
  
  <style>
    .main {
      margin: 20px;
      color: #667788;
      font-family:  Verdana, sans-serif;
    }
  </style>
</head>
<body>
  <div class="main">
    RxJS Demo
  </div>
</body>
</html>

The point of interest here is that we load require.js and we tell it to use main.js via data-main="main" as a starting point.

Now create main.js and throw in a little RxJS:

  requirejs(['node_modules/@reactivex/rxjs/dist/amd/Rx'], function(Rx) {
    var Observable = Rx.Observable;
    var body = document.querySelector('body');

    Observable.fromEvent(body, 'click')
    .subscribe(function () { console.log('The page was clicked!') });
  });

This uses the Rx.Observable object to create an Observable that will emit click events whenever the page is clicked. Then we subscribe to those events. When you open rxhs-demo.html in a browser you should see 'The page was clicked!' in the console whenever you click the page.

Congratulations, you've just run RxJS!

anatomy of an observer

  • next
  • error
  • complete

Let's create an observable that emits random numbers at random times.

We'll need a random function. Thank you MDN.

  function random(min, max) {
    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }

A numberMaker observable could look like this:

Now let's create some filters for the random number stream:

Now we subscribe to the page click observable to start and stop the new subscribers:

rxjs-demo's People

Contributors

jarvima avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.