Skip to content

Commit e22fa5c

Browse files
committed
C++ promisifyer
1 parent 2e0a0c6 commit e22fa5c

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

generate/templates/templates/nodegit.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,70 @@
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+
// js reserved words
24+
// "abstract", "arguments", "boolean", "break", "byte", "case", "catch",
25+
// "char", "class", "const", "continue", "debugger", "while", "with",
26+
// "yield", "do", "double", "else", "enum", "eval", "export", "extends",
27+
// "false", "final", "finally", "float", "for", "function", "goto", "if",
28+
// "implements", "import", "in", "instanceof", "int", "interface", "let",
29+
// "long", "native", "new", "null", "package", "private", "protected",
30+
// "public", "return", "short", "static", "super", "switch", "synchronized",
31+
// "this", "throw", "throws", "transient", "true", "try", "typeof", "var",
32+
// "void", "volatile",
33+
34+
"default", "delete"
35+
].indexOf(name) ? "$$" + name : name : "";
36+
37+
/* jshint ignore:start */
38+
return "(function " + name + "() {\n"
39+
+ "var self = this\n"
40+
+ "var len = arguments.length\n"
41+
+ "var args = new Array(len + 1)\n"
42+
+ "for (var i = 0; i < len; ++i) args[i] = arguments[i]\n"
43+
+ "var lastIndex = i\n"
44+
+ "return new Promise(function (resolve, reject) {\n"
45+
+ "args[lastIndex] = createCallback(resolve, reject)\n"
46+
+ "$$__fn__$$.apply(self, args)\n"
47+
+ "})\n"
48+
+ "})"
49+
/* jshint ignore:end */
50+
}
51+
52+
/* jshint ignore:start */
53+
function createCallback(resolve, reject) {
54+
return function(err, value) {
55+
if (err) return reject(err)
56+
var length = arguments.length
57+
if (length <= 2) return resolve(value)
58+
var values = new Array(length - 1)
59+
for (var i = 1; i < length; ++i) values[i - 1] = arguments[i]
60+
resolve(values)
61+
}
62+
}
63+
/* jshint ignore:end */
64+
65+
66+
67+
568
// Attempt to load the production release first, if it fails fall back to the
669
// debug release.
770
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)