Remove callback support and global Gamedig.debug option

This commit is contained in:
mmorrison 2019-01-12 22:38:49 -06:00
parent fdc08b5c09
commit dfa5c95efc
5 changed files with 28 additions and 46 deletions

View file

@ -1,48 +1,23 @@
const QueryRunner = require('./QueryRunner'),
GlobalUdpSocket = require('./GlobalUdpSocket');
const QueryRunner = require('./QueryRunner');
let singleton = null;
class Gamedig {
constructor() {
this.udpSocket = new GlobalUdpSocket();
this.queryRunner = new QueryRunner(this.udpSocket);
this._debug = false;
}
setDebug(on) {
this.udpSocket.debug = on;
this._debug = on;
this.queryRunner.debug = on;
this.queryRunner = new QueryRunner();
}
async query(userOptions) {
userOptions.debug |= this._debug;
return await this.queryRunner.run(userOptions);
}
static getInstance() {
if (!singleton) {
singleton = new Gamedig();
}
if (!singleton) singleton = new Gamedig();
return singleton;
}
static query(userOptions, callback) {
const promise = Gamedig.getInstance().query(userOptions);
if (callback && callback instanceof Function) {
if (callback.length === 2) {
promise
.then((state) => callback(null, state))
.catch((error) => callback(error));
} else if (callback.length === 1) {
promise
.then((state) => callback(state))
.catch((error) => callback({error: error}));
}
}
return promise;
static async query(...args) {
return await Gamedig.getInstance().query(...args);
}
}
Object.defineProperty(Gamedig, "debug", { set: on => Gamedig.getInstance().setDebug(on) });
module.exports = Gamedig;