cpunion / react-native-actioncable

[Unmaintained] ActionCable for react native
MIT License
59 stars 16 forks source link

remove not unsubscribing from channel #22

Closed hcyildirim closed 5 years ago

hcyildirim commented 5 years ago

Hi,

I have a channel which I can subscribe, but can't unsubscribe. Cable subscriptions count is always 0, not increasing or decreasing.

 constructor(props) {
    super(props);

    this.cable = null;
    this.myChannel = null;
  }

  componentWillMount() {
    this.cable = RNActionCable.createConsumer(
      `${Config.WEB_SOCKET_URL}?access-token=${accessToken}&uid=${uid}&client=${client}`,
    );

    if (_.isEmpty(this.myChannel)) {
      this.myChannel = this.cable.subscriptions.create('MyChannel', {
        received(data) {
          console.log(data);
        },
      });
    }
  }

  componentWillUnmount() {
    console.log(this.cable.subscriptions) // "0"
    this.cable.subscriptions.remove(this.myChannel);
    console.log(this.cable.subscriptions) // "0"
    console.log(this.myChannel.consumer.subscriptions) // "1"
  }

MyChannel subscriptions is 1, cable subscriptions is 0 so I think remove method not working or I have a problem with my code.

hcyildirim commented 5 years ago

Oh, I figured it out, it's not working if you assign subscription to a variable.

Not working:

      this.myChannel = this.cable.subscriptions.create('MyChannel', {
        received(data) {
          console.log(data);
        },
      });

Working:

      this.cable.subscriptions.create('MyChannel', {
        received(data) {
          console.log(data);
        },
      });