简体   繁体   中英

How to unsubscribe in Relay Modern

How do you unsubscribe from a subscription in Relay Modern?

I have followed the subscription tutorial on How to GraphQL React + Relay but it has no mention on how you unsubscribe either does the Relay Modern website.

Any help would be awesome.

UPDATE ----------

According to Lee Byron( see GitHub issue ) you just need to call dispose() on the requestSubscription()

After making the following modifications to the example:

./src/subscriptions/NewVoteSubscription.js (Adding return to requestSubscription)

export default () => {
    const subscriptionConfig = {
    subscription: newVoteSubscription,
    variables: {},
    updater: proxyStore => {
        const createVoteField = proxyStore.getRootField('Vote')
        const newVote = createVoteField.getLinkedRecord('node')
        const updatedLink = newVote.getLinkedRecord('link')
        const linkId = updatedLink.getValue('id')
        const newVotes = updatedLink.getLinkedRecord('_votesMeta')
        const newVoteCount = newVotes.getValue('count')

        const link = proxyStore.get(linkId)
        link.getLinkedRecord('votes').setValue(newVoteCount, 'count')
    },
    onError: error => console.log(`An error occured:`, error)
  }

  return requestSubscription(
      environment,
      subscriptionConfig
  )

./src/components/LinkList.js (Setting the subscription on the component and then using componentWillUnmount to dispose() it)

componentDidMount() {
    this.subscription = NewVoteSubscription()
}

componentWillUnmount() {
    this.subscription.dispose()
}

Here is the error I get:

Uncaught TypeError: Cannot read property 'dispose' of undefined
    at RelayObservable.js:94
    at doCleanup (RelayObservable.js:453)
    at Object.unsubscribe (RelayObservable.js:474)
    at RelayObservable.js:330
    at doCleanup (RelayObservable.js:453)
    at Object.unsubscribe (RelayObservable.js:474)
    at doCleanup (RelayObservable.js:450)
    at Object.unsubscribe [as dispose] (RelayObservable.js:474)
    at LinkList.componentWillUnmount (LinkList.js:18)
    at callComponentWillUnmountWithTimerInDev (react-dom.development.js:11123)
    at HTMLUnknownElement.callCallback (react-dom.development.js:1309)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:1348)
    at invokeGuardedCallback (react-dom.development.js:1205)
    at safelyCallComponentWillUnmount (react-dom.development.js:11131)
    at commitUnmount (react-dom.development.js:11421)
    at unmountHostComponents (react-dom.development.js:11362)
    at commitDeletion (react-dom.development.js:11392)
    at commitAllHostEffects (react-dom.development.js:12279)
    at HTMLUnknownElement.callCallback (react-dom.development.js:1309)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:1348)
    at invokeGuardedCallback (react-dom.development.js:1205)
    at commitAllWork (react-dom.development.js:12384)
    at workLoop (react-dom.development.js:12695)
    at HTMLUnknownElement.callCallback (react-dom.development.js:1309)
    at Object.invokeGuardedCallbackDev (react-dom.development.js:1348)
    at invokeGuardedCallback (react-dom.development.js:1205)
    at performWork (react-dom.development.js:12808)
    at batchedUpdates (react-dom.development.js:13262)
    at performFiberBatchedUpdates (react-dom.development.js:1656)
    at stackBatchedUpdates (react-dom.development.js:1647)
    at batchedUpdates (react-dom.development.js:1661)
    at Object.batchedUpdatesWithControlledComponents [as batchedUpdates] (react-dom.development.js:1674)
    at dispatchEvent (react-dom.development.js:1884)

and setup RelayEnvironment

function setupSubscription(config, variables, cacheConfig, observer) {
  const query = config.text;

  const subscriptionClient = new SubscriptionClient(websocketURL, {
    reconnect: true
  });

  const client = subscriptionClient.request({ query, variables }).subscribe({
    next: result => {
      observer.onNext({ data: result.data });
    },
    complete: () => {
      observer.onCompleted();
    },
    error: error => {
      observer.onError(error);
    }
  });

  return {
    dispose: client.unsubscribe
  };
}

Since relay-modern@6 you need return observable otherwise it will crash due this error: RelayObservable: Unhandled Error TypeError: source.subscribe is not a function

instead of

return {
    dispose: client.unsubscribe
};

you need return

return Observable.create(() => {
  // return cleanup function
  return yourFunctionToCleanUp;
});

where Observable you can import from relay-runtime .

Like:

import {
  Observable,
} from 'relay-runtime';

so when you will call this.subscription.dispose() in componentWillUnmount in reality it will call function yourFunctionToCleanUp() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM