Skip to content

Commit

Permalink
feat(cli): improved streamorders (#780)
Browse files Browse the repository at this point in the history
This commit changes the behavior of the `streamorders` cli call:

1. Allow it to start before xud. `streamorders` will wait for xud to
start and not issue an error.
2. Print a line upon connection.
3. Upon termination of xud or any other RPC error - reconnect to xud
and wait if xud is not available.
4. Upon connection to xud - print existing orders.

Closes #687.
  • Loading branch information
rsercano authored and sangaman committed Jan 15, 2019
1 parent 8b914ac commit 6084043
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions lib/cli/commands/subscribeorders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { loadXudClient } from '../command';
import { Arguments } from 'yargs';
import * as xudrpc from '../../proto/xudrpc_pb';
import { XudClient } from '../../proto/xudrpc_grpc_pb';

export const command = 'streamorders [existing]';

Expand All @@ -15,20 +16,58 @@ export const builder = {
};

export const handler = (argv: Arguments) => {
ensureConnection(argv, true);
};

let xud: XudClient;

const ensureConnection = (argv: Arguments, printError?: boolean) => {
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);
} else {
console.log('Successfully connected, subscribing for orders');
subscribeOrders(argv);
}
});
};

const subscribeOrders = (argv: Arguments) => {
const addedOrdersRequest = new xudrpc.SubscribeAddedOrdersRequest();
addedOrdersRequest.setExisting(argv.existing);
const addedOrdersSubscription = loadXudClient(argv).subscribeAddedOrders(addedOrdersRequest);
addedOrdersSubscription.on('data', (order: xudrpc.Order) => {
console.log(`Order added: ${JSON.stringify(order.toObject())}`);
});

// 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('error', (err: Error) => {
console.log(`Unexpected error occured: ${JSON.stringify(err)}, trying to reconnect`);
ensureConnection(argv);
});

const removedOrdersSubscription = loadXudClient(argv).subscribeRemovedOrders(new xudrpc.SubscribeRemovedOrdersRequest());
removedOrdersSubscription.on('data', (orderRemoval: xudrpc.OrderRemoval) => {
console.log(`Order removed: ${JSON.stringify(orderRemoval.toObject())}`);
});

// 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 caught above.
swapsSubscription.on('error', () => {});
};

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

0 comments on commit 6084043

Please sign in to comment.