Skip to content
This repository has been archived by the owner on Apr 13, 2023. It is now read-only.

Add complete handler to Subscription, to handle the case when the ser… #2716

Merged
merged 10 commits into from
Jan 24, 2019
12 changes: 12 additions & 0 deletions src/Subscriptions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface SubscriptionProps<TData = any, TVariables = OperationVariables>
shouldResubscribe?: any;
client?: ApolloClient<Object>;
onSubscriptionData?: (options: OnSubscriptionDataOptions<TData>) => any;
onSubscriptionComplete?: () => any;
hwillson marked this conversation as resolved.
Show resolved Hide resolved
children?: (result: SubscriptionResult<TData>) => React.ReactNode;
}

Expand All @@ -55,6 +56,7 @@ class Subscription<TData = any, TVariables = any> extends React.Component<
variables: PropTypes.object,
children: PropTypes.func,
onSubscriptionData: PropTypes.func,
onSubscriptionComplete: PropTypes.func,
shouldResubscribe: PropTypes.oneOfType([PropTypes.func, PropTypes.bool]),
};

Expand Down Expand Up @@ -136,6 +138,7 @@ class Subscription<TData = any, TVariables = any> extends React.Component<
this.querySubscription = this.queryObservable!.subscribe({
next: this.updateCurrentData,
error: this.updateError,
complete: this.completeSubscription
});
};

Expand Down Expand Up @@ -165,6 +168,15 @@ class Subscription<TData = any, TVariables = any> extends React.Component<
});
};

private completeSubscription = () => {
const {
client,
hwillson marked this conversation as resolved.
Show resolved Hide resolved
props: { onSubscriptionComplete },
} = this;
if (onSubscriptionComplete) onSubscriptionComplete();
this.endSubscription();
};

private endSubscription = () => {
if (this.querySubscription) {
this.querySubscription.unsubscribe();
Expand Down