Skip to content

Commit 6bb339b

Browse files
Refactor with ES6 standard
1 parent 5bbad46 commit 6bb339b

File tree

4 files changed

+264
-201
lines changed

4 files changed

+264
-201
lines changed

lib/zk_promise.js

+83-41
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,90 @@
1-
const promise = require("./promise");
2-
const ZooKeeper = require ("./zookeeper");
1+
const ZooKeeper = require("./zookeeper");
32

4-
class ZooKeeperPromise extends ZooKeeper {}
3+
class ZooKeeperPromise extends ZooKeeper {
4+
on_connected() {
5+
return new Promise((resolve) => {
6+
this.once('connect', () => {
7+
return resolve(this);
8+
});
9+
})
10+
}
511

6-
exports = module.exports = ZooKeeperPromise;
7-
exports.ZK = ZooKeeperPromise; // backwards compatibility
12+
create() {
13+
return this._promisify(super.a_create, arguments);
14+
}
15+
16+
exists() {
17+
return this._promisify(super.a_exists, arguments);
18+
}
19+
20+
w_exists() {
21+
return this._promisify(super.aw_exists, arguments);
22+
}
23+
24+
get() {
25+
return this._promisify(super.a_get, arguments);
26+
}
27+
28+
w_get() {
29+
return this._promisify(super.aw_get, arguments);
30+
}
31+
32+
get_children() {
33+
return this._promisify(super.a_get_children, arguments);
34+
}
35+
36+
w_get_children() {
37+
return this._promisify(super.aw_get_children, arguments);
38+
}
39+
40+
get_children2() {
41+
return this._promisify(super.a_get_children2, arguments);
42+
}
843

9-
ZooKeeperPromise.prototype.on_connected = function on_connected() {
10-
var deferred = promise.defer();
11-
this.once ('connect', () => {
12-
deferred.resolve(this);
13-
});
14-
return deferred.promise;
15-
};
16-
17-
const convertAsync = (fn) => {
18-
return () => {
19-
var deferred = promise.defer();
20-
arguments.length++;
21-
arguments[arguments.length-1] = (rc, error, result) => {
22-
if (rc) {
23-
deferred.emitError(rc);
24-
} else {
25-
if (arguments.length > 3) {
26-
// if there are multiple success values, we return an array
27-
Array.prototype.shift.call(arguments, 1);
28-
Array.prototype.shift.call(arguments, 1);
29-
deferred.emitSuccess(arguments);
44+
w_get_children2() {
45+
return this._promisify(super.aw_get_children2, arguments);
46+
}
47+
48+
set() {
49+
return this._promisify(super.a_set, arguments);
50+
}
51+
52+
delete_() {
53+
return this._promisify(super.delete_, arguments);
54+
}
55+
56+
get_acl() {
57+
return this._promisify(super.a_get_acl, arguments);
58+
}
59+
60+
set_acl() {
61+
return this._promisify(super.a_set_acl, arguments);
62+
}
63+
64+
sync() {
65+
return this._promisify(super.a_sync, arguments);
66+
}
67+
68+
_promisify(fn, args) {
69+
return new Promise((resolve, reject) => {
70+
const callback = (rc, error, result) => {
71+
if (rc) {
72+
reject(rc);
3073
} else {
31-
deferred.emitSuccess(result);
74+
if (args.length > 3) {
75+
// if there are multiple success values, we return an array
76+
Array.prototype.shift.call(args, 1);
77+
Array.prototype.shift.call(args, 1);
78+
resolve(args);
79+
} else {
80+
resolve(result);
81+
}
3282
}
33-
}
34-
};
35-
fn.apply(this, arguments);
36-
return deferred.promise;
37-
};
38-
};
39-
40-
for (var f in ZooKeeperPromise.prototype) {
41-
var m = f.match(/^a(w?)_(.*)$/);
42-
if (m) {
43-
var new_func = m[1]? m[1] + "_" + m[2] : m[2];
44-
ZooKeeperPromise.prototype[new_func] = convertAsync (ZooKeeperPromise.prototype[f]);
45-
//console.log ("function %s is %j", f, util.inspect(ZK.prototype[new_func],true,3));
46-
}
83+
};
84+
fn.apply(this, [...args, callback])
85+
});
86+
}
4787
}
4888

89+
exports = module.exports = ZooKeeperPromise;
90+
exports.ZK = ZooKeeperPromise; // backwards compatibility

0 commit comments

Comments
 (0)