From ee68b21d911aee76e4060aa2a5b306ec1bbc7e01 Mon Sep 17 00:00:00 2001 From: AJ ONeal Date: Fri, 31 Jul 2015 15:06:29 -0600 Subject: [PATCH] optionally use bluebird --- client.js | 10 ++++++++-- server.js | 12 +++++++++--- wrapper.js | 14 ++++++++++---- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/client.js b/client.js index d29fbb0..5d03375 100644 --- a/client.js +++ b/client.js @@ -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({ diff --git a/server.js b/server.js index eec7d4d..70aeb7f 100644 --- a/server.js +++ b/server.js @@ -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]; }); } diff --git a/wrapper.js b/wrapper.js index 7c10144..5ab33b8 100644 --- a/wrapper.js +++ b/wrapper.js @@ -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);