Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to signal a BehaviorSubject that the stream is completed

In angular 2, mySubject (see code) compiles a complete() function, but it errors during execution saying there is no such function. I was unable to get onComplete() to compile.

    import { Component, OnInit } from '@angular/core';
    import { NgForm } from '@angular/forms';
    import * as Rx from "rxjs";
    import {BehaviorSubject} from 'rxjs/BehaviorSubject';

    @Component({
      selector: 'app-home',
      templateUrl: './home.component.html',
      styleUrls: ['./home.component.scss']
    })    
    export class HomeComponent {
      myBehavior: any;
      mySubject: BehaviorSubject<string>;
      received = "nothing";
      chatter: string[];
      nxtChatter = 0;
      constructor() {
        this.myBehavior = new BehaviorSubject<string>("Behavior Subject Started");
        this.chatter = [
          "Four", "score", "and", "seven", "years", "ago"
      ]        
    }        

      Start() {
        this.mySubject = this.myBehavior.subscribe(
          (x) => { this.received = x;},
          (err) => { this.received = "Error: " + err; },
          () => { this.received = "Completed ... bye"; }
        );
    }         

      Send() {
        this.mySubject.next(this.chatter[this.nxtChatter++]);
        if (this.nxtChatter >= this.chatter.length) {
           this.nxtChatter = 0;
           this.mySubject.complete();
        }    
       }    
    }        
like image 464
Yogi Bear Avatar asked Aug 04 '17 19:08

Yogi Bear


People also ask

How do you complete a BehaviorSubject?

BehaviorSubject works in the following way: Create an internal subscriptions container. Set the current value kept by the subject to the initial value passed as an argument during instantiation. When a new subscription occurs, add it to the container and emit the current value to the corresponding observer.

Can you subscribe to a BehaviorSubject?

You can either get the value by accessing the . value property on the BehaviorSubject or you can subscribe to it. If you subscribe to it, the BehaviorSubject will directly emit the current value to the subscriber. Even if the subscriber subscribes much later than the value was stored.

Is BehaviorSubject an observable?

BehaviorSubject is both observer and type of observable. BehaviorSubject always need an initial/default value. Every observer on subscribe gets current value. Current value is either latest value emitted by source observable using next() method or initial/default value.

When should I use ReplaySubject?

Use a ReplaySubject when you need more than the last given value (For example, the previous five values). Or you want to set a time window for the values can be validly sent to subscribers. Use an AsyncSubject when you only want the last value to be passed to the subscribers.


1 Answers

This line:

this.mySubject = this.myBehavior.subscribe(

returns a subscription object, not the subject. And subscription doesn't have a complete or next function. To trigger complete on the subject do the following:

this.myBehavior.complete();

And also here you're triggering next on subscription:

this.mySubject.next(this.chatter[this.nxtChatter++]);

You need to trigger it on the subject:

this.myBehavior.next(this.chatter[this.nxtChatter++]);

To learn more about BehaviorSubject see this resource.

like image 86
Max Koretskyi Avatar answered Oct 16 '22 06:10

Max Koretskyi