From e99010f363fc7cbe7fd547d3ca5b32ea083c44e6 Mon Sep 17 00:00:00 2001 From: Ivan Tivonenko Date: Wed, 7 Oct 2015 00:40:55 +0300 Subject: [PATCH] Add Remote.closeCurrentPathFind function, so current pathfind can be properly closed, so new can be created without adding to queue --- src/core/remote.js | 17 +++++++++++++++++ test/remote-test.js | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/core/remote.js b/src/core/remote.js index 6df23917..a44b48e0 100644 --- a/src/core/remote.js +++ b/src/core/remote.js @@ -1813,6 +1813,19 @@ Remote.prototype.findAccount = function(accountID) { return account ? account : this.addAccount(accountID); }; +/** + * Closes current pathfind, if there is one. + * After that new pathfind can be created, without adding to queue. + * + * @return {void} - + */ +Remote.prototype.closeCurrentPathFind = function() { + if (this._cur_path_find !== null) { + this._cur_path_find.close(); + this._cur_path_find = null; + } +}; + /** * Create a pathfind * @@ -1822,6 +1835,10 @@ Remote.prototype.findAccount = function(accountID) { */ Remote.prototype.createPathFind = function(options, callback) { if (this._cur_path_find !== null) { + if (callback === undefined) { + throw new Error('Only one streaming pathfind ' + + 'request at a time is supported'); + } this._queued_path_finds.push({options, callback}); return null; } diff --git a/test/remote-test.js b/test/remote-test.js index 917b6f47..925b72a8 100644 --- a/test/remote-test.js +++ b/test/remote-test.js @@ -1927,6 +1927,30 @@ describe('Remote', function() { pathfind.on('update', message => console.log(message)); }); + it('createPathFind - throw error without callback if already running', function() { + const servers = [ + makeServer('wss://localhost:5006'), + makeServer('wss://localhost:5007') + ]; + + remote._servers = servers; + + const pathfindParam = { + src_account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54', + dst_account: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6', + dst_amount: '1/USD/rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54', + src_currencies: [{ + currency: 'BTC', issuer: 'rwxBjBC9fPzyQ9GgPZw6YYLNeRTSx5c2W6' + }] + }; + remote.createPathFind(pathfindParam); + remote.createPathFind(pathfindParam, () => {}); + assert.throws( + function() { + remote.createPathFind(pathfindParam); + }, Error); + }); + it('Construct path_find create request', function() { const request = remote.requestPathFindCreate({ source_account: 'rGr9PjmVe7MqEXTSbd3njhgJc2s5vpHV54',