Skip to content

Commit 39e5d07

Browse files
committed
A better way of wrapping native methods
1 parent e6e2612 commit 39e5d07

1 file changed

Lines changed: 37 additions & 48 deletions

File tree

lib/nodegit.js

Lines changed: 37 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,72 +1,61 @@
1-
var promisify = require('promisify-node');
2-
var descriptors = require('../generate/v0.20.0.json');
1+
var promisify = require("promisify-node");
2+
var descriptors = require("../generate/idefs.json");
33
var rawApi;
44

55
// Attempt to load the production release first, if it fails fall back to the
66
// debug release.
77
try {
8-
rawApi = require('../build/Release/nodegit');
8+
rawApi = require("../build/Release/nodegit");
99
}
1010
catch (e) {
11-
rawApi = require('../build/Debug/nodegit');
11+
rawApi = require("../build/Debug/nodegit");
1212
}
1313

1414
// Set the exports prototype to the raw API.
1515
exports.__proto__ = rawApi;
1616

1717
// Import extensions
18-
require('./commit.js');
19-
require('./diff.js');
20-
require('./blob.js');
21-
require('./object.js');
22-
require('./signature.js');
23-
require('./odb.js');
24-
require('./oid.js');
25-
require('./index.js');
26-
require('./repository.js');
27-
require('./reference.js');
28-
require('./revwalk.js');
29-
require('./tree.js');
18+
require("./commit.js");
19+
require("./diff.js");
20+
require("./blob.js");
21+
require("./object.js");
22+
require("./signature.js");
23+
require("./odb.js");
24+
require("./oid.js");
25+
require("./index.js");
26+
require("./repository.js");
27+
require("./reference.js");
28+
require("./revwalk.js");
29+
require("./tree.js");
3030

3131
// Wrap asynchronous methods to return promises.
3232
promisify(exports);
3333

3434
// Native methods do not return an identifiable function, so this function will
3535
// filter down the function identity to match the libgit2 descriptor.
36-
promisify(rawApi, function(func, keyName, parentKeyName) {
37-
// Find the correct descriptor.
38-
var descriptor = descriptors.filter(function(descriptor) {
39-
var key = parentKeyName ? parentKeyName.slice(0, -1) : keyName;
40-
return descriptor.jsClassName === key;
41-
})[0];
42-
43-
// If this is a top level construct, recurse into.
44-
if (!parentKeyName) {
45-
return true;
46-
}
47-
48-
// Determine if this is a prototype method.
49-
var isPrototypeMethod = parentKeyName.slice(-1) === "#";
50-
51-
if (descriptor && descriptor.functions) {
52-
// Find the nested function in the descriptor.
53-
if (parentKeyName.indexOf("Repo") > -1) { console.log(arguments); }
54-
var nestedFunction = descriptor.functions.filter(function(nestedFunction) {
55-
var test = keyName || func.name || parentKeyName;
56-
57-
if (nestedFunction.jsFunctionName === keyName) {
58-
return func.isPrototypeMethod === isPrototypeMethod;
59-
}
60-
})[0];
61-
62-
// Verify it is an asynchronous method.
63-
//console.log(nestedFunction && nestedFunction.isAsync);
64-
return nestedFunction && nestedFunction.isAsync;
65-
}
36+
descriptors.forEach(function(descriptor) {
37+
var Ctor = rawApi[descriptor.jsClassName];
38+
39+
// Iterate over each function in the file.
40+
descriptor.functions.filter(function(func) {
41+
return func.isAsync;
42+
}).forEach(function(asyncFunc) {
43+
var original = null;
44+
45+
// Special case when you have a prototype method.
46+
if (asyncFunc.isPrototypeMethod) {
47+
original = Ctor.prototype[asyncFunc.jsFunctionName];
48+
Ctor.prototype[asyncFunc.jsFunctionName] = promisify(original);
49+
}
50+
else {
51+
original = Ctor[asyncFunc.jsFunctionName];
52+
Ctor[asyncFunc.jsFunctionName] = promisify(original);
53+
}
54+
});
6655
});
6756

6857
// Set version.
69-
exports.version = require('./package').version;
58+
exports.version = require("../package").version;
7059

7160
// Initialize threads.
72-
exports.Threads.init();
61+
exports.Threads.threadsInit();

0 commit comments

Comments
 (0)