Firebase:如何停止收听快照

Firebase: How to stop listening to snapshot

我有一个 javascript 应用程序,它使用 onSnapshot 侦听器来侦听我的 firebase 集合中的更改。当我不再需要听众时,文档告诉我取消订阅。在 React 中,我会在 componentWillUnmount() 中执行此操作,但如果我在 vanilla class(不是 React)中设置监听器,在这种情况下我将如何取消订阅?

我是如何实施的...

class MyComponent extends React.Component {
  constructor() {
    super();
    this.presenter = new MyPresenter();
  }
  ...rest of component

class MyPresenter {
    constructor() {
      const listener = db.collection("cities")
        .onSnapshot(function () {});
    }
}

在我看来,您只想在 MyPresenter 上创建一个名为 "stop()" 的方法或类似的方法来取消订阅。然后,在您的组件中,在 componentWillUnmount().

期间调用 presenter.stop()