Reactive Programming with RxJS

Extracted from:

Reactive Programming with RxJS

Untangle Your Asynchronous JavaScript Code

This PDF file contains pages extracted from Reactive Programming with RxJS, published by the Pragmatic Bookshelf. For more information or to purchase a

paperback or PDF copy, please visit . Note: This extract contains some colored text (particularly in code listing). This is available only in online versions of the books. The printed versions are black and white. Pagination might vary between the online and printed versions; the

content is otherwise identical. Copyright ? 2015 The Pragmatic Programmers, LLC.

All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise,

without the prior consent of the publisher.

The Pragmatic Bookshelf

Raleigh, North Carolina

Reactive Programming with RxJS

Untangle Your Asynchronous JavaScript Code Sergi Mansilla

The Pragmatic Bookshelf

Raleigh, North Carolina

Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and The Pragmatic Programmers, LLC was aware of a trademark claim, the designations have been printed in initial capital letters or in all capitals. The Pragmatic Starter Kit, The Pragmatic Programmer, Pragmatic Programming, Pragmatic Bookshelf, PragProg and the linking g device are trademarks of The Pragmatic Programmers, LLC.

Every precaution was taken in the preparation of this book. However, the publisher assumes no responsibility for errors or omissions, or for damages that may result from the use of information (including program listings) contained herein.

Our Pragmatic books, screencasts, and audio books can help you and your team create better software and have more fun. Visit us at .

The team that produced this book includes:

Rebecca Gulick (editor) Potomac Indexing, LLC (index) Candace Cunningham (copyedit) Dave Thomas (layout) Janet Furlow (producer) Ellie Callahan (support)

For sales, volume licensing, and support, please contact support@.

For international rights, please contact rights@.

Copyright ? 2015 The Pragmatic Programmers, LLC.

All rights reserved.

No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form, or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior consent of the publisher.

Printed in the United States of America. ISBN-13: 978-1-68050-129-2 Encoded using the finest acid-free high-entropy binary digits. Book version: P2.0--August 2016

RxJS's Subject Class

A Subject is a type that implements both Observer and Observable types. As an Observer, it can subscribe to Observables, and as an Observable it can produce values and have Observers subscribe to it.

In some scenarios a single Subject can do the work of a combination of Observers and Observables. For example, for making a proxy object between a data source and the Subject's listeners, we could use this:

spaceship_reactive/subjects.js

var subject = new Rx.Subject(); var source = Rx.Observable.interval(300)

.map(function(v) { return 'Interval message #' + v; }) .take(5);

source.subscribe(subject);

var subscription = subject.subscribe( function onNext(x) { console.log('onNext: ' + x); }, function onError(e) { console.log('onError: ' + e.message); }, function onCompleted() { console.log('onCompleted'); }

);

subject.onNext('Our message #1'); subject.onNext('Our message #2');

setTimeout(function() { subject.onCompleted();

}, 1000);

Output:

onNext: Our message #1

onNext: Our message #2 onNext: Interval message #0 onNext: Interval message #1 onNext: Interval message #2 onCompleted

In the preceding example we create a new Subject and a source Observable that emits an integer every 300 milliseconds. Then we subscribe the Subject to the Observable. After that, we subscribe an Observer to the Subject itself. The Subject now behaves as an Observable.

Next we make the Subject emit values of its own (message1 and message2). In the final result, we get the Subject's own messages and then the proxied values from the source Observable. The values from the Observable come later because they are asynchronous, whereas we made the Subject's own values immediate. Notice that even if we tell the source Observable to take the first

? Click HERE to purchase this book now. discuss

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download