如何正确使用rxjs Subject?

时间:2019-09-26 00:25:03

标签: angular rxjs

我已经定义了带事件的Callback函数,并在与UI交互时被调用。 事件包含有关已完成更改的数据。 调用CallBack Function时,我必须根据事件中的数据更新本地存储数据,然后将发布请求发送到服务器。 在某些时候,回调函数会同时被多次调用,这是预期的行为。 我的问题是,如何有时不向服务器发送多个请求,而仅在发出最后一个事件时发送。

我正在尝试类似的操作,但在某些情况下会发送很多请求。

callBackFinction: (event) => {
    this.MyService.subject.next(event);
    this.MyService.UpdateData(event);
    this.MyService.subject.pipe(debounceTime(1000)).subscribe(() => {
        http.post('/url', data);
    });
}

1 个答案:

答案 0 :(得分:1)

您需要使用Get-ChildItem | % { Rename-Item -LiteralPath $_.FullName -NewName ("Bass Mekanik" + $_.Name ) -WhatIf } ngOnInit中的主题。因为您将在每次回调时生成一个新的订阅,并且没有取消订阅。

constructor