如何取消订阅Relay Modern中的订阅?
我已经关注了How to GraphQL React + Relay上的订阅教程,但是没有提及你如何取消订阅Relay Modern网站。
任何帮助都会很棒。
更新----------
根据Lee Byron(see GitHub issue),您只需致电dispose()
在requestSubscription()
对示例进行以下修改后:
./ src / subscriptions / NewVoteSubscription.js (将return
添加到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 (在组件上设置订阅,然后使用componentWillUnmount
到dispose()
)
componentDidMount() {
this.subscription = NewVoteSubscription()
}
componentWillUnmount() {
this.subscription.dispose()
}
这是我得到的错误:
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)
答案 0 :(得分:4)
并设置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
};
}
答案 1 :(得分:0)
由于relay-modern@6
,您需要返回observable,否则将由于以下错误而崩溃:RelayObservable: Unhandled Error TypeError: source.subscribe is not a function
代替
return {
dispose: client.unsubscribe
};
您需要退货
return Observable.create(() => {
// return cleanup function
return yourFunctionToCleanUp;
});
可以从relay-runtime
导入的地方。
赞:
import {
Observable,
} from 'relay-runtime';
因此,当您实际上在this.subscription.dispose()
中调用componentWillUnmount
时,它将调用函数yourFunctionToCleanUp()
。