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 3 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
36 changes: 36 additions & 0 deletions lib/cli/commands/subscribeorders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,56 @@ export const builder = {
};

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

const ensureConnection = (argv: Arguments, printError?: boolean) => {
loadXudClient(argv).waitForReady(Number.POSITIVE_INFINITY, (error: Error | null) => {
sangaman marked this conversation as resolved.
Show resolved Hide resolved
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('close', reconnect.bind(undefined, argv));
sangaman marked this conversation as resolved.
Show resolved Hide resolved
addedOrdersSubscription.on('error', (err: Error) => {
console.log(`Unexpected error occured: ${JSON.stringify(err)}, retrying to connect`);
sangaman marked this conversation as resolved.
Show resolved Hide resolved
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 cached 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.
sangaman marked this conversation as resolved.
Show resolved Hide resolved
swapsSubscription.on('error', () => {});
moshababo marked this conversation as resolved.
Show resolved Hide resolved
};

const reconnect = (argv: Arguments) => {
console.log('Stream has closed unexpectedly, trying to reconnect');
sangaman marked this conversation as resolved.
Show resolved Hide resolved
ensureConnection(argv, false);
};