galactic-bot/structure/client/TransactionHandler.js
Navy d6a0f99601 A lot of shit
Co-authored-by: nolan <noooolaaaan@gmail.com>
2020-04-17 18:23:13 +03:00

45 lines
1.0 KiB
JavaScript

const Collection = require('../../util/Collection.js');
class TransactionHandler {
constructor(client) {
this.client = client;
this.transactions = new Collection();
}
_receive(message) {
if(!message.transactionID) return undefined;
const transaction = this.transactions.get(message.transactionID);
if(!transaction) return undefined;
if(message.error) transaction.reject(message.message);
else transaction.resolve(message.result);
this.transactions.delete(message.transactionID);
}
_send(message, options = {}) {
return new Promise((resolve, reject) => {
const id = this.transactionID;
this.transactions.set(id, { id, resolve, reject } );
process.send({ _storage: true, transactionID: id, ...message, ...options });
});
}
get transactionID() {
return `${[ this.client.shard.ids ]}-${Date.now().toString(36)}`;
}
}
module.exports = TransactionHandler;