Skip to content
This repository was archived by the owner on Feb 22, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 3 additions & 1 deletion src/botPage/bot/Interpreter.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,10 @@ export default class Interpreter {
}
terminateSession() {
this.$scope.api.disconnect();
globalObserver.emit('bot.stop');
this.stopped = true;

globalObserver.emit('bot.stop');
globalObserver.setState({ isRunning: false });
}
stop() {
if (this.bot.tradeEngine.isSold === false && !this.isErrorTriggered) {
Expand Down
1 change: 1 addition & 0 deletions src/botPage/bot/TradeEngine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ export default class TradeEngine extends Balance(Purchase(Sell(OpenContract(Prop
}

globalObserver.emit('bot.running');
globalObserver.setState({ isRunning: true });

this.tradeOptions = expectTradeOptions(tradeOptions);

Expand Down
61 changes: 46 additions & 15 deletions src/botPage/view/View.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,11 +511,19 @@ export default class View {
});

const startBot = limitations => {
const $runButtons = $('#runButton, #summaryRunButton');
const $stopButtons = $('#stopButton, #summaryStopButton');
$stopButtons.show();
$runButtons.hide();
$runButtons.prop('disabled', true);
const elRunButtons = document.querySelectorAll('#runButton, #summaryRunButton');
const elStopButtons = document.querySelectorAll('#stopButton, #summaryStopButton');

elRunButtons.forEach(el => {
const elRunButton = el;
elRunButton.style.display = 'none';
elRunButton.setAttributeNode(document.createAttribute('disabled'));
});
elStopButtons.forEach(el => {
const elStopButton = el;
elStopButton.style.display = 'inline-block';
});

globalObserver.emit('summary.disable_clear');
showSummary();
this.blockly.run(limitations);
Expand Down Expand Up @@ -627,31 +635,54 @@ export default class View {
this.blockly.stop();
}
addEventHandlers() {
const getRunButtonElements = () => document.querySelectorAll('#runButton, #summaryRunButton');
const getStopButtonElements = () => document.querySelectorAll('#stopButton, #summaryStopButton');

window.addEventListener('storage', e => {
window.onbeforeunload = null;
if (e.key === 'activeToken' && !e.newValue) window.location.reload();
if (e.key === 'realityCheckTime') hideRealityCheck();
});

globalObserver.register('Error', error => {
$('#runButton, #summaryRunButton').prop('disabled', false);
getRunButtonElements().forEach(el => {
const elRunButton = el;
elRunButton.removeAttribute('disabled');
});

if (error.error && error.error.error.code === 'InvalidToken') {
removeAllTokens();
updateTokenList();
this.stop();
}
});

globalObserver.register('bot.running', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaron-binary does this globalObserver have something like reaction so it listen to state changes and when ever bot state change to running or not running , disable and enable the buttons once only.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Through the observer you can observer.register('name') to a named event, once that event is emitted (through observer.emit('name')) the callback will be executed. In some cases, the buttons should not be enabled when the bot is not running, e.g. when the user is fetching contracts_for from the API.

getRunButtonElements().forEach(el => {
const elRunButton = el;
elRunButton.style.display = 'none';
elRunButton.setAttributeNode(document.createAttribute('disabled'));
});
getStopButtonElements().forEach(el => {
const elStopButton = el;
elStopButton.style.display = 'inline-block';
elStopButton.removeAttribute('disabled');
});
});

globalObserver.register('bot.stop', () => {
const $runButtons = $('#runButton, #summaryRunButton');
const $stopButtons = $('#stopButton, #summaryStopButton');
if ($runButtons.is(':visible') || $stopButtons.is(':visible')) {
$runButtons.show();
$stopButtons.hide();

$stopButtons.prop('disabled', false);
$runButtons.prop('disabled', false);
}
// Enable run button, this event is emitted after the interpreter
// killed the API connection.
getStopButtonElements().forEach(el => {
const elStopButton = el;
elStopButton.style.display = null;
elStopButton.removeAttribute('disabled');
});
getRunButtonElements().forEach(el => {
const elRunButton = el;
elRunButton.style.display = null;
elRunButton.removeAttribute('disabled');
});
});

globalObserver.register('bot.info', info => {
Expand Down
19 changes: 17 additions & 2 deletions src/botPage/view/blockly/blocks/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,21 @@ export const getPredictionForContracts = (contracts, selectedContractType) => {
return predictionRange;
};

export const disableRunButton = isDisabled => {
$('#runButton, #summaryRunButton').attr('disabled', isDisabled);
export const disableRunButton = shouldDisable => {
const elRunButtons = document.querySelectorAll('#runButton, #summaryRunButton');
const isRunning = globalObserver.getState('isRunning');

elRunButtons.forEach(elRunButton => {
if (isRunning) {
if (shouldDisable) {
elRunButton.setAttributeNode(document.createAttribute('disabled'));
} else {
// Do not enable. The bot is running.
}
} else if (shouldDisable) {
elRunButton.setAttributeNode(document.createAttribute('disabled'));
} else {
elRunButton.removeAttribute('disabled');
}
});
};
12 changes: 11 additions & 1 deletion src/botPage/view/blockly/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,17 @@ while(true) {
}
stop(stopBeforeStart) {
if (!stopBeforeStart) {
$('#stopButton, #summaryStopButton').prop('disabled', true);
const elRunButtons = document.querySelectorAll('#runButton, #summaryRunButton');
const elStopButtons = document.querySelectorAll('#stopButton, #summaryStopButton');

elRunButtons.forEach(el => {
const elRunButton = el;
elRunButton.style.display = 'initial';
});
elStopButtons.forEach(el => {
const elStopButton = el;
elStopButton.style.display = null;
});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aaron-binary you copy this code almost 4 times. can it all goes in some "setBotButtonsVisibility` or st like that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Each of the blocks does different things, e.g. in one case the block will just be hidden, in another, the block will be hidden and disabled, and in the other, the block is just disabled.

}
if (this.interpreter) {
this.interpreter.stop();
Expand Down
7 changes: 7 additions & 0 deletions src/common/utils/observer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Map, List } from 'immutable';
export default class Observer {
constructor() {
this.eam = new Map(); // event action map
this.state = {};
}
register(event, _action, once, unregisterIfError, unregisterAllBefore) {
if (unregisterAllBefore) {
Expand Down Expand Up @@ -53,6 +54,12 @@ export default class Observer {
this.eam.get(event).forEach(action => action.action(data));
}
}
setState(state = {}) {
this.state = Object.assign({}, this.state, state);
}
getState(key) {
return this.state[key];
}
}

export const observer = new Observer();
2 changes: 1 addition & 1 deletion static/css/bot.scss
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ body {
background: black;
}

#stopButton {
#stopButton, #summaryStopButton {
display: none;
}

Expand Down