Skip to content

Commit

Permalink
fix(currency): shared rates to be able to exchange on workers (#2268)
Browse files Browse the repository at this point in the history
Fixes #2229
  • Loading branch information
sogehige committed Jun 18, 2019
1 parent 796af5a commit 43f4a2b
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 26 deletions.
9 changes: 4 additions & 5 deletions src/bot/currency.ts
Expand Up @@ -10,7 +10,7 @@ import { isMainThread } from 'worker_threads';

import Core from './_interface';
import constants from './constants';
import { settings, ui } from './decorators';
import { settings, ui, shared } from './decorators';

class Currency extends Core {
@settings('currency')
Expand All @@ -20,7 +20,9 @@ class Currency extends Core {
})
public mainCurrency: 'USD' | 'AUD' | 'BGN' | 'BRL' | 'CAD' | 'CHF' | 'CNY' | 'CZK' | 'DKK' | 'EUR' | 'GBP' | 'HKD' | 'HRK' | 'HUF' | 'IDR' | 'ILS' | 'INR' | 'ISK' | 'JPY' | 'KRW' | 'MXN' | 'MYR' | 'NOK' | 'NZD' | 'PHP' | 'PLN' | 'RON' | 'RUB' | 'SEK' | 'SGD' | 'THB' | 'TRY' | 'ZAR' = 'EUR';

@shared()
public rates: { [x: string]: number } = {};

public timeouts: any = {};
public base: string = 'CZK';

Expand Down Expand Up @@ -73,10 +75,7 @@ class Currency extends Core {
linenum++;
continue;
}
line = line.split('|');
const count = line[2];
const code = line[3];
const rate = line[4];
const [,, count, code, rate] = line.split('|');
this.rates[code] = Number((rate.replace(',', '.') / count).toFixed(3));
}
global.log.info(chalk.yellow('CURRENCY:') + ' fetched rates');
Expand Down
21 changes: 0 additions & 21 deletions src/bot/decorators.ts
Expand Up @@ -102,28 +102,7 @@ export function shared() {
try {
const self = type === 'core' ? global[name] : global[type][name];
const defaultValue = self[key];

VariableWatcher.add(`${type}.${name}.${key}`, defaultValue);

Object.defineProperty(self, key, {
get: () => {
return _.get(self, '_shared.' + key, defaultValue);
},
set: (value: any) => {
if (!_.isEqual(_.get(self, '_shared.' + key, defaultValue), value)) {
_.set(self, '_shared.' + key, value);
// update variable in all workers (only RAM)
const proc = {
type: 'interface',
system: type,
class: name,
path: '_shared.' + key,
value,
};
global.workers.sendToAll(proc);
}
},
});
} catch (e) {
console.log(e);
}
Expand Down

0 comments on commit 43f4a2b

Please sign in to comment.