Skip to content

Commit 9b43401

Browse files
committed
swaps: remove api polling for pending swaps
1 parent 55dc5ad commit 9b43401

File tree

4 files changed

+0
-103
lines changed

4 files changed

+0
-103
lines changed

app/src/__stories__/HistoryPage.stories.tsx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,13 @@ const updateSwapsStatus = (swaps: Swap[]) => {
2323

2424
export const Default = () => {
2525
const store = useStore();
26-
store.swapStore.stopAutoPolling();
2726
updateSwapsStatus(store.swapStore.sortedSwaps);
2827
return <HistoryPage />;
2928
};
3029

3130
export const InsideLayout = () => {
3231
const store = useStore();
3332
store.uiStore.page = 'history';
34-
store.swapStore.stopAutoPolling();
3533
updateSwapsStatus(store.swapStore.sortedSwaps);
3634
return (
3735
<Layout>

app/src/__stories__/ProcessingSwaps.stories.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@ const delay = (timeout: number) =>
6666

6767
export const AllSwapStates = () => {
6868
const store = useStore();
69-
store.swapStore.stopAutoPolling();
7069
store.swapStore.swaps = createSwaps();
7170
return <ProcessingSwaps />;
7271
};
7372

7473
export const LoopInProgress = () => {
7574
const store = useStore();
76-
store.swapStore.stopAutoPolling();
7775
const swap = mockSwap(LOOP_IN, INITIATED);
7876
store.swapStore.swaps = observable.map({ [swap.id]: swap });
7977

@@ -98,7 +96,6 @@ export const LoopInProgress = () => {
9896

9997
export const LoopOutProgress = () => {
10098
const store = useStore();
101-
store.swapStore.stopAutoPolling();
10299
const swap = mockSwap(LOOP_OUT, INITIATED);
103100
store.swapStore.swaps = observable.map({ [swap.id]: swap });
104101

app/src/__tests__/store/swapStore.spec.ts

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -75,58 +75,4 @@ describe('SwapStore', () => {
7575
swap.type = type;
7676
expect(swap.typeName).toEqual(label);
7777
});
78-
79-
it('should poll for swap updates', async () => {
80-
await store.fetchSwaps();
81-
const swap = store.sortedSwaps[0];
82-
// create a pending swap to trigger auto-polling
83-
swap.state = LOOP.SwapState.INITIATED;
84-
expect(store.pendingSwaps).toHaveLength(1);
85-
// wait for polling to start
86-
await waitFor(() => {
87-
expect(store.pollingInterval).toBeDefined();
88-
});
89-
// change the swap to complete
90-
swap.state = LOOP.SwapState.SUCCESS;
91-
expect(store.pendingSwaps).toHaveLength(0);
92-
// confirm polling has stopped
93-
await waitFor(() => {
94-
expect(store.pollingInterval).toBeUndefined();
95-
});
96-
});
97-
98-
it('should handle startPolling when polling is already running', () => {
99-
expect(store.pollingInterval).toBeUndefined();
100-
store.startPolling();
101-
expect(store.pollingInterval).toBeDefined();
102-
store.startPolling();
103-
expect(store.pollingInterval).toBeDefined();
104-
});
105-
106-
it('should handle stopPolling when polling is already stopped', () => {
107-
expect(store.pollingInterval).toBeUndefined();
108-
store.stopPolling();
109-
expect(store.pollingInterval).toBeUndefined();
110-
});
111-
112-
it('should stop polling if there is an error fetching swaps', async () => {
113-
await store.fetchSwaps();
114-
const swap = store.sortedSwaps[0];
115-
// create a pending swap to trigger auto-polling
116-
swap.state = LOOP.SwapState.INITIATED;
117-
expect(store.pendingSwaps).toHaveLength(1);
118-
// wait for polling to start
119-
await waitFor(() => {
120-
expect(store.pollingInterval).toBeDefined();
121-
});
122-
// induce a failure when fetching swaps
123-
grpcMock.unary.mockImplementationOnce(desc => {
124-
if (desc.methodName === 'ListSwaps') throw new Error('test-err');
125-
return undefined as any;
126-
});
127-
// confirm polling has stopped
128-
await waitFor(() => {
129-
expect(store.pollingInterval).toBeUndefined();
130-
});
131-
});
13278
});

app/src/store/stores/swapStore.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
import {
22
action,
33
computed,
4-
IReactionDisposer,
54
observable,
65
ObservableMap,
7-
reaction,
86
runInAction,
97
toJS,
108
values,
119
} from 'mobx';
1210
import { SwapStatus } from 'types/generated/loop_pb';
13-
import { IS_PROD, IS_TEST } from 'config';
1411
import { Store } from 'store';
1512
import { Swap } from '../models';
1613

1714
export default class SwapStore {
1815
private _store: Store;
19-
/** a reference to the polling timer, needed to stop polling */
20-
pollingInterval?: NodeJS.Timeout;
21-
/** the mobx disposer func to cancel automatic polling */
22-
stopAutoPolling: IReactionDisposer;
23-
2416
/** the collection of swaps */
2517
@observable swaps: ObservableMap<string, Swap> = observable.map();
2618

@@ -29,21 +21,6 @@ export default class SwapStore {
2921

3022
constructor(store: Store) {
3123
this._store = store;
32-
33-
// automatically start & stop polling for swaps if there are any pending
34-
this.stopAutoPolling = reaction(
35-
() => this.pendingSwaps.length,
36-
(length: number) => {
37-
if (length > 0) {
38-
this.startPolling();
39-
} else {
40-
this.stopPolling();
41-
// also update our channels and balances when the loop is complete
42-
this._store.channelStore.fetchChannels();
43-
this._store.nodeStore.fetchBalances();
44-
}
45-
},
46-
);
4724
}
4825

4926
/** swaps sorted by created date descending */
@@ -103,7 +80,6 @@ export default class SwapStore {
10380
});
10481
} catch (error) {
10582
this._store.uiStore.handleError(error, 'Unable to fetch Swaps');
106-
if (this.pollingInterval) this.stopPolling();
10783
}
10884
}
10985

@@ -127,26 +103,6 @@ export default class SwapStore {
127103
this._store.channelStore.fetchChannels();
128104
}
129105

130-
@action.bound
131-
startPolling() {
132-
if (this.pollingInterval) this.stopPolling();
133-
this._store.log.info('start polling for swap updates');
134-
const ms = IS_PROD ? 60 * 1000 : IS_TEST ? 100 : 1000;
135-
this.pollingInterval = setInterval(this.fetchSwaps, ms);
136-
}
137-
138-
@action.bound
139-
stopPolling() {
140-
this._store.log.info('stop polling for swap updates');
141-
if (this.pollingInterval) {
142-
clearInterval(this.pollingInterval);
143-
this.pollingInterval = undefined;
144-
this._store.log.info('polling stopped');
145-
} else {
146-
this._store.log.info('polling was already stopped');
147-
}
148-
}
149-
150106
/** exports the sorted list of swaps to CSV file */
151107
@action.bound
152108
exportSwaps() {

0 commit comments

Comments
 (0)