-
Notifications
You must be signed in to change notification settings - Fork 298
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sandeep/fix: dbot-slowness issue v2 (#7345)
* fix: multiplier is not working fine with the dbot slowness fix * fix: please log in issue caused due to performance optimization * Revert "Revert V20230106_0 (#7334)" This reverts commit 69825d3. * fix: multiplier contract purchase * fix: removed multiplier check and new ws creation * chore: clear timeout resolvers on bot close Co-authored-by: balakrishna-binary <bala.krishna@regentmarkets.com> Co-authored-by: Prince <prince@deriv.com>
- Loading branch information
1 parent
415b5f8
commit cc8aee7
Showing
18 changed files
with
270 additions
and
73 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,132 @@ | ||
import { observer as globalObserver } from '../../utils/observer'; | ||
import { doUntilDone } from '../tradeEngine/utils/helpers'; | ||
import { generateDerivApiInstance, getLoginId, getToken } from './appId'; | ||
|
||
class APIBase { | ||
api; | ||
token; | ||
account_id; | ||
pip_sizes = {}; | ||
account_info = {}; | ||
is_running = false; | ||
subscriptions = []; | ||
time_interval = null; | ||
has_activeSymbols = false; | ||
|
||
init(force_update = false) { | ||
if (getLoginId()) { | ||
this.toggleRunButton(true); | ||
if (force_update) this.terminate(); | ||
this.api = generateDerivApiInstance(); | ||
this.initEventListeners(); | ||
this.authorizeAndSubscribe(); | ||
if (this.time_interval) clearInterval(this.time_interval); | ||
this.time_interval = null; | ||
this.getTime(); | ||
} | ||
} | ||
|
||
terminate() { | ||
// eslint-disable-next-line no-console | ||
console.log('connection terminated'); | ||
if (this.api) this.api.disconnect(); | ||
} | ||
|
||
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.api | ||
.authorize(this.token) | ||
.then(({ authorize }) => { | ||
if (this.has_activeSymbols) { | ||
this.toggleRunButton(false); | ||
} else { | ||
this.getActiveSymbols(); | ||
} | ||
this.subscribe(); | ||
this.account_info = authorize; | ||
}) | ||
.catch(e => { | ||
globalObserver.emit('Error', e); | ||
}); | ||
} | ||
} | ||
|
||
subscribe() { | ||
doUntilDone(() => this.api.send({ balance: 1, subscribe: 1 })); | ||
doUntilDone(() => this.api.send({ transaction: 1, subscribe: 1 })); | ||
} | ||
|
||
getActiveSymbols = async () => { | ||
doUntilDone(() => this.api.send({ active_symbols: 'brief' })).then(({ active_symbols = [] }) => { | ||
const pip_sizes = {}; | ||
if (active_symbols.length) this.has_activeSymbols = true; | ||
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 = []; | ||
|
||
// Resetting timeout resolvers | ||
const global_timeouts = globalObserver.getState('global_timeouts') ?? []; | ||
|
||
global_timeouts.forEach((_, i) => { | ||
clearTimeout(i); | ||
}); | ||
} | ||
|
||
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
Oops, something went wrong.