2020-04-17 17:23:13 +02:00
|
|
|
const Collection = require('../../util/Collection.js');
|
2020-04-14 17:05:56 +02:00
|
|
|
|
|
|
|
class TransactionHandler {
|
|
|
|
|
|
|
|
constructor(client) {
|
|
|
|
|
|
|
|
this.client = client;
|
|
|
|
this.transactions = new Collection();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
_receive(message) {
|
|
|
|
|
2020-04-14 21:01:32 +02:00
|
|
|
if(!message.transactionID) return undefined;
|
2020-04-14 17:05:56 +02:00
|
|
|
const transaction = this.transactions.get(message.transactionID);
|
|
|
|
|
2020-04-14 21:01:32 +02:00
|
|
|
if(!transaction) return undefined;
|
|
|
|
|
2020-04-14 17:05:56 +02:00
|
|
|
if(message.error) transaction.reject(message.message);
|
|
|
|
else transaction.resolve(message.result);
|
|
|
|
|
2020-04-14 21:01:32 +02:00
|
|
|
this.transactions.delete(message.transactionID);
|
|
|
|
|
2020-04-14 17:05:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
_send(message, options = {}) {
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
2020-04-16 14:37:04 +02:00
|
|
|
const id = this.transactionID;
|
|
|
|
this.transactions.set(id, { id, resolve, reject } );
|
2020-04-14 17:05:56 +02:00
|
|
|
|
2020-04-16 14:37:04 +02:00
|
|
|
process.send({ _storage: true, transactionID: id, ...message, ...options });
|
2020-04-14 17:05:56 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
get transactionID() {
|
|
|
|
return `${[ this.client.shard.ids ]}-${Date.now().toString(36)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TransactionHandler;
|