Ben Nadel
On User Experience (UX) Design, JavaScript, ColdFusion, Node.js, Life, and Love.
Ben Nadel at BFusion / BFLEX 2010 (Bloomington, Indiana) with: Ed Bartram and Vaughn Bartram
Ben Nadel at BFusion / BFLEX 2010 (Bloomington, Indiana) with: Ed Bartram@edbartram ) and Vaughn Bartram

EventEmitter Is An RxJS Observable Stream In Angular 2 Beta 6

By Ben Nadel on

Yesterday, as I was looking through the Angular 2 Beta 6 source code, I noticed that the EventEmitter class - which powers component and directive outputs - extends the RxJS Subject class. Subject then, in turn, extends both the RxJS Observer and Observable classes. This means that the EventEmitter class is essentially an RxJS observable stream. Which, of course, means that we should be able to operate on it and subscribe to it just like we would any other RxJS stream. And, since I am still learning RxJS streams, I figured this would be a good subject (no pun intended) for a quick demo.


 
 
 

 
 
 
 
 

Run this demo in my JavaScript Demos project on GitHub.

Honestly, I couldn't really think of a good use-case for this off the top of my head. So, I came up with something that is a little contrived, but I think demonstrates the possibilities. In the following code, I'm using a host binding to pipe local event coordinates into the .next() method of an EventEmitter instance. I'm then transforming and subscribing to the EventEmitter such that I can translate the mouse coordinates into CSS style properties for an element within the view.

  • <!doctype html>
  • <html>
  • <head>
  • <meta charset="utf-8" />
  •  
  • <title>
  • EventEmitter Is An RxJS Observable Stream In Angular 2 Beta 6
  • </title>
  •  
  • <link rel="stylesheet" type="text/css" href="./demo.css"></link>
  • </head>
  • <body>
  •  
  • <h1>
  • EventEmitter Is An RxJS Observable Stream In Angular 2 Beta 6
  • </h1>
  •  
  • <my-app>
  • Loading...
  • </my-app>
  •  
  • <!-- Load demo scripts. -->
  • <script type="text/javascript" src="../../vendor/angularjs-2-beta/6/es6-shim.min.js"></script>
  • <script type="text/javascript" src="../../vendor/angularjs-2-beta/6/Rx.umd.min.js"></script>
  • <script type="text/javascript" src="../../vendor/angularjs-2-beta/6/angular2-polyfills.min.js"></script>
  • <script type="text/javascript" src="../../vendor/angularjs-2-beta/6/angular2-all.umd.js"></script>
  • <!-- AlmondJS - minimal implementation of RequireJS. -->
  • <script type="text/javascript" src="../../vendor/angularjs-2-beta/6/almond.js"></script>
  • <script type="text/javascript">
  •  
  • // Defer bootstrapping until all of the components have been declared.
  • // --
  • // NOTE: Not all components have to be required here since they will be
  • // implicitly required by other components.
  • requirejs(
  • [ /* Using require() for better readability. */ ],
  • function run() {
  •  
  • var App = require( "App" );
  •  
  • ng.platform.browser.bootstrap( App );
  •  
  • }
  • );
  •  
  •  
  • // --------------------------------------------------------------------------- //
  • // --------------------------------------------------------------------------- //
  •  
  •  
  • // I provide the root App component.
  • define(
  • "App",
  • function registerApp() {
  •  
  • // Configure the App component definition.
  • ng.core
  • .Component({
  • selector: "my-app",
  • // Instead of invoking a callback in our host binding, we're
  • // actually just piping the local event coordinates directly
  • // into the mouse stream (which we are subscribing to within
  • // the component controller).
  • host: {
  • "(mousemove)": "mouseStream.next({ x: $event.pageX, y: $event.pageY });"
  • },
  • template:
  • `
  • <div
  • class="dot"
  • [style.left.px]="position.left"
  • [style.top.px]="position.top">
  • </div>
  • `
  • })
  • .Class({
  • constructor: AppController
  • })
  • ;
  •  
  • return( AppController );
  •  
  •  
  • // I control the App component.
  • function AppController() {
  •  
  • var vm = this;
  •  
  • // I hold the position of the dot within the bounds of the component.
  • vm.position = {
  • left: -50,
  • top: -50
  • };
  •  
  • // As the user moves the mouse, we are going to pipe those (x,y)
  • // coordinates into this EventEmitter instance.
  • // --
  • // NOTE: This is populated with .next() values by the host-binding
  • // on the app component host element.
  • vm.mouseStream = new ng.core.EventEmitter();
  •  
  • // Since EventEmitter inherits from RxJS.Subject, which in turn
  • // inherits from RxJS.Observable, it means that the EventEmitter, in
  • // Angular 2, is actually an Observable sequence. Which, of course,
  • // means that we can operate on it and subscribe to it. In this
  • // stream, we're going to transform the raw mouse coordinates into
  • // coordinates that can be used to update the view.
  • // --
  • // NOTE: This stream is far more complicated than it needs to be - I
  • // am simply seizing this as an opportunity to experiment with stream
  • // mechanics and operators.
  • vm.mouseStream
  • // Offset the coordinates away from the mouse.
  • .map(
  • function offsetFromCursor( coordinate ) {
  •  
  • return({
  • x: ( coordinate.x - 25 ),
  • y: ( coordinate.y - 20 )
  • });
  •  
  • }
  • )
  • // Snap the coordinates to a 25x25 grid system.
  • .map(
  • function snapToGrid( coordinate ) {
  •  
  • var gridSize = 25;
  •  
  • return({
  • x: ( coordinate.x - ( coordinate.x % gridSize ) ),
  • y: ( coordinate.y - ( coordinate.y % gridSize ) )
  • });
  •  
  • }
  • )
  • // Don't pass on the next coordinate value until it has moved
  • // to another box in the grid system (this one really isn't
  • // necessary, I'm just experimenting with streams).
  • .distinctUntilChanged(
  • function comparator( a, b ) {
  •  
  • return( ( a.x === b.x ) && ( a.y === b.y ) );
  •  
  • }
  • )
  • // Log out the value that is being passed onto the subscriber.
  • // This does not affect the outcome of the value chain.
  • .do(
  • function asideNext( value ) {
  •  
  • console.log( "Passing through (%s,%s).", value.x, value.y );
  •  
  • }
  • )
  •  
  • // Here, we are actually subscribing to the stream of
  • // coordinates which we are using to update the position of the
  • // Dot in the view.
  • // --
  • // CAUTION: If this were a real-world scenario, we'd have to
  • // capture this subscription and then unsubscribe to it in the
  • // ngOnDestroy() life-cycle event handler.
  • .subscribe(
  • function handleNext( coordinate ) {
  •  
  • vm.position = {
  • left: coordinate.x,
  • top: coordinate.y
  • };
  •  
  • }
  • )
  • ;
  •  
  • }
  •  
  • }
  • );
  •  
  • </script>
  •  
  • </body>
  • </html>

As you can see, I'm taking the EventEmitter instance and (by way of chaining) I'm calling the following methods on it:

  • .map()
  • .map()
  • .distinctUntilChanged()
  • .do()
  • .subscribe() -> Returns a subscription which we are ignoring.

Basically, I'm treating it like it's an RxJS observable stream because, well, it is. And, when we run this code and move the mouse around, we get the following output:


 
 
 

 
 The EventEmitter class in Angular 2 is actually an RxJS observable sequence. 
 
 
 

The EventEmitter class powers the directive output bindings. So, I have no doubt that this "EventEmitter as RxJS stream" concept is actually powering a lot of functionality behind the scenes, below the surface of the framework. But, now that we know that it can be leveraged explicitly, it will be fun to see where else event-powered streams can make sense in our Angular 2 applications.




Reader Comments

It would be interesting to see how performant this type of app is when the application is run entirely in a webworker. See https://github.com/angular/angular/blob/master/modules/angular2/docs/web_workers/web_workers.md

Like how would you prevent default events etc
I have a feeling that webworker apps might become an anti-pattern for Angular2 apps, unless it is easy enough to balance where initial event processing takes place.

In any case given that apps can live on webworkers, it makes sense for events to be observables.

Reply to this Comment

@Steve,

That's a super interesting topic. To be honest, I don't really have my head wrapped around the idea of running the application in different environments, including the Server and Web Workers. I see that Angular 2 is doing a lot to keep the environment-specific properties encapsulated though things like the ElementRef and the Renderer. But, even in my little bit of experimentation so far, it seems near impossible to NOT depend on the browser whether its for the window.location or something else that doesn't seem to be readily available as an encapsulated area.

So, I have no idea :P

Reply to this Comment

Do NOT count on EventEmitter continuing to be an Observable!

Do NOT count on those Observable operators being there in the future!

These will be deprecated soon and probably removed before release.

Use EventEmitter only for event binding between a child and parent component. Do not subscribe to it. Do not call any of those methods. Only call `eve.emit()`

Reply to this Comment

Post A Comment

You — Get Out Of My Dreams, Get Into My Comments
Live in the Now
Oops!
Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.