Skip to content

Commit

Permalink
sandeep/65842/unifiy-websocket -- using the same websocket instance f…
Browse files Browse the repository at this point in the history
…rom the core … (deriv-com#6555)

* unified websocket -- using the same websocket instance from the core packages

* added reset transaction_subscription_id on close of instance

Co-authored-by: Carol Sachdeva <58209918+carol-binary@users.noreply.github.com>
  • Loading branch information
sandeep-deriv and carolsachdeva authored Sep 27, 2022
1 parent 1b5fd1b commit e2b3d2e
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/* eslint-disable no-unused-expressions */
import { generateDerivApiInstance } from '../appId';
import TicksService from '../ticks_service';
import { WS } from '@deriv/shared';

jasmine.DEFAULT_TIMEOUT_INTERVAL = 20000;

const ticksService = new TicksService(generateDerivApiInstance());
const ticksService = new TicksService(WS.get());

const isTick = t => Number.isInteger(t.epoch) && Number.isFinite(t.quote);

Expand Down
12 changes: 0 additions & 12 deletions packages/bot-skeleton/src/services/api/appId.js

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ export default Engine =>
} = data;

balance_string = getFormattedText(b, currency);

info({ accountID: this.accountInfo.loginid, balance: balance_string });
info({ accountID: this.accountInfo?.loginid, balance: balance_string });
}
});
}
Expand Down
18 changes: 15 additions & 3 deletions packages/bot-skeleton/src/services/tradeEngine/trade/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ export default class TradeEngine extends Balance(Purchase(Sell(OpenContract(Prop
forget_proposal_ids: [],
};
this.store = createStore(rootReducer, applyMiddleware(thunk));
this.api.connection.onclose = () => {
globalObserver.setState({ transaction_subscription_id: null });
};
}

init(...args) {
Expand Down Expand Up @@ -130,19 +133,28 @@ export default class TradeEngine extends Balance(Purchase(Sell(OpenContract(Prop
}, 1500);
}
if (data.msg_type === 'authorize') {
this.accountInfo = data;
const { authorize = {} } = data;
this.accountInfo = authorize;
this.token = token;

// Only subscribe to balance in browser, not for tests.
if (document) {
doUntilDone(() => this.api.send({ balance: 1, subscribe: 1 })).then(r => {
doUntilDone(() => this.api.send({ balance: 1 })).then(r => {
this.balance = Number(r.balance.balance);
resolve();
});
} else {
resolve();
}
doUntilDone(() => this.api.send({ transaction: 1, subscribe: 1 }));
if (!globalObserver.getState('transaction_subscription_id')) {
doUntilDone(() => this.api.send({ transaction: 1, subscribe: 1 }))
.then(({ transaction }) => {
globalObserver.setState({ transaction_subscription_id: transaction.id });
})
.catch(err => {
this.$scope.observer.emit('Error', err);
});
}
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import TicksService from '../../api/ticks_service';
import Observer from '../../../utils/observer';
import { generateDerivApiInstance } from '../../api/appId';
import { WS } from '@deriv/shared';

export const createScope = () => {
const observer = new Observer();
const api = generateDerivApiInstance();
const api = WS.get();
const ticksService = new TicksService(api);
const stopped = false;
return { observer, api, ticksService, stopped };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,6 @@ const Interpreter = () => {
}

function terminateSession() {
const { connection } = $scope.api;
if (connection.readyState === 0) {
connection.addEventListener('open', () => connection.close());
} else if (connection.readyState === 1) {
connection.close();
}

$scope.stopped = true;
$scope.is_error_triggered = false;
globalObserver.emit('bot.stop');
Expand Down
3 changes: 2 additions & 1 deletion packages/bot-web-ui/src/app/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
Toolbar,
NetworkToastPopup,
} from 'Components';
import { LocalStore } from '@deriv/shared';
import { LocalStore, setWebsocket } from '@deriv/shared';
import { MobxContentProvider } from 'Stores/connect';
import RootStore from 'Stores';
import GTM from 'Utils/gtm';
Expand All @@ -31,6 +31,7 @@ const App = ({ passthrough }) => {
const { onMount, onUnmount, showDigitalOptionsMaltainvestError } = app;

React.useEffect(() => {
setWebsocket(WS);
/**
* Inject: External Script Hotjar - for DBot only
*/
Expand Down

0 comments on commit e2b3d2e

Please sign in to comment.