fix: ensure sub is cancelled if dialog is closed (#1472)

* fix: ensure sub is cancelled if dialog is closed

* fix: pass transaction object in use proposal hooks
This commit is contained in:
Matthew Russell 2022-09-28 12:34:49 -07:00 committed by GitHub
parent 26e3596c5a
commit 8bffc05be2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 31 additions and 13 deletions

View File

@ -6,6 +6,7 @@ import type {
ProposalEvent_busEvents_event_Proposal, ProposalEvent_busEvents_event_Proposal,
} from './__generated__/ProposalEvent'; } from './__generated__/ProposalEvent';
import type { Subscription } from 'zen-observable-ts'; import type { Subscription } from 'zen-observable-ts';
import type { VegaTxState } from '@vegaprotocol/wallet';
export const PROPOSAL_EVENT_SUB = gql` export const PROPOSAL_EVENT_SUB = gql`
subscription ProposalEvent($partyId: ID!) { subscription ProposalEvent($partyId: ID!) {
@ -24,7 +25,7 @@ export const PROPOSAL_EVENT_SUB = gql`
} }
`; `;
export const useProposalEvent = () => { export const useProposalEvent = (transaction: VegaTxState) => {
const client = useApolloClient(); const client = useApolloClient();
const subRef = useRef<Subscription | null>(null); const subRef = useRef<Subscription | null>(null);
@ -66,10 +67,13 @@ export const useProposalEvent = () => {
); );
useEffect(() => { useEffect(() => {
if (!transaction.dialogOpen) {
subRef.current?.unsubscribe();
}
return () => { return () => {
subRef.current?.unsubscribe(); subRef.current?.unsubscribe();
}; };
}, []); }, [transaction.dialogOpen]);
return waitForProposalEvent; return waitForProposalEvent;
}; };

View File

@ -8,9 +8,9 @@ import type { ProposalEvent_busEvents_event_Proposal } from './__generated__/Pro
export const useProposalSubmit = () => { export const useProposalSubmit = () => {
const { keypair } = useVegaWallet(); const { keypair } = useVegaWallet();
const waitForProposalEvent = useProposalEvent();
const { send, transaction, setComplete, Dialog } = useVegaTransaction(); const { send, transaction, setComplete, Dialog } = useVegaTransaction();
const waitForProposalEvent = useProposalEvent(transaction);
const [finalizedProposal, setFinalizedProposal] = const [finalizedProposal, setFinalizedProposal] =
useState<ProposalEvent_busEvents_event_Proposal | null>(null); useState<ProposalEvent_busEvents_event_Proposal | null>(null);

View File

@ -11,7 +11,6 @@ export interface CancelOrderArgs {
export const useOrderCancel = () => { export const useOrderCancel = () => {
const { keypair } = useVegaWallet(); const { keypair } = useVegaWallet();
const waitForOrderEvent = useOrderEvent();
const [cancelledOrder, setCancelledOrder] = const [cancelledOrder, setCancelledOrder] =
useState<OrderEvent_busEvents_event_Order | null>(null); useState<OrderEvent_busEvents_event_Order | null>(null);
@ -24,6 +23,8 @@ export const useOrderCancel = () => {
Dialog, Dialog,
} = useVegaTransaction(); } = useVegaTransaction();
const waitForOrderEvent = useOrderEvent(transaction);
const reset = useCallback(() => { const reset = useCallback(() => {
resetTransaction(); resetTransaction();
setCancelledOrder(null); setCancelledOrder(null);

View File

@ -25,7 +25,7 @@ export const useOrderEdit = (order: OrderWithMarket | null) => {
Dialog, Dialog,
} = useVegaTransaction(); } = useVegaTransaction();
const waitForOrderEvent = useOrderEvent(); const waitForOrderEvent = useOrderEvent(transaction);
const reset = useCallback(() => { const reset = useCallback(() => {
resetTransaction(); resetTransaction();

View File

@ -7,8 +7,9 @@ import type {
OrderEvent_busEvents_event_Order, OrderEvent_busEvents_event_Order,
} from './'; } from './';
import type { Subscription } from 'zen-observable-ts'; import type { Subscription } from 'zen-observable-ts';
import type { VegaTxState } from '@vegaprotocol/wallet';
export const useOrderEvent = () => { export const useOrderEvent = (transaction: VegaTxState) => {
const client = useApolloClient(); const client = useApolloClient();
const subRef = useRef<Subscription | null>(null); const subRef = useRef<Subscription | null>(null);
@ -50,10 +51,14 @@ export const useOrderEvent = () => {
); );
useEffect(() => { useEffect(() => {
if (!transaction.dialogOpen) {
subRef.current?.unsubscribe();
}
return () => { return () => {
subRef.current?.unsubscribe(); subRef.current?.unsubscribe();
}; };
}, []); }, [transaction.dialogOpen]);
return waitForOrderEvent; return waitForOrderEvent;
}; };

View File

@ -96,7 +96,6 @@ export const getOrderDialogIcon = (
export const useOrderSubmit = () => { export const useOrderSubmit = () => {
const { keypair } = useVegaWallet(); const { keypair } = useVegaWallet();
const waitForOrderEvent = useOrderEvent();
const { const {
send, send,
@ -106,6 +105,8 @@ export const useOrderSubmit = () => {
Dialog, Dialog,
} = useVegaTransaction(); } = useVegaTransaction();
const waitForOrderEvent = useOrderEvent(transaction);
const [finalizedOrder, setFinalizedOrder] = const [finalizedOrder, setFinalizedOrder] =
useState<OrderEvent_busEvents_event_Order | null>(null); useState<OrderEvent_busEvents_event_Order | null>(null);

View File

@ -8,7 +8,6 @@ import type { Position } from '../';
export const useClosePosition = () => { export const useClosePosition = () => {
const { keypair } = useVegaWallet(); const { keypair } = useVegaWallet();
const waitForPositionEvent = usePositionEvent();
const { const {
send, send,
@ -17,6 +16,7 @@ export const useClosePosition = () => {
setComplete, setComplete,
Dialog, Dialog,
} = useVegaTransaction(); } = useVegaTransaction();
const waitForPositionEvent = usePositionEvent(transaction);
const reset = useCallback(() => { const reset = useCallback(() => {
resetTransaction(); resetTransaction();

View File

@ -1,7 +1,8 @@
import type { VegaTxState } from '@vegaprotocol/wallet';
import { useCallback } from 'react'; import { useCallback } from 'react';
// this should be replaced by implementation of busEvents listener when it will be available // this should be replaced by implementation of busEvents listener when it will be available
export const usePositionEvent = () => { export const usePositionEvent = (transaction: VegaTxState) => {
const waitForOrderEvent = useCallback( const waitForOrderEvent = useCallback(
(id: string, partyId: string, callback: () => void) => { (id: string, partyId: string, callback: () => void) => {
Promise.resolve().then(() => { Promise.resolve().then(() => {

View File

@ -15,7 +15,6 @@ export interface WithdrawalArgs {
export const useCreateWithdraw = () => { export const useCreateWithdraw = () => {
const waitForWithdrawalApproval = useWithdrawalApproval(); const waitForWithdrawalApproval = useWithdrawalApproval();
const waitForWithdrawal = useWithdrawalEvent();
const [approval, setApproval] = const [approval, setApproval] =
useState<Erc20Approval_erc20WithdrawalApproval | null>(null); useState<Erc20Approval_erc20WithdrawalApproval | null>(null);
const [withdrawal, setWithdrawal] = useState<WithdrawalFields | null>(null); const [withdrawal, setWithdrawal] = useState<WithdrawalFields | null>(null);
@ -27,6 +26,8 @@ export const useCreateWithdraw = () => {
const { transaction, send, setComplete, reset, Dialog } = const { transaction, send, setComplete, reset, Dialog } =
useVegaTransaction(); useVegaTransaction();
const waitForWithdrawal = useWithdrawalEvent(transaction);
const submit = useCallback( const submit = useCallback(
async (withdrawal: WithdrawalArgs) => { async (withdrawal: WithdrawalArgs) => {
if (!keypair) { if (!keypair) {

View File

@ -1,4 +1,5 @@
import { useApolloClient } from '@apollo/client'; import { useApolloClient } from '@apollo/client';
import type { VegaTxState } from '@vegaprotocol/wallet';
import { useCallback, useEffect, useRef } from 'react'; import { useCallback, useEffect, useRef } from 'react';
import type { Subscription } from 'zen-observable-ts'; import type { Subscription } from 'zen-observable-ts';
import { WITHDRAWAL_BUS_EVENT_SUB } from './use-withdrawals'; import { WITHDRAWAL_BUS_EVENT_SUB } from './use-withdrawals';
@ -12,7 +13,8 @@ type WaitForWithdrawalEvent = (
id: string, id: string,
partyId: string partyId: string
) => Promise<WithdrawalEvent_busEvents_event_Withdrawal>; ) => Promise<WithdrawalEvent_busEvents_event_Withdrawal>;
export const useWithdrawalEvent = () => {
export const useWithdrawalEvent = (transaction: VegaTxState) => {
const client = useApolloClient(); const client = useApolloClient();
const subRef = useRef<Subscription | null>(null); const subRef = useRef<Subscription | null>(null);
@ -52,10 +54,13 @@ export const useWithdrawalEvent = () => {
); );
useEffect(() => { useEffect(() => {
if (!transaction.dialogOpen) {
subRef.current?.unsubscribe();
}
return () => { return () => {
subRef.current?.unsubscribe(); subRef.current?.unsubscribe();
}; };
}, []); }, [transaction.dialogOpen]);
return waitForWithdrawalEvent; return waitForWithdrawalEvent;
}; };