Skip to content

Commit

Permalink
requested changes ExchangeUnion#687
Browse files Browse the repository at this point in the history
  • Loading branch information
sercan committed Jan 8, 2019
1 parent 94bbb4a commit 44880d9
Showing 1 changed file with 8 additions and 6 deletions.
14 changes: 8 additions & 6 deletions lib/cli/commands/subscribeorders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ export const handler = (argv: Arguments) => {
ensureConnection(argv, true);
};

let xud: any;

const ensureConnection = (argv: Arguments, printError?: boolean) => {
loadXudClient(argv).waitForReady(Number.POSITIVE_INFINITY, (error: Error | null) => {
if (!xud) xud = loadXudClient(argv);
xud.waitForReady(Number.POSITIVE_INFINITY, (error: Error | null) => {
if (error) {
if (printError) console.error(`${error.name}: ${error.message}`);
setTimeout(ensureConnection.bind(undefined, argv), 3000);
Expand All @@ -41,9 +44,8 @@ const subscribeOrders = (argv: Arguments) => {
// adding end, close, error events only once,
// since they'll be thrown for three of subscriptions in the corresponding cases, catching once is enough.
addedOrdersSubscription.on('end', reconnect.bind(undefined, argv));
addedOrdersSubscription.on('close', reconnect.bind(undefined, argv));
addedOrdersSubscription.on('error', (err: Error) => {
console.log(`Unexpected error occured: ${JSON.stringify(err)}, retrying to connect`);
console.log(`Unexpected error occured: ${JSON.stringify(err)}, trying to reconnect`);
ensureConnection(argv);
});

Expand All @@ -52,19 +54,19 @@ const subscribeOrders = (argv: Arguments) => {
console.log(`Order removed: ${JSON.stringify(orderRemoval.toObject())}`);
});

// prevent exiting and do nothing, it's already cached above.
// prevent exiting and do nothing, it's already caught above.
removedOrdersSubscription.on('error', () => {});

const swapsSubscription = loadXudClient(argv).subscribeSwaps(new xudrpc.SubscribeSwapsRequest());
swapsSubscription.on('data', (swapResult: xudrpc.SwapResult) => {
console.log(`Order swapped: ${JSON.stringify(swapResult.toObject())}`);
});

// prevent exiting and do nothing, it's already cached above.
// prevent exiting and do nothing, it's already caught above.
swapsSubscription.on('error', () => {});
};

const reconnect = (argv: Arguments) => {
console.log('Stream has closed unexpectedly, trying to reconnect');
console.log('Stream has closed, trying to reconnect');
ensureConnection(argv, false);
};

0 comments on commit 44880d9

Please sign in to comment.