【问题标题】:How can the subscriber of an observable receive items emitted (all items during it's subscription time) through a time interval of 6 seconds?observable 的订阅者如何通过 6 秒的时间间隔接收发出的项目(在其订阅时间内的所有项目)?
【发布时间】:2018-05-06 07:13:31
【问题描述】:

我正在寻找一种方法来让我的可观察到的发射间隔。我有一个处理它的服务,以及使用该主题发出值的组件和一个需要以 6 秒间隔接收它的组件。

export class Service {

  constructor() {}

  private subject = new Subject<string>();
  public observable$ = this.toastMessage.asObservable();

  updateSubject(data) {
     this.subject.next(data);
  }

}

// somewhere else
export class Component {

  constructor(private srv: Service) {}

  ngOnInit() {
    this.srv.observable$.subscribe(data => {
       console.log(data, 'should emit once every 6 seconds');
    })
  }
}

// also elsewhere
export class ComponentUpdater {

  constructor(private srv: Service) {}

  ngOnInit() {
    this.srv.updateSubject('way');
    this.srv.updateSubject('to');
    this.srv.updateSubject('fast');
  }
}

有谁知道我如何使用 Subject 来做到这一点?

【问题讨论】:

  • thisthis 的可能重复
  • “每 6 秒更新一次(Subject)值”和“每 6 秒收听一个可观察对象”之间存在根本区别。你想达到哪一个?
  • @CozyAzure 我希望订阅者每 6 秒接收一次在其订阅时间内发出的所有值。
  • @fatemefazli 我正在尝试让它与附加的“主题”一起工作

标签: angular rxjs angular2-observables


【解决方案1】:

确保导入区间

import 'rxjs/add/observable/interval';

然后

   Observable.interval(6000).subscribe( x => {
          this.srv.observable$.subscribe(data => {
          console.log(data, 'should emit once every 6 seconds');
         })
   })

【讨论】:

  • 但是如何发送可观察的数据来发射?我正在尝试使用“主题”,以便可观察对象仅发出并且不能直接修改。
  • 当其他组件更新“主题”时,此代码也会跳过一些值,并且每 6 秒持续发出相同的最后一个值
【解决方案2】:

您可以通过使用bufferTimer-operator 来实现此目的

import { bufferTime, filter } from 'rxjs/operators';

export class Service {

  constructor() {}

  private subject = new Subject<string>();
  public observable$ = this.toastMessage.asObservable();

  updateSubject(data) {
    this.subject.next(data);
  }

}

// somewhere else
export class Component {

  constructor(private srv: Service) {}

  ngOnInit() {
    this.srv.observable$
      .pipe(
        bufferTime(6000),
        filter (data => data.length)
      )
      ^^^^^^^^^^^^^^^^^^^^^^^
      .subscribe(data => {
      console.log(data, 'should emit once every 6 seconds');
    })
  }
}

// also elsewhere
export class ComponentUpdater {

  constructor(private srv: Service) {}

  ngOnInit() {
    this.srv.updateSubject('way');
    this.srv.updateSubject('to');
    this.srv.updateSubject('fast');
  }
}

【讨论】:

  • 尝试使用缓冲区操作符,但它抛出错误“TypeError: Cannot read property 'lift' of undefined”——悬停在它上面时的类型错误还显示:“类型的 'this' 上下文'void' 不能分配给类型为 'Observable' 的方法的 'this'。” -- 我不确定,但我认为这可能与我的 observable 是如何通过 .asObservable() 创建的有关 -- 我不确定是否应该添加更多参数或对创建的 observable 做一些不同的事情。
  • @Jonathan002 抱歉,不知何故我搞砸了导入。尝试从rxjs/operators导入它
  • 感谢您的更新。我已经尝试过了,但是它一次发出所有项目,并且每 6 秒发出一个空白数组。我希望找到可以捕获所有发出的东西的东西。 var catch = ['way', 'too' , '快速地']。但是对于它捕获的每个数据,每 6 秒放慢一次并运行订阅功能。 0sec - data = catch[0]... 6sec 后 - data = catch[1]... 12sec 后 data = catch [2] ... 如果 catch = [] 订阅函数不应该被调用了..
  • @Jonathan002 你可以过滤掉空数组
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-30
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
相关资源
最近更新 更多