简体   繁体   中英

How to empty an observable in angular 4

I have created an observable, which is given below

  private permissionSubject = new Subject<any>();
  permissionObservable$ = this.permissionSubject.asObservable();


  constructor( public apiService: ApiService) { }

  updatePermissionsData(permissionData){
    this.permissionSubject.next(permissionData);
  }

  getPermissions(){
    return this.apiService.get('/getUserPrivileges')
    .map(data =>  data)
  }

Here what I am doing is, whenever I am getting the data, I am pushing the data to Observable

Ex: consider an observable -> [1, 2] and pushing 3 as it is new, data now observable will become [1, 2, 3]

But I want to remove 1, 2 value from Observable before pushing 3 to it. How can I do that?

Is Observable.empty() will do that, if it can, how can I update my code?

I have seen many questions in stackoverflow, but nothing helped :-( that's why I am asking this question again...

Updated code

Subscribing observable

    checkPermissions() {
            this.checkPermService.permissionObservable$.subscribe(
    // Below one is getting executed for so many times whenever 
observable get new data (stream data)
              data => {
                  this.saveMenuItemsOnPermissions(data)
              }
            )
          }

I think there is a misunderstanding of how Observables work. You have no buffer/memory structure in your code.

Your Code explained

// instance of a subject.
// Subjects don't have memory!! The stream is pushed to subscribers once.
private permissionSubject = new Subject<any>();


// Here you make a restriction on `permissionObservable$`, so it listens, but doesn't publish
permissionObservable$ = this.permissionSubject.asObservable();


// constructor instanciates apiService
constructor( public apiService: ApiService) { }

// each time this function is called, permissionData is pushed through
// permissionObservable and permissionObservable$ subscribers.
updatePermissionsData(permissionData){
   this.permissionSubject.next(permissionData);
}

// calls a service and waits for subscription (http call I suppose) 
// the map function is useless BTW
getPermissions(){
  return this.apiService.get('/getUserPrivileges')
  .map(data =>  data)
}

Observable.empty()

create an Observable that emits no items but terminates normally

Observable.empty() is not a method !! It is an observable whose purpose is to :

  1. emit nothing
  2. hang the stream

Edit:

If you just want to ignore the 2 first elements of an observable, you can use skip operator.

Skip operator :

Skip allows you to ignore the first x emissions from the source. Generally skip is used when you have an observable that always emits certain values on subscription that you wish to ignore. Perhaps those first few aren't needed or you are subscribing to a Replay or BehaviorSubject and do not need to act on the initial values. Reach for skip if you are only concerned about later emissions.

// Below one is getting executed for so many times whenever observable get new data (stream data)
checkPermissions() {
  this.checkPermService.permissionObservable$.skip(2)
     .subscribe( data => {
              this.saveMenuItemsOnPermissions(data)
      })
 }

There are 2 important points to bear in mind:

  1. Subscription must occur before observable starts emitting
  2. checkPermissions will ignore the 2 first received elements during subscription, but it will take all the following others.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM