Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved streamOrders #687 #780

Merged
merged 5 commits into from
Jan 15, 2019
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions lib/cli/commands/subscribeorders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,58 @@ export const builder = {
};

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

let xud: any;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nit, but this can be XudClient instead of any.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure thing


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', () => {});
moshababo marked this conversation as resolved.
Show resolved Hide resolved
};

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