optionally use bluebird

This commit is contained in:
AJ ONeal 2015-07-31 15:06:29 -06:00
parent 55a9a65da7
commit ee68b21d91
3 changed files with 27 additions and 9 deletions

View File

@ -2,6 +2,12 @@
/*global Promise*/
var PromiseA = Promise;
try {
PromiseA = require('bluebird').Promise;
} catch(e) {
console.warn("For better Promise support please use bluebird");
}
// TODO iterate over the prototype
// translate request / response
var sqlite3real = require('sqlite3');
@ -25,7 +31,7 @@ function startServer(opts) {
}
function getConnection(opts) {
return new Promise(function (resolve) {
return new PromiseA(function (resolve) {
//setTimeout(function () {
var WebSocket = require('ws');
var ws = new WebSocket('ws+unix:' + opts.sock);
@ -105,7 +111,7 @@ function create(opts) {
var messages = [];
function init(opts) {
return new Promise(function (resolve) {
return new PromiseA(function (resolve, reject) {
var id = Math.random();
ws.send(JSON.stringify({

View File

@ -1,12 +1,18 @@
'use strict';
/*global Promise*/
var PromiseA = Promise;
try {
PromiseA = require('bluebird').Promise;
} catch(e) {
console.warn("For better Promise support please use bluebird");
}
var wsses = {};
function createApp(server, options) {
if (wsses[options.filename]) {
return Promise.resolve(wsses[options.filename]);
return PromiseA.resolve(wsses[options.filename]);
}
return require('./wrapper').create(options).then(function (db) {
@ -119,7 +125,7 @@ function create(options) {
var fs = require('fs');
var ps = [];
ps.push(new Promise(function (resolve) {
ps.push(new PromiseA(function (resolve) {
fs.unlink(options.sock, function () {
// ignore error when socket doesn't exist
@ -132,7 +138,7 @@ function create(options) {
return { masterClient: app.masterClient };
}));
return Promise.all(ps).then(function (results) {
return PromiseA.all(ps).then(function (results) {
return results[1];
});
}

View File

@ -1,6 +1,12 @@
'use strict';
/*global Promise*/
var PromiseA = Promise;
try {
PromiseA = require('bluebird').Promise;
} catch(e) {
console.warn("For better Promise support please use bluebird");
}
var sqlite3 = require('sqlite3');
var dbs = {};
@ -35,7 +41,7 @@ function create(opts) {
var key = newOpts.key || opts.key;
var bits = newOpts.bits || opts.bits;
return new Promise(function (resolve, reject) {
return new PromiseA(function (resolve, reject) {
console.log('OPTS', opts);
console.log('BITS', bits);
if (db._initialized) {
@ -62,13 +68,13 @@ function create(opts) {
}
// TODO db.run(sql, function () { resolve() });
setup.push(new Promise(function (resolve, reject) {
setup.push(new PromiseA(function (resolve, reject) {
db.run("PRAGMA KEY = \"x'" + sanitize(key) + "'\"", [], function (err) {
if (err) { reject(err); return; }
resolve(this);
});
}));
setup.push(new Promise(function (resolve, reject) {
setup.push(new PromiseA(function (resolve, reject) {
//process.nextTick(function () {
db.run("PRAGMA CIPHER = 'aes-" + sanitize(bits) + "-cbc'", [], function (err) {
if (err) { reject(err); return; }
@ -77,7 +83,7 @@ function create(opts) {
//});
}));
Promise.all(setup).then(function () {
PromiseA.all(setup).then(function () {
// restore original functions
resolve(db);
}, reject);