Add support for promises

This commit is contained in:
mmorrison 2017-03-14 03:40:02 -05:00
parent 42df8ed96b
commit 7b9fe2161c
4 changed files with 99 additions and 71 deletions

View file

@ -14,18 +14,30 @@ Usage from Node.js
npm install gamedig npm install gamedig
``` ```
Promise:
```javascript ```javascript
var Gamedig = require('gamedig'); var Gamedig = require('gamedig');
Gamedig.query( Gamedig.query({
{
type: 'minecraft', type: 'minecraft',
host: 'mc.example.com' host: 'mc.example.com'
}, }).then((state) => {
function(state) { console.log(state);
if(state.error) console.log("Server is offline"); }).catch((error) => {
console.log("Server is offline");
});
```
or Node.JS Callback:
```javascript
var Gamedig = require('gamedig');
Gamedig.query({
type: 'minecraft',
host: 'mc.example.com'
},
function(e,state) {
if(e) console.log("Server is offline");
else console.log(state); else console.log(state);
} });
);
``` ```
> Is NPM out of date? If you're feeling lucky, you can install the latest code with > Is NPM out of date? If you're feeling lucky, you can install the latest code with
@ -40,14 +52,9 @@ Gamedig.query(
* **port**: (optional) Uses the protocol default if not set * **port**: (optional) Uses the protocol default if not set
* **notes**: (optional) Passed through to output * **notes**: (optional) Passed through to output
###Callback Function ### Return Value
The callback function is "guaranteed" to be called exactly once. The returned state object will contain the following keys:
If an error occurs, the returned object will contain an "error" key, indicating the issue.
If the error key exists, it should be assumed that the game server is offline or unreachable.
Otherwise, the returned object is guaranteed to contain the following keys:
**Stable, always present:** **Stable, always present:**
@ -70,7 +77,7 @@ Some servers may return an additional player count number, which may be present
Games List Games List
--- ---
###Supported ### Supported
<!--- BEGIN GENERATED GAMES --> <!--- BEGIN GENERATED GAMES -->
* 7 Days to Die (7d2d) [[Separate Query Port](#separate-query-port)] * 7 Days to Die (7d2d) [[Separate Query Port](#separate-query-port)]
@ -305,7 +312,7 @@ Games List
<!--- END GENERATED GAMES --> <!--- END GENERATED GAMES -->
###Not supported (yet) ### Not supported (yet)
* rFactor Engine (rfactor): * rFactor Engine (rfactor):
* rFactor * rFactor

View file

@ -22,13 +22,19 @@ for(var key in argv) {
var Gamedig = require('../lib/index'); var Gamedig = require('../lib/index');
if(debug) Gamedig.debug = true; if(debug) Gamedig.debug = true;
Gamedig.isCommandLine = true; Gamedig.isCommandLine = true;
Gamedig.query(
options, Gamedig.query(options)
function(state) { .then((state) => {
if(outputFormat == 'pretty') { if(outputFormat == 'pretty') {
console.log(JSON.stringify(state,null,' ')); console.log(JSON.stringify(state,null,' '));
} else { } else {
console.log(JSON.stringify(state)); console.log(JSON.stringify(state));
} }
})
.catch((error) => {
if(outputFormat == 'pretty') {
console.log(JSON.stringify({error:error},null,' '));
} else {
console.log(JSON.stringify({error:error}));
} }
); });

View file

@ -29,14 +29,18 @@ udpSocket.on('error', function(e) {
Gamedig = { Gamedig = {
query: function(options,callback) { query: function(options,callback) {
if(callback) options.callback = callback; const promise = new Promise((resolve,reject) => {
options.callback = (state) => {
if (state.error) reject(state.error);
else resolve(state);
};
var query; var query;
try { try {
query = TypeResolver.lookup(options.type); query = TypeResolver.lookup(options.type);
} catch(e) { } catch(e) {
process.nextTick(function() { process.nextTick(function() {
callback({error:e.message}); options.callback({error:e.message});
}); });
return; return;
} }
@ -71,8 +75,21 @@ Gamedig = {
process.nextTick(function() { process.nextTick(function() {
query.start(); query.start();
}); });
});
return query; 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;
} }
}; };

View file

@ -11,7 +11,7 @@
], ],
"main": "lib/index.js", "main": "lib/index.js",
"author": "Michael Morrison", "author": "Michael Morrison",
"version": "0.2.25", "version": "0.2.26",
"repository" : { "repository" : {
"type" : "git", "type" : "git",
"url" : "https://github.com/sonicsnes/node-gamedig.git" "url" : "https://github.com/sonicsnes/node-gamedig.git"
@ -19,12 +19,10 @@
"bugs" : { "bugs" : {
"url" : "https://github.com/sonicsnes/node-gamedig/issues" "url" : "https://github.com/sonicsnes/node-gamedig/issues"
}, },
"licenses" : [ "license" : "MIT",
{ "engines" : {
"type" : "MIT", "node" : ">=4.0.0"
"url" : "https://raw.github.com/sonicsnes/node-gamedig/master/LICENSE" },
}
],
"dependencies": { "dependencies": {
"iconv-lite": "~0.4.6", "iconv-lite": "~0.4.6",
"long": "~2.2.3", "long": "~2.2.3",