Skip to content

Commit 693c19e

Browse files
committed
C++ promisifyer
1 parent 2e0a0c6 commit 693c19e

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

generate/templates/templates/nodegit.js

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,67 @@
11
var Promise = require("bluebird");
2-
var promisify = require("thenify-all");
32
var rawApi;
43

4+
function promisify(source) {
5+
var destination = {};
6+
if (typeof source === "function") {
7+
return thenify(source);
8+
} else {
9+
Object.keys(source).forEach(function (name) {
10+
if (typeof source[name] === "function") {
11+
destination[name] = thenify(source[name]);
12+
}
13+
});
14+
}
15+
}
16+
17+
function thenify ($$__fn__$$) {
18+
return eval(createWrapper($$__fn__$$.name));
19+
}
20+
21+
function createWrapper (name) {
22+
name = name ? ~[ /*
23+
"abstract", "arguments", "boolean", "break", "byte", "case", "catch",
24+
"char", "class", "const", "continue", "debugger", "while", "with", "yield",
25+
"do", "double", "else", "enum", "eval", "export", "extends", "false",
26+
"final", "finally", "float", "for", "function", "goto", "if", "implements",
27+
"import", "in", "instanceof", "int", "interface", "let", "long", "native",
28+
"new", "null", "package", "private", "protected", "public", "return",
29+
"short", "static", "super", "switch", "synchronized", "this", "throw",
30+
"throws", "transient", "true", "try", "typeof", "var", "void", "volatile",
31+
*/ "default", "delete"
32+
].indexOf(name) ? "$$" + name : name : "";
33+
34+
/* jshint ignore:start */
35+
return "(function " + name + "() {\n"
36+
+ "var self = this\n"
37+
+ "var len = arguments.length\n"
38+
+ "var args = new Array(len + 1)\n"
39+
+ "for (var i = 0; i < len; ++i) args[i] = arguments[i]\n"
40+
+ "var lastIndex = i\n"
41+
+ "return new Promise(function (resolve, reject) {\n"
42+
+ "args[lastIndex] = createCallback(resolve, reject)\n"
43+
+ "$$__fn__$$.apply(self, args)\n"
44+
+ "})\n"
45+
+ "})"
46+
/* jshint ignore:end */
47+
}
48+
49+
/* jshint ignore:start */
50+
function createCallback(resolve, reject) {
51+
return function(err, value) {
52+
if (err) return reject(err)
53+
var length = arguments.length
54+
if (length <= 2) return resolve(value)
55+
var values = new Array(length - 1)
56+
for (var i = 1; i < length; ++i) values[i - 1] = arguments[i]
57+
resolve(values)
58+
}
59+
}
60+
/* jshint ignore:end */
61+
62+
63+
64+
565
// Attempt to load the production release first, if it fails fall back to the
666
// debug release.
767
try {

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@
5858
"fs-extra": "^0.18.2",
5959
"node-pre-gyp": "^0.6.5",
6060
"npm": "^2.9.0",
61-
"thenify": "heavyk/thenify",
6261
"thenify-all": "^1.6.0",
6362
"which-native-nodish": "^1.1.1"
6463
},

0 commit comments

Comments
 (0)