вызов утилизировать при подписке с компонентом WillUnMount не работает - PullRequest
0 голосов
/ 27 февраля 2019

утром у меня настроена релейная подписка, поэтому я неожиданно начал получать эту ошибку:

 Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

, поэтому я попытался реализовать некоторые решения, которые не смогли решить мою проблему: вот как выглядит мой код:

network.js

const networkSubscriptions = async (config, variables, cacheConfig, observer) => {
  const query = config.text;
  let token = await accessHelper();
  if (token != null || token != undefined) {
    const subscriptionClient = new SubscriptionClient(`ws://${api}/graphql`,
      {
        reconnect: true,
        connectionParams: {
          Authorization: token,
        },
      });
    const client = subscriptionClient.subscribe({ query, variables }, (error, result) => {
      observer.onNext({ data: result })
    })
    return {
      dispose: client.unsubscribe
    };
  }
}

subscription.js:

import {
  graphql,
  requestSubscription
} from 'react-relay'
import environment from '../network';

const subscription = graphql`
  subscription chatCreatedSubscription{
    chatCreated{
      id
      initiate_time
      update_time
      support_id
      category_id
      email
      name
    }
  }
  `;

function chatCreated(callback) {
  const variables = {};
  requestSubscription(environment, {
    subscription,
    variables,
    onError: (error)=> {
      console.log(error, "error");
    },
    onNext: () => {
      callback()
    },
    updater: () => {
      console.log("updater");
    }
  });
}

module.exports = chatCreated;

код для вызова подписки:

componentDidMount() {
    this.chat = chatCreated(this._refetch);
  }
componentWillUnmount() {
   this.chat.dispose()
}

, но сейчасthis.chat.dispose () выдает ошибку:

TypeError: TypeError: TypeError: TypeError: undefined is not an object (evaluating 'this.chat.dispose')

любая помощь и предложения приветствуются.Спасибо !!

...