Unsubscribing from a subscription in dva

465 Views Asked by At

I have a model that will subscribe a websocket to several expensive end points when the user is on particular routes. When the user leaves the route, I want to disconnect the websockets.

The dva api docs say

Notice: if we want to unregister a model with app.unmodel(), it's subscriptions must return unsubscribe method.

However the docs do not include how to register a subscription with an unsubscribe method.

How does one create a subscription with an unsubscribe handler?

1

There are 1 best solutions below

0
On

It's necessary to add return to the end of your function.

  subscriptions: {
    setup() {
      emitter.on('event', () => {
        emitterCount += 1;
      });

      return () => {
        emitter.removeAllListeners();
      };
    },
  },