forked from binary-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: dbot performance issue -- unified websocket connection and moved… (
binary-com#7103) * fix: dbot performance issue -- unified websocket connection and moved apis to app initialization * fix: improved the api_base class added fallback and account change logics * fix: removed clientid logic from dbot app * fix: added mobx reaction to handle accountid change * fix: moved the initialization of the websocket after app initializes * fix: fixed pip_sizes in api_base made the names consistent * fix: code improvisation -- worked on the suggested changes * chore: fixed toggle button conditionals and base class name * fix: hoist self-exclusion checks on initialization * fix: prevent from subscribing multiple time to ticks_history with same symbol * fix: #83948 'This market is closed' repetedly appears even after the bot is stopped * fix: removed redundunt api subscriptions open_proposal_contract, balance, and proposal * fix: profit/loss log is getting printed 2 times - unsubscribed when we stop the bot * fix: added time api request to keep ws alive
- Loading branch information
1 parent
dc3e990
commit 1abb7de
Showing
17 changed files
with
246 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { observer as globalObserver } from '../../utils/observer'; | ||
import { generateDerivApiInstance, getLoginId, getToken } from './appId'; | ||
|
||
class APIBase { | ||
api; | ||
token; | ||
account_id; | ||
pip_sizes = {}; | ||
account_info = {}; | ||
is_running = false; | ||
subscriptions = []; | ||
time_interval = null; | ||
|
||
init(force_update = false) { | ||
if (getLoginId()) { | ||
this.toggleRunButton(true); | ||
if (force_update && this.api) this.api.disconnect(); | ||
this.api = generateDerivApiInstance(); | ||
this.initEventListeners(); | ||
this.authorizeAndSubscribe(); | ||
if (this.time_interval) clearInterval(this.time_interval); | ||
this.time_interval = null; | ||
this.getTime(); | ||
} | ||
} | ||
|
||
initEventListeners() { | ||
if (window) { | ||
window.addEventListener('online', this.reconnectIfNotConnected); | ||
window.addEventListener('focus', this.reconnectIfNotConnected); | ||
} | ||
} | ||
|
||
createNewInstance(account_id) { | ||
if (this.account_id !== account_id) { | ||
this.init(true); | ||
} | ||
} | ||
|
||
reconnectIfNotConnected = () => { | ||
// eslint-disable-next-line no-console | ||
console.log('connection state: ', this.api.connection.readyState); | ||
if (this.api.connection.readyState !== 1) { | ||
// eslint-disable-next-line no-console | ||
console.log('Info: Connection to the server was closed, trying to reconnect.'); | ||
this.init(); | ||
} | ||
}; | ||
|
||
authorizeAndSubscribe() { | ||
const { token, account_id } = getToken(); | ||
if (token) { | ||
this.token = token; | ||
this.account_id = account_id; | ||
this.getActiveSymbols(); | ||
this.api | ||
.authorize(this.token) | ||
.then(({ authorize }) => { | ||
this.subscribe(); | ||
this.account_info = authorize; | ||
}) | ||
.catch(e => { | ||
globalObserver.emit('Error', e); | ||
}); | ||
} | ||
} | ||
|
||
subscribe() { | ||
this.api.send({ balance: 1, subscribe: 1 }).catch(e => { | ||
globalObserver.emit('Error', e); | ||
}); | ||
this.api.send({ transaction: 1, subscribe: 1 }).catch(e => { | ||
globalObserver.emit('Error', e); | ||
}); | ||
} | ||
|
||
getActiveSymbols = async () => { | ||
const { active_symbols = [] } = await this.api.send({ active_symbols: 'brief' }).catch(e => { | ||
globalObserver.emit('Error', e); | ||
}); | ||
const pip_sizes = {}; | ||
active_symbols.forEach(({ symbol, pip }) => { | ||
pip_sizes[symbol] = +(+pip).toExponential().substring(3); | ||
}); | ||
this.pip_sizes = pip_sizes; | ||
this.toggleRunButton(false); | ||
}; | ||
|
||
toggleRunButton = toggle => { | ||
const run_button = document.querySelector('#db-animation__run-button'); | ||
if (!run_button) return; | ||
run_button.disabled = toggle; | ||
}; | ||
|
||
setIsRunning(toggle = false) { | ||
this.is_running = toggle; | ||
} | ||
|
||
pushSubscription(subscription) { | ||
this.subscriptions.push(subscription); | ||
} | ||
|
||
clearSubscriptions() { | ||
this.subscriptions.forEach(s => s.unsubscribe()); | ||
this.subscriptions = []; | ||
} | ||
|
||
getTime() { | ||
if (!this.time_interval) { | ||
this.time_interval = setInterval(() => { | ||
this.api.send({ time: 1 }); | ||
}, 30000); | ||
} | ||
} | ||
} | ||
|
||
export const api_base = new APIBase(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.